zoukankan      html  css  js  c++  java
  • Spring和Mybatis的四种整合方式

    一、采用org.mybatis.spring.mapper.MapperScannerConfigurer

    整体结构如下图:

    1、配置文件

    1>applicationContext01.xml:

    1. xmlversionxmlversion="1.0"encoding="UTF-8"?>  
    2. <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"  
    3.   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"  
    4.   xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"  
    5.   xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.      http://www.springframework.org/schema/beans/spring-beans.xsd  
    7.     http://www.springframework.org/schema/aop  
    8.     http://www.springframework.org/schema/aop/spring-aop.xsd  
    9.     http://www.springframework.org/schema/context  
    10.     http://www.springframework.org/schema/context/spring-context.xsd  
    11.     http://www.springframework.org/schema/tx  
    12.     http://www.springframework.org/schema/tx/spring-tx.xsd  
    13.      http://www.springframework.org/schema/cache  
    14.     http://www.springframework.org/schema/cache/spring-cache.xsd">  
    15.       
    16.     <!-- 自动扫描 -->  
    17.     <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>  
    18.     <!-- 引入外置文件 -->  
    19.     <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    20.         <propertynamepropertyname="location"value="classpath:jdbc.properties"/>  
    21.     </bean>  
    22.      
    23.     <!--数据库连接池配置-->  
    24.     <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">    
    25.         <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>  
    26.         <propertynamepropertyname="url"value="${jdbc.url}"/>  
    27.         <propertynamepropertyname="username"value="${jdbc.username}"/>  
    28.         <propertynamepropertyname="password"value="${jdbc.password}"/>  
    29.     </bean>  
    30.    
    31.     <!-- spring和MyBatis完美整合 -->  
    32.     <beanidbeanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">  
    33.         <!-- 指定数据源 -->  
    34.         <propertynamepropertyname="dataSource"ref="dataSource"/>  
    35.         <!-- 具体指定xml文件,可不配 -->  
    36.         <propertynamepropertyname="configLocation" value="classpath:mybatis-config.xml"/>  
    37.         <!-- 自动扫描mapping.xml文件,**表示迭代查找 ,,也可在mybatis-config.xml中单独指定xml文件 -->  
    38.         <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>  
    39.     </bean>  
    40.    
    41.     <!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象 -->  
    42.     <beanclassbeanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    43.         <!--  
    44.         basePackage 属性是映射器接口文件的包路径。  
    45.                  你可以使用分号或逗号 作为分隔符设置多于一个的包路径  
    46.         -->  
    47.         <propertynamepropertyname="basePackage"value="com/hys/app/**/dao"/>  
    48.         <!--  
    49.                  因为会自动装配 SqlSessionFactory和SqlSessionTemplate  
    50.                  所以没 有 必 要 去 指 定 SqlSessionFactory或 SqlSessionTemplate  
    51.                  因此可省略不配置;  
    52.                  但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。  
    53.                  这种 情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用;  
    54.         -->  
    55.         <propertynamepropertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>  
    56.     </bean>  
    57.      
    58.     <!-- 事务管理器  
    59.     <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    60.         <propertynamepropertyname="dataSource" ref="dataSource" />  
    61.     </bean>    
    62.     -->  
    63.     <!-- 使用声明式事务   
    64.     <tx:annotation-driventransaction-managertx:annotation-driventransaction-manager="txManager" />  
    65.     -->  
    66. </beans>  

    2>jdbc.properties外置文件

    3>mybatis-config.xml

     

    4>studentMapper.xml

      

    5>pom.xml

     

    1. <projectxmlnsprojectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    3.   <modelVersion>4.0.0</modelVersion>  
    4.    
    5.   <groupId>hys.web.project</groupId>  
    6.   <artifactId>hys_demo_ssm</artifactId>  
    7.   <version>0.0.1-SNAPSHOT</version>  
    8.   <packaging>jar</packaging>  
    9.   <name>hys_demo_ssm</name>  
    10.   <url>http://maven.apache.org</url>  
    11.    
    12.   <properties>  
    13.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    14.   </properties>  
    15.    
    16.   <dependencies>  
    17.     <dependency>  
    18.       <groupId>junit</groupId>  
    19.       <artifactId>junit</artifactId>  
    20.       <version>4.10</version>  
    21.       <scope>test</scope>  
    22.     </dependency>  
    23.      
    24.     <!-- 数据源 -->  
    25.     <dependency>  
    26.         <groupId>commons-dbcp</groupId>  
    27.         <artifactId>commons-dbcp</artifactId>  
    28.         <version>1.4</version>  
    29.     </dependency>  
    30.      
    31.        <dependency>  
    32.        <groupId>mysql</groupId>  
    33.        <artifactId>mysql-connector-java</artifactId>  
    34.        <version>5.0.8</version>  
    35.     </dependency>  
    36.      
    37.     <!-- Mybatis3.4.1 -->  
    38.     <dependency>  
    39.         <groupId>org.mybatis</groupId>  
    40.         <artifactId>mybatis</artifactId>  
    41.         <version>3.4.1</version>  
    42.     </dependency>  
    43.      
    44.     <!-- spring整合mybatis -->  
    45.     <dependency>  
    46.         <groupId>org.mybatis</groupId>  
    47.         <artifactId>mybatis-spring</artifactId>  
    48.         <version>1.3.0</version>  
    49.     </dependency>  
    50.      
    51.     <!-- Spring-4.2.0 -->  
    52.     <dependency>  
    53.         <groupId>org.springframework</groupId>  
    54.         <artifactId>spring-web</artifactId>  
    55.         <version>4.2.0.RELEASE</version>  
    56.     </dependency>  
    57.      
    58.     <dependency>  
    59.         <groupId>org.springframework</groupId>  
    60.         <artifactId>spring-orm</artifactId>  
    61.         <version>4.2.0.RELEASE</version>  
    62.     </dependency>  
    63.      
    64.     <dependency>  
    65.         <groupId>org.springframework</groupId>  
    66.         <artifactId>spring-expression</artifactId>  
    67.         <version>4.2.0.RELEASE</version>  
    68.     </dependency>  
    69.      
    70.   </dependencies>  
    71. </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 

    1. packagecom.hys.app.student.entity;  
    2.    
    3. public class Student {  
    4.      
    5.     private Stringid;  
    6.     private Stringname;  
    7.     private Stringsex;  
    8.     private int age;  
    9.      
    10.     publicStudent(){  
    11.         
    12.     }    
    13.     publicStudent(Stringid, Stringname, Stringsex,intage) {  
    14.        this.id =id;  
    15.        this.name =name;  
    16.        this.sex =sex;  
    17.        this.age =age;  
    18.     }  
    19.     public StringgetId() {  
    20.        returnid;  
    21.     }  
    22.     public void setId(String id) {  
    23.        this.id =id;  
    24.     }  
    25.     public StringgetName() {  
    26.        returnname;  
    27.     }  
    28.     public void setName(String name) {  
    29.        this.name =name;  
    30.     }  
    31.     public StringgetSex() {  
    32.        returnsex;  
    33.     }  
    34.     public void setSex(String sex) {  
    35.        this.sex =sex;  
    36.     }  
    37.     public int getAge() {  
    38.        returnage;  
    39.     }  
    40.     public void setAge(intage) {  
    41.        this.age =age;  
    42.     }  
    43.     @Override  
    44.     public StringtoString() {  
    45.        return"Student [id=" +id +", name=" +name +", sex=" +sex  
    46.               + ", age=" + age + "]";  
    47.     }  
    48.      
    49. }  

    2>dao接口

    3>service服务

    4>action测试类

    二、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数

    整体结构如下图:

    1、配置文件

    applicationContext04.xml: 

    1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
    2. <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"  
    3.   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"  
    4.   xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"  
    5.   xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.      http://www.springframework.org/schema/beans/spring-beans.xsd  
    7.      http://www.springframework.org/schema/aop  
    8.     http://www.springframework.org/schema/aop/spring-aop.xsd  
    9.     http://www.springframework.org/schema/context  
    10.      http://www.springframework.org/schema/context/spring-context.xsd  
    11.      http://www.springframework.org/schema/tx  
    12.     http://www.springframework.org/schema/tx/spring-tx.xsd  
    13.     http://www.springframework.org/schema/cache  
    14.     http://www.springframework.org/schema/cache/spring-cache.xsd">  
    15.       
    16.     <!-- 自动扫描 -->  
    17.     <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>  
    18.     <!-- 引入外置文件 -->  
    19.     <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    20.        <propertynamepropertyname="location"value="classpath:jdbc.properties"/>  
    21.     </bean>  
    22.      
    23.     <!--数据库连接池配置-->  
    24.     <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">    
    25.        <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>  
    26.        <propertynamepropertyname="url"value="${jdbc.url}"/>  
    27.        <propertynamepropertyname="username"value="${jdbc.username}"/>  
    28.         <propertynamepropertyname="password"value="${jdbc.password}"/>  
    29.     </bean>  
    30.    
    31.     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    32.     <beanidbeanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">  
    33.        <propertynamepropertyname="dataSource"ref="dataSource"/>  
    34.     </bean>  
    35.    
    36.     <!--  
    37.     创建数据映射器,数据映射器必须为接口  
    38.     这种配置方式有个缺点,有多少个dao接口就要配置多少个数据映射器,增加了开发时间  
    39.     可用MapperScannerConfigurer代替,能够完全解决问题  
    40.    <bean id="studentMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">    
    41.         <propertynamepropertyname="mapperInterface"value="com.hys.app.student.dao.StudentDao" />   
    42.         <propertynamepropertyname="sqlSessionFactory"ref="sqlSessionFactory"/>  
    43.    </bean>   
    44.    -->   
    45.      
    46.     <!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象 -->  
    47.     <beanclassbeanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    48.        <!--  
    49.        basePackage 属性是映射器接口文件的包路径。  
    50.                  你可以使用分号或逗号作为分隔符设置多于一个的包路径  
    51.        -->  
    52.        <propertynamepropertyname="basePackage"value="com/hys/app/**/dao"/>  
    53.        <!--  
    54.                  因为会自动装配 SqlSessionFactory和SqlSessionTemplate  
    55.                  所以没 有 必 要去 指 定 SqlSessionFactory或SqlSessionTemplate  
    56.                  因此可省略不配置;  
    57.                  但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。  
    58.                  这种 情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用;  
    59.        -->  
    60.        <propertynamepropertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>  
    61.     </bean>  
    62.      
    63.     <!-- 事务管理器  
    64.    <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    65.        <property name="dataSource" ref="dataSource"/>  
    66.    </bean>    
    67.    -->  
    68.     <!-- 使用声明式事务   
    69.    <tx:annotation-driven transaction-manager="txManager" />  
    70.    -->  
    71. </beans>  

    2、创建action、service、dao、entity类

    1>dao接口

     

    2>service服务

     三、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession

    整体结构如下图:

    1、配置文件

    applicationContext03.xml:

     
    1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
    2. <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"  
    3.   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"  
    4.   xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"  
    5.   xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.      http://www.springframework.org/schema/beans/spring-beans.xsd  
    7.      http://www.springframework.org/schema/aop  
    8.     http://www.springframework.org/schema/aop/spring-aop.xsd  
    9.     http://www.springframework.org/schema/context  
    10.     http://www.springframework.org/schema/context/spring-context.xsd  
    11.     http://www.springframework.org/schema/tx  
    12.      http://www.springframework.org/schema/tx/spring-tx.xsd  
    13.     http://www.springframework.org/schema/cache  
    14.     http://www.springframework.org/schema/cache/spring-cache.xsd">  
    15.       
    16.     <!-- 自动扫描 -->  
    17.     <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>  
    18.     <!-- 引入外置文件 -->  
    19.     <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    20.         <propertynamepropertyname="location"value="classpath:jdbc.properties"/>  
    21.     </bean>  
    22.      
    23.     <!--数据库连接池配置-->  
    24.     <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">    
    25.        <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>  
    26.        <propertynamepropertyname="url"value="${jdbc.url}"/>  
    27.        <propertynamepropertyname="username"value="${jdbc.username}"/>  
    28.         <propertynamepropertyname="password"value="${jdbc.password}"/>  
    29.     </bean>  
    30.    
    31.     <beannamebeanname="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">  
    32.       <propertynamepropertyname="dataSource"ref="dataSource"/>  
    33.       <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>  
    34.     </bean>  
    35.      
    36.     <!-- 将sqlSessionTemplate手工注入到SqlSessionDaoSupport中 -->  
    37.     <beanidbeanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">  
    38.         <constructor-argindexconstructor-argindex="0"ref="sqlSessionFactory"/>  
    39.     </bean>  
    40.      
    41. </beans>  

    studentMapper.xml

     

    2、创建action、service、dao、entity类

    1>dao接口

    2>dao接口实现类:

    packagecom.hys.app.student.dao;  

    1. importjavax.annotation.Resource;  
    2. importorg.mybatis.spring.SqlSessionTemplate;  
    3. importorg.mybatis.spring.support.SqlSessionDaoSupport;  
    4. importorg.springframework.stereotype.Service;  
    5. importcom.hys.app.student.entity.Student;  
    6.    
    7. @Service  
    8. publicclass StudentDaoImp extends SqlSessionDaoSupport implements StudentDao{  
    9.      
    10.      /**  
    11.       * 我们发现这个类中没有把SqlSessionTemplate作为一个属性,因为我们继承了SqlSessionDaoSupport  
    12.       *SqlSessionDaoSupport他会提供sqlsession  
    13.      */  
    14.    
    15.     @Override  
    16.     public void save(Student student) {  
    17.        // TODO Auto-generated method stub  
    18.        this.getSqlSession().insert("com.hys.app.student.dao.StudentDao.save",student);  
    19.     }  
    20.    
    21.     @Override  
    22.     public Student getStudent(String id) {     
    23.        // TODO Auto-generated method stub  
    24.        returnthis.getSqlSession().selectOne("com.hys.app.student.dao.StudentDao.getStudent",id);  
    25.     }  
    26.    
    27.     /**  
    28.      * 使用SqlSessionDaoSupport必须注意,此处源码1.1.1中有自动注入,1.2中取消了自动注入,需要手工注入,侵入性强  
    29.      * 也可在spring-mybatis.xml中如下配置,但是这种有多少个dao就要配置到多少个,多个dao就很麻烦。  
    30.      * <bean id="userDao"class="com.hua.saf.dao.UserDao">  
    31.      * <propertynamepropertyname="sqlSessionFactory" ref="sqlSessionFactory"/>  
    32.      * </bean>  
    33.      */  
    34.     @Resource  
    35.     public voidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){  
    36.        super.setSqlSessionTemplate(sqlSessionTemplate);  
    37.     }  
    38.      
    39. }  

    3>service服务

     

    四、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate

    整体结构如下图:

    1、配置文件

    applicationContext02.xml:

     
    1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?>  
    2. <beansxmlns:xsibeansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"  
    3.   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"  
    4.   xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"  
    5.   xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.      http://www.springframework.org/schema/beans/spring-beans.xsd  
    7.     http://www.springframework.org/schema/aop  
    8.     http://www.springframework.org/schema/aop/spring-aop.xsd  
    9.     http://www.springframework.org/schema/context  
    10.     http://www.springframework.org/schema/context/spring-context.xsd  
    11.     http://www.springframework.org/schema/tx  
    12.     http://www.springframework.org/schema/tx/spring-tx.xsd  
    13.     http://www.springframework.org/schema/cache  
    14.     http://www.springframework.org/schema/cache/spring-cache.xsd">  
    15.       
    16.     <!-- 自动扫描 -->  
    17.     <context:component-scanbase-packagecontext:component-scanbase-package="com.hys.app.**.service,com.hys.app.**.dao,com.hys.app.**.action"/>  
    18.     <!-- 引入外置文件 -->  
    19.     <beanidbeanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    20.         <propertynamepropertyname="location"value="classpath:jdbc.properties"/>  
    21.     </bean>  
    22.      
    23.     <!--数据库连接池配置-->  
    24.     <beanidbeanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">    
    25.        <propertynamepropertyname="driverClassName"value="${jdbc.driverClassName}"/>  
    26.        <propertynamepropertyname="url"value="${jdbc.url}"/>  
    27.        <propertynamepropertyname="username"value="${jdbc.username}"/>  
    28.         <propertynamepropertyname="password"value="${jdbc.password}"/>  
    29.     </bean>  
    30.    
    31.     <beannamebeanname="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">  
    32.       <propertynamepropertyname="dataSource"ref="dataSource"/>  
    33.       <propertynamepropertyname="mapperLocations"value="classpath:com/hys/app/**/dao/*.xml"/>  
    34.     </bean>  
    35.      
    36.     <!--  
    37.        Mybatis-Spring为我们提供了一个实现了SqlSession接口的SqlSessionTemplate类,它是线程安全的,可以被多个Dao同时使用。  
    38.        同时它还跟Spring的事务进行了关联,确保当前被使用的SqlSession是一个已经和Spring的事务进行绑定了的。  
    39.        而且它还可以自己管理Session的提交和关闭。当使用了Spring的事务管理机制后,SqlSession还可以跟着Spring的事务一起提交和回滚。  
    40.     -->  
    41.     <beanidbeanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate">  
    42.         <constructor-argindexconstructor-argindex="0"ref="sqlSessionFactory"/>  
    43.     </bean>  
    44.        
    45. </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

  • 相关阅读:
    SQL中distinct的用法
    python requests 高级用法 -- 包括SSL 证书错误的解决方案
    odoo js
    线程池的理解及使用
    单点登录原理及简单实现
    如何重写hashCode()和equals()方法
    多线程中的Lock小结
    Sql语句的优化摘要
    Spring事务管理
    并发编程之原子性、可见性、有序性的简单理解
  • 原文地址:https://www.cnblogs.com/anzmri/p/7716206.html
Copyright © 2011-2022 走看看