使用Spring整合MyBatis,需要将MyBatis的管理权交给Spring管理,也就是将MyBatis的SqlSessionFactory交由Spring,如果需要将SqlSessionFactory交给Spring管理,一共有三种方法:
一、让DAO层实现类继承SqlSessionDaoSupport
1.导入jar包
Spring整个MyBatis除了需要Spring和MyBatis的jar包之外,还需要一个整合包:mybatis-spring.jar
2.新建实体类和数据表
数据表为student,有三个字段stuno、stuname、stuage
实体类分别为对应的三个属性。
3.新建Spring的配置文件:ApplicationContext.xml
在Spring的配置文件中配置数据库,使用commons-dbcp.jar包中的BasicDataSource类进行配置,不需要在MyBatis的conf.xml中配置。
还需要使用mybatis.spring.jar包中的SqlSessionFactoryBean配置sqlSessionFactory。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 引入属性文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 配置数据库 代替MyBatis中的conf.xml -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mapper.xml文件 -->
<property name="mapperLocations" value="com/lee/mapper/*.xml"></property>
</bean>
<bean id="studentDao" class="com.lee.dao.impl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="com.lee.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentDao"></property>
</bean>
</beans>
4.mapper.xml配置文件
在mapper.xml文件中和MyBatis中写法一样。
<?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.lee.mapper.StudentMapper">
<select id="queryStudentByStuNo" parameterType="int" resultType="com.lee.entity.Student">
select * from student where stuNo = #{stuNo}
</select>
<select id="addStudent" parameterType="com.lee.entity.Student">
insert into student(stuNo,stuName,stuAge) values(#{stuNo},#{stuName},#{stuAge})
</select>
</mapper>
5.接口
接口也和MyBatis中一样,mapper.xml配置文件的namespace也就是接口的全类名。
package com.lee.mapper;
import com.lee.entity.Student;
public interface StudentMapper {
public void addStudent(Student student);
Student queryStudentByStuNo(int stuNo);
}
6.DAO层-接口的实现类
在DAO层需要实现接口StudentMapper 和继承父类SqlSessionDaoSupport ,继承该父类之后即可直接通过父类的getSqlSession()方法获取到session对象,获取到session对象之后即和MyBatis操作增删改查一样。
在applicationContext.xml配置文件中将DAO层放入IOC容器的时候需要给其父类SqlSessionDaoSupport的属性sqlSessionFactory赋值在配置文件中配置好的sqlSessionFactory。
package com.lee.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.lee.entity.Student;
import com.lee.mapper.StudentMapper;
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public void addStudent(Student student) {
SqlSession session = super.getSqlSession();
StudentMapper stuDao = session.getMapper(StudentMapper.class);
stuDao.addStudent(student);
}
@Override
public Student queryStudentByStuNo(int stuNo) {
SqlSession session = super.getSqlSession();
StudentMapper stuDao = session.getMapper(StudentMapper.class);
Student student = stuDao.queryStudentByStuNo(stuNo);
return student;
}
}
7.Service层
在Service层中需要调用DAO层,所以需要DAO层的构造方法在将Service加入到IOC容器的时候将DAO赋值给Service
package com.lee.service.impl;
import com.lee.entity.Student;
import com.lee.mapper.StudentMapper;
import com.lee.service.IStudentService;
public class StudentServiceImpl implements IStudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
@Override
public Student queryStudentByStuNo(int stuNo) {
return studentMapper.queryStudentByStuNo(stuNo);
}
}
8.测试
在测试中通过Spring获取到IOC容器中的Service,即可调用Service的方法,然后Service再调用DAO层的方法。
package com.lee.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lee.entity.Student;
import com.lee.service.IStudentService;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStudentService studentService = (IStudentService)context.getBean("studentService");
Student student = new Student(1,"zs",23);
studentService.addStudent(student);
Student studentInfo = studentService.queryStudentByStuNo(1);
System.out.println(studentInfo);
}
}
二、使用MapperFactoryBean替换掉DAO层实现类
DAO层的实现类在mybatis-spring.jar包中已经写好了,所以不需要我们自己写实现类,可以将applicationContext.xml配置文件中的DAO层实现类的bean替换为MapperFactoryBean,在该类中需要配置两个属性,一个是mapperInterface,该属性指定该类实现的接口,另一个属性是sqlSessionFactory,该属性指定配置好的SqlSessionFactory对象。
除了修改该实现类,别的不用修改。
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.lee.mapper.StudentMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
三、批量实现Mapper对象
在使用第二种方法的时候有一个缺点:每有一个接口就必须在applicationContext.xml文件中配置一个对应的MapperFactoryBean,所以第三种方法就是使用MapperScannerConfigurer类批量实现mapper类。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.lee.mapper"></property>
</bean>
使用MapperScannerConfigurer批量实现类的话需要配置两个属性,sqlSessionFactoryBeanName:和sqlSessionFactory一样,指定sqlSessionFactory,basePackage:指定需要批量实现的接口所在的包。
约定:使用该方法批量实现mapper对象,bean的id值就是接口的名称的首字母小写。
SSM整合:SSM整合_良猿啊-CSDN博客
关注公众号:良猿,回复关键词“电子书”即可获得50本Java学习精品书籍,收集不易,多多支持哦。