Spring整合mybatis
1、依赖的jar包,maven引入依赖:
<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency> </dependencies>
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> <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>
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>
自 1.3.0 版本开始,新增的 configuration 属性能够在没有对应的 MyBatis XML 配置文件的情况下,直接设置 Configuration 实例。例如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property> </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()+"\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+"条数据!"); }