http://blog.csdn.net/qq598535550/article/details/51703190
二、Spring整合mybatis其实是在mybatis的基础上实现Spring框架
1、依赖的jar包,maven引入依赖:
junit junit 4.3 test log4j log4j 1.2.17 org.mybatis mybatis 3.2.2 mysql mysql-connector-java 5.1.43 org.springframework spring-beans 4.2.3.RELEASE org.springframework spring-core 4.2.0.RELEASE org.springframework spring-context 4.2.2.RELEASE org.springframework spring-expression 4.2.2.RELEASE org.aspectj aspectjweaver 1.8.7 org.springframework spring-jdbc 4.2.0.RELEASE com.alibaba druid 1.0.18 commons-dbcp commons-dbcp 1.4 com.mchange c3p0 0.9.5.2 org.mybatis mybatis-spring 1.2.0
简化依赖:webmvc
Spring整合Myabtis依赖的:
org.mybatis mybatis-spring 1.2.0
2、编写核心配置文件(mybatis的许多功能都可以使用Spring来完成)
mybatis-config.xml的很多功能可以移除
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><package name="cn.tengyu.entity"/> <package name="cn.tengyu.dao"/>
mapper.xml不用修改
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
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="cn.tengyu.dao.EmpMapper">
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
<select id="count" resultType="int">
SELECT count(1) AS count FROM emp
select>
<select id="findAll" resultType="Emp">
SELECT <include refid="empColumn"/> FROM emp
select>
<sql id="empColumn">
empno,empname,deptno
sql>
<resultMap id="EmpResult" type="Emp">
<id property="empno" column="empno"/>
<result property="empname" column="empname"/>
<association property="dept" javaType="Dept">
<id property="deptno" column="deptno"/>
<result property="deptname" column="deptname"/>
association>
resultMap>
<select id="findByIndex" resultMap="EmpResult">
SELECT e.empno,e.empname,d.deptno,d.deptname FROM emp AS e,dept AS d WHERE e.deptno=d.deptno LIMIT #{startIndex},#{endIndex}
select>
mapper>
applicationContext.xml添加功能,即Spring接管mybatis里面的SqlSessionFactory等Bean的实现
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties"/>
bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config2.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tengyu.dao"/>
bean>
<bean id="deptService" class="cn.tengyu.service.impl.DeptServiceImpl">
<property name="dao" ref="deptMapper"/>
bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
3、service的实现依然使用私有接口属性
4.数据源可以灵活使用
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="database.properties"/> bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> bean>
5.mapper的使用很简单
<bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config2.xml"/> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.tengyu.dao"/> bean> <bean id="deptService" class="cn.tengyu.service.impl.DeptServiceImpl"> <property name="dao" ref="deptMapper"/> bean>
测试:
@Test public void findAll(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IDeptService deptService = (IDeptService) context.getBean("deptService"); Listlist = deptService.findAll(); for (Dept o :list) { logger.debug(o.getDeptno()+"\t"+o.getDeptname()); } } @Test public void Test02(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IDeptService deptService = (IDeptService) context.getBean("deptService"); Dept dept = new Dept(); dept.setDeptname("测试部"); int i = deptService.addDept(dept); logger.debug("执行"+i+"条数据!"); }
注:另外需要灵活使用Spring的AOP进行事务控制