阅读教程实例,我们了解原理后可以自己模仿地写一个程序练练手。
1、创建表,然后手动导入数据
create table `four kings`
(
id int primary key,
name varchar(4) ,
idea varchar(20)
)CHARSET=utf8;;
2、 写一个实体类,对应数据库的表
package Num;
public class fk {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdea() {
return idea;
}
public void setIdea(String idea) {
this.idea = idea;
}
private int id;
private String name;
private String idea;
}
3、src中配置文件,仔细看教程,对应好自己的文件名、文件位置以及数据库
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="Num"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/cart?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Num/fkxml.xml"/>
</mappers>
</configuration>
4、配置包中文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Num">
<select id="listfk" resultType="fk">
select * from four kings
</select>
</mapper>
5、测试类,注意这个测试直接在运行方式是直接java运行,不用在服务器上运行
package controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import Num.fk;
public class TestMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
List<fk> fourkings =session.selectList("listfk");
for (fk c : fourkings) {
System.out.println(c.getName() + ' ' + c.getIdea());
}
}
}
没问题
接下来完成教材案例。还是老套路,大致阅读源代码拿到需求之后,我们自己来写一份代码(日志功能我就没有写了,主要是debug用的)。教材源代码是拿着id或者模糊查询来找用户,我们可以来写个拿着账号来找用户。
1、创建数据库,并插入数据
create table `data`
(
name varchar(10) primary key,
zhanghao varchar(20) not null UNIQUE,
password varchar(20) not null UNIQUE,
age int
)CHARSET=utf8;;
2、写一个实体类,对应表中数据
package Num;
public class fk {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZhanghao() {
return zhanghao;
}
public void setZhanghao(String zhanghao) {
this.zhanghao = zhanghao;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private String zhanghao;
private String password;
private int age;
}
3、src中配置文件。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="Num"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/cart?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Num/fkxml.xml"/>
</mappers>
</configuration>
4、配置包中文件,注意sql语句的书写,以及基本对象parameterType要与账号String相对应
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Num">
<select id="query" parameterType="String" resultType="fk">
select * from `data` where zhanghao=#{zhanghao}
</select>
</mapper>
5、编写一个测试类,输入你的账号,系统会输出这个账号的相关信息
package controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import Num.fk;
public class TestMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
fk user = session.selectOne("query",s);
System.out.println("姓名:" + user.getName());
System.out.println("年龄:" + user.getAge());
session.commit();
session.close();
}
}
结果没问题: