一. 配置映射文件方式
1. 导入 jar 包
MyBatis 下载
- 导入
mybatis-3.4.5.jar
- 导入
mysql-connector-java-5.1.44-bin.jar
2. 创建实体类
public class Student {
private String stu_name;
private Integer stu_id;
private Integer stu_age;
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public Integer getStu_age() {
return stu_age;
}
public void setStu_age(Integer stu_age) {
this.stu_age = stu_age;
}
@Override
public String toString() {
return "Student{" +
"stu_name='" + stu_name + '\'' +
", stu_id=" + stu_id +
", stu_age=" + stu_age +
'}';
}
}
3.MyBatis 配置
- 编写全局配置文件
<?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>
<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:///test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="studentMapper.xml"/>
</mappers>
</configuration>
- 编写映射文件
<?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="test">
<select id="testId" resultType="com.mybatis.test.Student">
select * from student where stu_id = #{id}
</select>
</mapper>
4. 单元测试
public class MyBatisHelloWorld {
@Test
public void test() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Object o = sqlSession.selectOne("test.testId", 1);
System.out.println(o);
}
}
- 注意数据库有对应的记录
- 若实体类的字段和数据库的字段名不一致可采用别名方式对应
二. 面向接口方式
1. 创建一个接口
创建接口,定义抽象方法.
public interface IStudent {
Student getStudent(Integer id);
}
2. 修改映射文件
<?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="com.mybatis.test.IStudent">
<select id="getStudent" resultType="com.mybatis.test.Student">
select * from student where stu_id = #{id}
</select>
</mapper>
修改以下信息:
- 将
namespace
改为接口的全类名- 将
id
改为接口中定义的方法名
3. 编写测试方法
@Test
public void test2() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
IStudent mapper = sqlSession.getMapper(IStudent.class);
System.out.println(mapper.getStudent(1));
}
- 使用
sqlSession.getMapper(IStudent.class);
方法获取接口实现的代理对象sqlSession
是非线程安全的,所以不能做成员变量.每次需要时重新获取.