spring和mybatis的整合配置
参考自:
http://www.cnblogs.com/wangmingshun/p/5674633.html
链接中的文章里一共有三种整合方式,太多了怕记混了。
我这里只保留第二种。
spring中引入properties文件至少有以下两种方式:
a.
<!-- 引入db.properties --> <context:property-placeholder location="classpath:db.properties" />
b.
1 <!-- 引入properties文件 --> 2 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 3 <property name="location" value="classpath:jdbc.properties" /> 4 </bean>
我觉得还是第一种比较更简单一些。
2、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 自动扫描 --> 13 <context:component-scan base-package="com.hua.saf" /> 14 <!-- 引入properties文件 --> 15 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 16 <property name="location" value="classpath:jdbc.properties" /> 17 </bean> 18 19 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 20 <property name="driverClassName" value="${driver}" /> 21 <property name="url" value="${url}" /> 22 <property name="username" value="${username}" /> 23 <property name="password" value="${password}" /> 24 <!-- 初始化连接大小 --> 25 <property name="initialSize" value="${initialSize}" /> 26 <!-- 连接池最大数量 --> 27 <property name="maxActive" value="${maxActive}" /> 28 <!-- 连接池最大空闲 --> 29 <property name="maxIdle" value="${maxIdle}" /> 30 <!-- 连接池最小空闲 --> 31 <property name="minIdle" value="${minIdle}" /> 32 <!-- 获取连接最大等待时间 --> 33 <property name="maxWait" value="${maxWait}" /> 34 </bean> 35 36 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 37 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 38 <property name="dataSource" ref="dataSource" /> 39 <property name="configLocation" value="classpath:sqlMapConfig.xml"/> 40 <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件--> 41 <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" /> 42 </bean> 43 44 <!-- mybatis spring sqlSessionTemplate,使用时直接让spring注入即可 --> 45 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 46 <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> 47 </bean> 48 49 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 50 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 51 <property name="dataSource" ref="dataSource" /> 52 </bean> 53 </beans>
sqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC 3 "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <typeAlias type="com.hua.saf.pojo.User" alias="User" /> 8 </typeAliases> 9 </configuration>
User.java
1 public class User { 2 3 private int id; 4 private String username; 5 private String password; 6 private int age; 7 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 public String getPassword() { 21 return password; 22 } 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 public int getAge() { 27 return age; 28 } 29 public void setAge(int age) { 30 this.age = age; 31 } 32 }
UserDao.java
1 @Repository 2 public class UserDao{ 3 4 @Resource 5 private SqlSessionTemplate sqlSessionTemplate; 6 7 public User getUser(int id) { 8 return sqlSessionTemplate.selectOne(this.getClass().getName() + ".getUser", 1); 9 } 10 }
UserService.java
1 @Service 2 public class UserService{ 3 4 @Resource 5 private UserDao userDao; 6 7 public User getUser(int id) { 8 return userDao.getUser(id); 9 } 10 }