http://blog.csdn.net/qq598535550/article/details/51703190
二、Spring整合mybatis其实是在mybatis的基础上实现Spring框架
1、依赖的jar包,maven引入依赖:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.3</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <!--****************************************************--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.2.RELEASE</version> </dependency> <!--织入的包--> <dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.8.7</version> </dependency> <!--添加数据源--> <!--spring JDBCTemplate--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <!--dbcp 数据源--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--c3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--spring整合mybatis--> <!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency>
简化依赖:webmvc
Spring整合Myabtis依赖的:
<!--spring整合mybatis--> <!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency>
2、编写核心配置文件(mybatis的许多功能都可以使用Spring来完成)
mybatis-config.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="cn.tengyu.entity"/> </typeAliases> <mappers> <package name="cn.tengyu.dao"/> </mappers> </configuration>
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="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> <!--方法(1)--> <!--<resultMap id="DeptResult" type="Dept"> <id property="deptno" column="deptno"/> <result property="deptname" column="deptname"/> </resultMap> <resultMap id="EmpResult" type="Emp"> <id property="empno" column="empno"/> <result property="empname" column="empname"/> <association property="dept" javaType="Dept" resultMap="DeptResult"/> </resultMap> <select id="findByIndex" resultMap="EmpResult"> SELECT emp.*,dept.* FROM emp,dept WHERE emp.deptno=dept.deptno LIMIT #{startIndex},#{endIndex} </select>--> <!--方法(2)--> <!--<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 e,dept d WHERE e.deptno=d.deptno LIMIT #{startIndex},#{endIndex} </select>--> <!--方法(3)--> <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元素生命需要Spring创建的实例。该实例的类型通过class属性指定, 并通过id属性为该实例制定一个名称,以便于访问--> <!--引入数据库配置文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="database.properties"/> </bean> <!--配置数据源--> <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--alibaba--> <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--dbcp--> <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--c3p0--> <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> <!--引入Mybatis文件--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config2.xml"/> </bean> <!--mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.tengyu.dao"/> </bean> <!--service--> <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.数据源可以灵活使用
<!--引入数据库配置文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="database.properties"/> </bean> <!--配置数据源--> <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--alibaba--> <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--dbcp--> <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--c3p0--> <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的使用很简单
<!--引入Mybatis文件--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config2.xml"/> </bean> <!--mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.tengyu.dao"/> </bean> <!--service--> <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"); List<Dept> list = deptService.findAll(); for (Dept o :list) { logger.debug(o.getDeptno()+" "+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进行事务控制