mybatis整合spring
mybatis整合spring
- 整合思路
- SqlSessionFactory对象应该放到spring容器中作为单例存在。
- 传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
- Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
- 数据库的连接以及数据库连接池事务管理都交给spring容器来完成。
- 整合步骤
- 创建一个java工程。
- 导入jar包。(课前资料中mybatis与spring整合所有包)
- mybatis的配置文件sqlmapConfig.xml
配置文件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>
<typeAliases>
<package name="com.mybatisspring.pojo"/>
</typeAliases>
</configuration>
- 编写Spring的配置文件
a. 数据库连接及连接池
b. sqlsessionFactory对象,配置到spring容器中
c. 编写Spring的配置文件
applicationContext.xml中代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据库连接池 -->
<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>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 别名包扫描 -->
<property name="typeAliasesPackage" value="com.mybatisspring.pojo"></property>
</bean>
</beans>
- 复制jdbc.properties配置文件到新工程
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookg?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
- 复制log4j.properties配置文件到新工程
# Global logging configuration
log4j.rootLogger=DEBUG,stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
- Dao的开发
- 传统Dao开发
- 复制user.xml到新工程,并修改,只留下要测试的三个方法
- 在SqlMapConfig.xml中加载user.xml
<mappers>
<mapper resource="mybatis/user.xml"></mapper>
</mappers>
- 复制UserDao接口到新工程,并修改,只留下要测试的三个方法
- 编写UserDaoImpl实现类,关键是继承SqlSessionDaoSupport
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User getUserById(Integer id) {
SqlSession sqlSession = super.getSqlSession();
//查询用户
User user = sqlSession.selectOne("user.getUserById", id);
//不能关闭SqlSession
//sqlSession.close();
return user;
}
@Override
public List<User> getUserByUserName(String name) {
SqlSession sqlSession = super.getSqlSession();
List<User> list = sqlSession.selectList("user.getUserByName", name);
//不能关闭SqlSession
return list;
}
@Override
public void insertUser(User user) {
SqlSession sqlSession = super.getSqlSession();
sqlSession.insert("user.insertUser", user);
//不用手动提交事务,交给spring
}
}
-
在applicationContext.xml中配置UserDaoImpl实现类
<bean class="com.itheima.mybatis.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
-
编写测试类,新建单完测试类
public class UserDaoTest {
private ApplicationContext applicationContext;
@Before
public void init(){
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testGetUserById() {
UserDao userDao = applicationContext.getBean(UserDao.class);
User user = userDao.getUserById(10);
System.out.println(user);
}
}
- Mapper代理开发Dao
- 复制UserMapper.xml到新工程,并修改,只留下要测试的三个方法
- 复制UserMapper接口到新工程,并修改,只留下要测试的三个方法
- 配置Mapper
a. 单个接口配置MapperFactoryBean
b. 配置包扫描器<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 用户动态代理扫描 --> <bean parent="baseMapper"> <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper" /> </bean>
<!-- 动态代理Dao开发,第一种方式,包扫描器(推荐使用) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage:配置映射包装扫描,多个包时用","或";"分隔 --> <property name="basePackage" value="com.itheima.mybatis.mapper" /> </bean>
- 测试
public class UserMapperTest {
private ApplicationContext applicationContext;
@Before
public void init(){
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testGetUserById() {
UserMapper userMapper = applicationContext.getBean(UserMapper.class);
User user = userMapper.getUserById(10);
System.out.println(user);
}