Spring和Mybatis的四种整合方式
一、采用org.mybatis.spring.mapper.MapperScannerConfigurer
整体结构如下图:
1、配置文件
1>applicationContext01.xml:
- xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:p="http://www.springframework.org/schema/p"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
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!-- 自动扫描 -->
- <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>
- <!-- 引入外置文件 -->
- <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertynamepropertyname="location"value="classpath:jdbc.properties"/>
- </bean>
- <!--数据库连接池配置-->
- <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>
- <propertynamepropertyname="url"value="${jdbc.url}"/>
- <propertynamepropertyname="username"value="${jdbc.username}"/>
- <propertynamepropertyname="password"value="${jdbc.password}"/>
- </bean>
- <!-- spring和MyBatis完美整合 -->
- <beanidbeanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 指定数据源 -->
- <propertynamepropertyname="dataSource"ref="dataSource"/>
- <!-- 具体指定xml文件,可不配 -->
- <propertynamepropertyname="configLocation" value="classpath:mybatis-config.xml"/>
- <!-- 自动扫描mapping.xml文件,**表示迭代查找 ,,也可在mybatis-config.xml中单独指定xml文件 -->
- <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>
- </bean>
- <!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象 -->
- <beanclassbeanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!--
- basePackage 属性是映射器接口文件的包路径。
- 你可以使用分号或逗号 作为分隔符设置多于一个的包路径
- -->
- <propertynamepropertyname="basePackage"value="com/hys/app/**/dao"/>
- <!--
- 因为会自动装配 SqlSessionFactory和SqlSessionTemplate
- 所以没 有 必 要 去 指 定 SqlSessionFactory或 SqlSessionTemplate
- 因此可省略不配置;
- 但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。
- 这种 情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用;
- -->
- <propertynamepropertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
- </bean>
- <!-- 事务管理器
- <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <propertynamepropertyname="dataSource" ref="dataSource" />
- </bean>
- -->
- <!-- 使用声明式事务
- <tx:annotation-driventransaction-managertx:annotation-driventransaction-manager="txManager" />
- -->
- </beans>
2>jdbc.properties外置文件
3>mybatis-config.xml
4>studentMapper.xml
5>pom.xml
- <projectxmlnsprojectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>hys.web.project</groupId>
- <artifactId>hys_demo_ssm</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>hys_demo_ssm</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
- <!-- 数据源 -->
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.0.8</version>
- </dependency>
- <!-- Mybatis3.4.1 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.1</version>
- </dependency>
- <!-- spring整合mybatis -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.3.0</version>
- </dependency>
- <!-- Spring-4.2.0 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>4.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>4.2.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>4.2.0.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
建立表:
CREATETABLE t_app_student
( idVARCHAR(32) NOT NULL PRIMARY KEY,
name VARCHAR(15),
sex VARCHAR(2),
age INT
)
2、创建action、service、dao、entity类
1>实体类Student
- packagecom.hys.app.student.entity;
- public class Student {
- private Stringid;
- private Stringname;
- private Stringsex;
- private int age;
- publicStudent(){
- }
- publicStudent(Stringid, Stringname, Stringsex,intage) {
- this.id =id;
- this.name =name;
- this.sex =sex;
- this.age =age;
- }
- public StringgetId() {
- returnid;
- }
- public void setId(String id) {
- this.id =id;
- }
- public StringgetName() {
- returnname;
- }
- public void setName(String name) {
- this.name =name;
- }
- public StringgetSex() {
- returnsex;
- }
- public void setSex(String sex) {
- this.sex =sex;
- }
- public int getAge() {
- returnage;
- }
- public void setAge(intage) {
- this.age =age;
- }
- @Override
- public StringtoString() {
- return"Student [id=" +id +", name=" +name +", sex=" +sex
- + ", age=" + age + "]";
- }
- }
2>dao接口
3>service服务
4>action测试类
二、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数
整体结构如下图:
1、配置文件
applicationContext04.xml:
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:p="http://www.springframework.org/schema/p"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
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!-- 自动扫描 -->
- <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>
- <!-- 引入外置文件 -->
- <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertynamepropertyname="location"value="classpath:jdbc.properties"/>
- </bean>
- <!--数据库连接池配置-->
- <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>
- <propertynamepropertyname="url"value="${jdbc.url}"/>
- <propertynamepropertyname="username"value="${jdbc.username}"/>
- <propertynamepropertyname="password"value="${jdbc.password}"/>
- </bean>
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <beanidbeanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
- <propertynamepropertyname="dataSource"ref="dataSource"/>
- </bean>
- <!--
- 创建数据映射器,数据映射器必须为接口
- 这种配置方式有个缺点,有多少个dao接口就要配置多少个数据映射器,增加了开发时间
- 可用MapperScannerConfigurer代替,能够完全解决问题
- <bean id="studentMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">
- <propertynamepropertyname="mapperInterface"value="com.hys.app.student.dao.StudentDao" />
- <propertynamepropertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
- </bean>
- -->
- <!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象 -->
- <beanclassbeanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!--
- basePackage 属性是映射器接口文件的包路径。
- 你可以使用分号或逗号作为分隔符设置多于一个的包路径
- -->
- <propertynamepropertyname="basePackage"value="com/hys/app/**/dao"/>
- <!--
- 因为会自动装配 SqlSessionFactory和SqlSessionTemplate
- 所以没 有 必 要去 指 定 SqlSessionFactory或SqlSessionTemplate
- 因此可省略不配置;
- 但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。
- 这种 情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用;
- -->
- <propertynamepropertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
- </bean>
- <!-- 事务管理器
- <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- -->
- <!-- 使用声明式事务
- <tx:annotation-driven transaction-manager="txManager" />
- -->
- </beans>
2、创建action、service、dao、entity类
1>dao接口
2>service服务
三、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession
整体结构如下图:
1、配置文件
applicationContext03.xml:
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:p="http://www.springframework.org/schema/p"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
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!-- 自动扫描 -->
- <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>
- <!-- 引入外置文件 -->
- <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertynamepropertyname="location"value="classpath:jdbc.properties"/>
- </bean>
- <!--数据库连接池配置-->
- <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>
- <propertynamepropertyname="url"value="${jdbc.url}"/>
- <propertynamepropertyname="username"value="${jdbc.username}"/>
- <propertynamepropertyname="password"value="${jdbc.password}"/>
- </bean>
- <beannamebeanname="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
- <propertynamepropertyname="dataSource"ref="dataSource"/>
- <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>
- </bean>
- <!-- 将sqlSessionTemplate手工注入到SqlSessionDaoSupport中 -->
- <beanidbeanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-argindexconstructor-argindex="0"ref="sqlSessionFactory"/>
- </bean>
- </beans>
studentMapper.xml
2、创建action、service、dao、entity类
1>dao接口
2>dao接口实现类:
packagecom.hys.app.student.dao;
- importjavax.annotation.Resource;
- importorg.mybatis.spring.SqlSessionTemplate;
- importorg.mybatis.spring.support.SqlSessionDaoSupport;
- importorg.springframework.stereotype.Service;
- importcom.hys.app.student.entity.Student;
- @Service
- publicclass StudentDaoImp extends SqlSessionDaoSupport implements StudentDao{
- /**
- * 我们发现这个类中没有把SqlSessionTemplate作为一个属性,因为我们继承了SqlSessionDaoSupport
- *SqlSessionDaoSupport他会提供sqlsession
- */
- @Override
- public void save(Student student) {
- // TODO Auto-generated method stub
- this.getSqlSession().insert("com.hys.app.student.dao.StudentDao.save",student);
- }
- @Override
- public Student getStudent(String id) {
- // TODO Auto-generated method stub
- returnthis.getSqlSession().selectOne("com.hys.app.student.dao.StudentDao.getStudent",id);
- }
- /**
- * 使用SqlSessionDaoSupport必须注意,此处源码1.1.1中有自动注入,1.2中取消了自动注入,需要手工注入,侵入性强
- * 也可在spring-mybatis.xml中如下配置,但是这种有多少个dao就要配置到多少个,多个dao就很麻烦。
- * <bean id="userDao"class="com.hua.saf.dao.UserDao">
- * <propertynamepropertyname="sqlSessionFactory" ref="sqlSessionFactory"/>
- * </bean>
- */
- @Resource
- public voidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
- super.setSqlSessionTemplate(sqlSessionTemplate);
- }
- }
3>service服务
四、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate
整体结构如下图:
1、配置文件
applicationContext02.xml:
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:p="http://www.springframework.org/schema/p"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
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!-- 自动扫描 -->
- <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>
- <!-- 引入外置文件 -->
- <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertynamepropertyname="location"value="classpath:jdbc.properties"/>
- </bean>
- <!--数据库连接池配置-->
- <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>
- <propertynamepropertyname="url"value="${jdbc.url}"/>
- <propertynamepropertyname="username"value="${jdbc.username}"/>
- <propertynamepropertyname="password"value="${jdbc.password}"/>
- </bean>
- <beannamebeanname="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
- <propertynamepropertyname="dataSource"ref="dataSource"/>
- <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>
- </bean>
- <!--
- Mybatis-Spring为我们提供了一个实现了SqlSession接口的SqlSessionTemplate类,它是线程安全的,可以被多个Dao同时使用。
- 同时它还跟Spring的事务进行了关联,确保当前被使用的SqlSession是一个已经和Spring的事务进行绑定了的。
- 而且它还可以自己管理Session的提交和关闭。当使用了Spring的事务管理机制后,SqlSession还可以跟着Spring的事务一起提交和回滚。
- -->
- <beanidbeanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-argindexconstructor-argindex="0"ref="sqlSessionFactory"/>
- </bean>
- </beans>
studentMapper.xml
2、创建action、service、dao、entity类
1>dao接口
2>service服务
如果看不懂的话分享个地址:http://blog.csdn.net/xqhys/article/details/53994740 或者:http://www.cnblogs.com/wangmingshun/p/5674633.html