Mybatis+Sping整合
思路:将mybatis框架的对象,交给spring管理。
mybatis框架包
spring框架包
mybaits-spring整合包
数据库驱动包
数据库连接池包(dbcp)
log4j日志包
junit单元测试包
配置别名
配置加载mapper映射文件(在这里配置的是原始的dao开发方法的配置文件)
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="cn.bdqn.ssm.po"/> typeAliases>
<mappers> <mapper resource="sqlmap/User.xml"/> mappers>
configuration> |
配置数据源对象(dataSource)
配置mybatis核心对象(SqlSessionFactory)
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:db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" />
<property name="maxActive" value="${db.maxActive}" />
<property name="minIdle" value="${db.minIdle}" />
<property name="maxIdle" value="${db.maxIdle}" />
<property name="initialSize" value="${db.initialSize}" />
<property name="maxWait" value="${db.maxWait}" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> bean> beans> |
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/dbqn db.username=root db.password=root
db.maxActive=10 db.minIdle=2 db.maxIdle=5 db.initialSize=5 db.maxWait=6000 |
# 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 |
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="queryUserById" parameterType="int" resultType="user" > select * from `user` where id=#{id} select>
<insert id="insertUser" parameterType="user"> insert into `user`(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) insert>
mapper> |
package cn.bdqn.ssm.dao;
import cn.bdqn.ssm.po.User;
public interface UserDao {
//1.根据用户id查询用户 User queryUserById(Integer id);
// 2.新增一个用户 void insertUser(User user);
} |
说明:整合包提供了SqlSessionDaoSupport类,我们的dao实现类需要继承SqlSessionDaoSupport类,获取到sqlSession对象。
package cn.bdqn.ssm.dao.impl;
import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.bdqn.ssm.dao.UserDao; import cn.bdqn.ssm.po.User;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
/** * 根据用户id查询用户 */ public User queryUserById(Integer id) { // 1.获取sqlSession对象 SqlSession sqlSession = this.getSqlSession();
// 2.使用sqlSession对象,调用方法执行 Object user = sqlSession.selectOne("test.queryUserById", id);
/** * 释放资源: * 与spring整合以后,sqlSession对象交给spring管理,不需要再手动释放(不能) */
return (User) user; }
/** * 新增一个用户 */ public void insertUser(User user) { // 1.获取sqlSession对象 SqlSession sqlSession = this.getSqlSession();
// 2.使用sqlSession对象,调用方法执行 sqlSession.insert("test.insertUser", user);
/** * 释放资源: * 1.与spring整合以后,sqlSession对象交给spring管理,不需要再手动释放(不能) * 2.与spring整合以后,sqlSession对象交给spring管理,不需要再关心事务 * (如果配置了spring的事务,使用spring事务;如果没有配置,使用jdbc的事务) */
}
} |
<bean id="userDao" class="cn.bdqn.ssm.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/> bean> |
package cn.bdqn.ssm.test;
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.bdqn.ssm.dao.UserDao; import cn.bdqn.ssm.po.User;
/** * @ClassName: UserDaoTest * @Description:用户dao开发方法测试 * @author 老师 * @date 2018-4-9 下午2:40:34 * */ public class UserDaoTest {
/** * 测试根据用户id查询用户 */ @Test public void queryUserByIdTest(){
// 1.加载spring配置文件,创建spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
// 2.从容器中,获取用户dao对象 UserDao userDao = (UserDao) context.getBean("userDao");
User user = userDao.queryUserById(2); System.out.println(user);
}
} |
测试新增用户:
/** * 测试新增一个用户 */ @Test public void insertUserTest(){ // 1.加载spring配置文件,创建spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
// 2.从容器中,获取用户dao对象 UserDao userDao = (UserDao) context.getBean("userDao");
// 创建用户对象 User user = new User(); user.setUsername("李云龙"); user.setBirthday(new Date()); user.setSex("1"); user.setAddress("独立团");
userDao.insertUser(user); } |
说明:整合包提供了一个MapperFactoryBean类,用于配置mapper代理对象。
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="cn.bdqn.ssm.mapper.UserMapper"/>
|
测试新增用户:
/** * 测试新增用户 */ @Test public void insertUserTest(){
// 1.加载spring配置文件,创建spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
// 2.从容器中获取mapper代理对象 UserMapper mapper = (UserMapper) context.getBean("userMapper");
// 创建用户对象 User user = new User(); user.setUsername("楚云飞"); user.setBirthday(new Date()); user.setSex("1"); user.setAddress("国军");
mapper.insertSelective(user); } |
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.bdqn.ssm.mapper"/> bean> |