需要spring通过单例方式管理SqlSessionFactory
spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession(spring和mybatis整合自动完成),持久层的mapper需要由spring进行管理
整合环境搭建
- jar包
- spring包
- mybatis_spring整合包
- mybatis包
- …
SqlSessionFactory配置
applicationContext.xml
在applicationContext.xml配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-benas-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--sqlSessionFactory-->
<bean id="ssqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载mybatis配置文件-->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--数据源,使用dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!--加载配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
</beans>
原始dao开发
mapper.xml
<?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="findUserByID" parameterType="int" resultType="com.sws.mapper.User">
select * from user where id = #{id}
</select>
</mapper>
在SqlMapConfig.xml中加载user.xml
<?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="com.sws.mapper"/>
</typeAliases>
<!--加载映射文件-->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>
dao(继承SqlSessionDaoSupport)
UserDao.java
package com.sws.dao;
import com.sws.mapper.User;
public interface UserDao {
public User findUserById(int id) throws Exception;
}
dao接口实现UserDaoImp.java,通过spring注入sqlSessionFactory
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
public User findUserById(int id) throws Exception {
//继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlsession
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("test.findUserById",id);
return user;
}
}
配置dao
applicationContext.xml
<!--原始dao接口-->
<bean id="userDao" class="com.sws.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
mapper代理开发
mapper.xml和mapper.java
<?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.sws.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
package com.sws.mapper;
import com.sws.entity.User;
public interface UserMapper {
public User findUserById(int id) throws Exception;
}
通过mapperFactoryBean创建代理对象
applicationContext.xml配置
<!--mapper配置
MapperFactoryBean根据mapper接口生成代理对象-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.sws.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
SqlMapConfig.xml
<!--加载映射文件-->
<mappers>
<package name="com.sws.mapper"/>
</mappers>
问题
需要争对每一个mapper进行配置
解决方法
applicationContext.xml
<!--mapper批量扫描,从mapper包中扫描mapper接口,自动创建代理对象并且在spring容器中注册
mapper接口类名和mapper.xml映射文件保持一致,且在同一目录下
自动扫描出的mapper的bean的id为mapper类名(首字母小写)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定扫描包名
如果扫描多个包,包名使用逗号隔开
-->
<property name="basePackage" value="com.sws.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
SqlMapConfig.xml
<mappers>
<!--和spring整合后,使用mapper扫描器,这里可以不需要配置了-->
<package name="com.sws.mapper"/>
</mappers>