将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring来管理。所以,该整合,只需要将 SqlSessionFactory 的对象生成器SqlSessionFactoryBean 注册在 Spring 容器 中,再将其注入给 Dao 的实现类即可完成整合。
整合思路:
1、SqlSessionFactory对象应该放到spring容器中作为单例存在。
2、传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
3、Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成。
添加依赖
需要使用Mybatis提供的Mybatis和spring的整合包mybatis-spring
所以pom.xml中需要提供spring,Mybatis,mybatis-spring.还有数据库连接的驱动坐标
<!--spring依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><!--MySQL驱动依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!--连接池依赖--><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!--Mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!--Mybatis与spring整合的依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency>
定义Mybatis主配置文件SqlMapConfig.xml
注:这个一定要有,主要一些连接数据库的操作,将在spring中完成
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--一些别名的配置还是再这里完成主要连接数据的那块挪走--><typeAliases><!--....--></typeAliases><!--mapper映射文件的路径也在这里完成--><mappers><package name=""/></mappers></configuration>
配置spring的配置文件application.xml
配置连接池
<!-- 加载配置文件 --><context:property-placeholder location="classpath:jdbcConfig.properties" /><!--这里使用spring中自带的数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!--设置数据源属性--><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>
把SQLSessionFactory对象交给spring管理,创建该对象
<!--创建SQLsessionfactory对象,这个对象是由Mybatis和spring的整合包提供的--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ><!--加在Mybatis的核心配置文件--><property name="configLocation" value="classpath:SqlMapConfig.xml"/><!--注入数据源--><property name="dataSource" ref="dataSource"/></bean>
相当于之前在Mybatis中

使用原始的dao开发(不使用代理模式)
public class User {private int id;private String username;private String address;//set,get方法}
准备简单的dao层
public interface UserDao {User findById(Integer id);}
实现类,并同时继承工厂SqlSessionDaoSupport 支持
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {@Overridepublic User findById(Integer id) {//从工厂中获取sqlSession对象return this.getSqlSession().selectOne("findById",id);}}

在spring容器中创建dao层,因为继承了SqlSessionDaoSupport .所以这个类提供了set方法注入,原码如上图
<!--创建dao层--><bean id="userDao" class="com.huge.dao.impl.UserDaoImpl"><!--注入工厂--><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean>
相当于之前

映射文件mapper的开发
<mapper namespace="test"><select id="findById" parameterType="int" resultType="com.huge.pojo.User">select * from user where id = ${value}</select></mapper>
MySQL的主配置文件sqlMapConfig.xml中加载映射文件
<mappers><mapper resource="mapper/User.xml"/></mappers>
测试代码
public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext3.xml");UserDaoImpl userDaoImp = ac.getBean("userDao", UserDaoImpl.class);User user = userDaoImp.findById(2);System.out.println(user);}
Mapper代理形式开发dao(XML)
只有接口,没有实现类
public interface UserDao {User findById(Integer id);}
在spring容器中创建代理对象,通过MapperFactoryBean这个对象工厂
<!--创建代理对象--><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!--提供接口--><property name="mapperInterface" value="com.huge.dao.UserDao"/><!--注入工厂--><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean>

相当于:

映射文件
<mapper namespace="com.huge.dao.UserDao"><select id="findById" parameterType="int" resultType="com.huge.pojo.User">select * from User where id = ${value}</select></mapper>
加载配置文件
<mappers><package name="com.huge.dao"/></mappers>
测试方法:
public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext3.xml");UserDao userMapper = ac.getBean("userMapper", UserDao.class);User user = userMapper.findById(1);System.out.println(user);}
注解实现:
配置开启创建代理对象的配置类
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置需要扫描的包--><property name="basePackage" value="com.huge.dao" /></bean>
测试:
public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext3.xml");UserDao userMapper = ac.getBean(UserDao.class);User user = userMapper.findById(1);System.out.println(user);}
喜欢转发
明天见





