整合的思路:将SqlSessionFactory交给Spring管理;
使用步骤:
一 、引入依赖
需要引入Spring-Mybatis整合的依赖包、和Mybatis的依赖包,(当然,还有Spring的基本依赖包,前面说过,这只写需要新添加的):
org.mybatis
mybatis-spring
1.3.0
org.mybatis
mybatis
3.4.1
先来介绍这一种:
二、创建相关的类
创建对应的bean类:
public class Student {
private int id;
private String name;
private String sex;
private int age;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
Mapper接口:
public interface StudentMapper {
public Student getStudentById(int id);
}
dao层对象:
public class StudentDao {
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Student getStudentBean(int id) {
//获取sqlSession实例
SqlSession sqlSession = sqlSessionFactory.openSession();
Student student = (Student) sqlSession.selectOne("com.tulun6.dao.StudentMapper.getStudentById", id);
return student;
}
}
这个dao层类还可以这样写,更简单:
public class StudentDao extends SqlSessionDaoSupport {
public Student getStudentBean(int id) {
//获取sqlSession实例
SqlSession sqlSession = this.getSqlSession();
Student student = (Student) sqlSession.selectOne("com.tulun6.dao.StudentMapper.getStudentById", id);
return student;
}
}
因为SqlSessionDaoSupport类里面本身就有SqlSessionFactory属性,并提供了set方法,所以直接继承拿人家的用就好了;
service层:
public class StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student getStudentBean(int id) {
return studentDao.getStudentBean(id);
}
}
controller层:
public class StudentController {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public Student getStudentById(int id) {
return studentService.getStudentBean(id);
}
}
三、创建相关的配置文件
然后就是配置文件,StudentMapper.xml:
mybatis的配置文件,mybatis.xml:
其实更多的时候,我们会把映射文件写在mybatis的配置文件里面,这样可以减少对Spring配置文件的依赖,但这一节为了介绍如何在Spring里面配置映射,就没在这儿写(就是注释掉的那部分语句,但是要知道,写在这儿也是可以的,只是Spring关于映射的语句就不能写了,不然有两个,它就不知道怎么办了,就会报错),下面就是Spring的配置文件spring-mybatis.xml:
spring-mybatis整合/StudentMapper.xml
上面spring的配置文件里面我打标记的那部分语句和mybatis里面的映射语句只能存在一个!!
四、使用
public class TestDemo1 {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis整合/spring-mybatis.xml");
StudentController studentController = (StudentController)context.getBean("controller");
Student student = studentController.getStudentById(1);
System.out.println(student);
}
}
这种写法就是使用Mapper代理对象进行映射开发,就像在Mybatis框架里面一样,我们要通过SqlSessionFactory最终得到Mapper对象,然后使用一样:
bean层类不用改,dao层的类也不用改,因为不用dao层的对象了,service需要改一下(属性是Mapper接口类型的):
public class StudentService1 {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
public Student getStudentBean(int id) {
return studentMapper.getStudentById(id);
}
}
那对应的controller层类也就需要改一下了:
public class StudentController1 {
private StudentService1 studentService1;
public void setStudentService1(StudentService1 studentService1) {
this.studentService1 = studentService1;
}
public Student getStudentById(int id) {
return studentService1.getStudentBean(id);
}
}
三个配置文件,需要该spring的配置文件(改的是我写标记的那部分):
spring-mybatis整合/StudentMapper.xml
测试:
public class TestDemo2 {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis整合/spring-mybatis-plus.xml");
StudentController1 studentController1 = (StudentController1) context.getBean("controller1");
Student student = studentController1.getStudentById(1);
System.out.println(student);
}
}