1、整合思路
(1)数据源信息交给spring管理
(2)SqlSessionFactory交给spring进行单例管理
(3)由spring来管理原始dao的实现类或者mapper代理的代理类
2、案例需求
(1)使用原始dao方式和mapper代理方式实现以下功能:
根据用户ID查询商品信息
3、工程创建
(1)添加jar:ysql的驱动包;Mybatis的核心包和依赖包;Mybatis和spring的整合包;Spring的包;dbcp数据库连接池包
(2)图解:
4、整合配置文件
(1)mybatis配置文件:
在config下,创建mybatis目录,然后创建SqlMapConfig.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>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="mybatis/sqlmap/User.xml"/>
</mappers>
</configuration>
(2)spring配置文件:
在config下,创建spring目录,然后创建applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加载Java配置文件 -->
<context:property-placeholder location="db.properties"/>
<!-- 创建数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全局配置路径 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- userDao -->
<bean id="userDao" class="com.san.dao.UserDaoImpl">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 配置UserMapper代理类 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 设置代理类的接口 -->
<property name="mapperInterface" value="com.san.mapper.UserMapper"/>
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
5、整合代码—原始dao开发
(1)映射文件:
在config/mybatis下创建sqlmap,然后创建User.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">
<!-- 根据用户ID查询用户信息 -->
<select id="findUserById" resultType="com.san.po.User" parameterType="int">
select * from user where id=#{id}
</select>
</mapper>
(2)dao代码:
public interface UserDao {
/**
* 根据用户ID查询用户信息
* @param id
* @return
*/
public User findUserById(int id);
}
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) {
return this.getSqlSession().selectOne("test.findUserById",id);
}
}
(3)配置UserDao实现类:
在applicationContext.xml中配置UserDao实现类
<!-- userDao -->
<bean id="userDao" class="com.san.dao.UserDaoImpl">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
(4)测试代码:
private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindUserById() {
UserDao userDao = (UserDao) cxt.getBean("userDao");
User user= userDao.findUserById(1);
System.out.println(user);
}
6、整合代码—mapper代理
(1)映射文件:
将映射文件放到UserMapper接口的同包下
<?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.san.mapper.UserMapper">
<!-- 根据用户ID查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="com.san.po.User">
select * from user where id=#{id}
</select>
</mapper>
(2)mapper接口:
public interface UserMapper {
/**
* 根据用户信息查询用户信息
* @param id
* @return
*/
public User findUserById(int id);
}
(3)配置mapper代理类:
单个mapper代理类配置:
<!-- 配置UserMapper代理类 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 设置代理类的接口 -->
<property name="mapperInterface" value="com.san.mapper.UserMapper"/>
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
批量设置mapper代理类:
<!-- 批量配置mapper代理类,默认bean的id为类名首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描的包 -->
<property name="basePackage" value="com.san.mapper"></property>
<!-- 默认不需要配置SqlSessionFactory(只有一个SqlSessionFactory时),单独配置也可以 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
(4)测试代码:
private ApplicationContext cxt;
@Before
public void setUp() throws Exception {
cxt = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindUserById() {
UserMapper mapper = (UserMapper) cxt.getBean("userMapper");
User user = mapper.findUserById(1);
System.out.println(user);
}