• Spring学习笔记:spring与mybatis四种整合方法


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

    1. <!-- 引入jdbc配置文件 -->  
    2.      <context:property-placeholder location="jdbc.properties"/>  
    3.       <!--创建jdbc数据源 -->  
    4.       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
    5.         <property name="driverClassName" value="${driver}"/>  
    6.         <property name="url" value="${url}"/>  
    7.         <property name="username" value="${username}"/>  
    8.         <property name="password" value="${password}"/>  
    9.         <property name="initialSize" value="${initialSize}"/>  
    10.         <property name="maxActive" value="${maxActive}"/>  
    11.         <property name="maxIdle" value="${maxIdle}"/>  
    12.         <property name="minIdle" value="${minIdle}"/>  
    13.       </bean>  
    14.       <!-- 创建SqlSessionFactory,同时指定数据源-->  
    15.       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    16.       <property name="dataSource" ref="dataSource" />   
    17.       </bean>  
    18.       <!--创建数据映射器,数据映射器必须为接口-->  
    19.       <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
    20.       <property name="mapperInterface" value="com.xxt.ibatis.dbcp.dao.UserMapper" />  
    21.       <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
    22.       </bean>  
    23.       <bean id="userDaoImpl2" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2">  
    24.       <property name="userMapper" ref="userMapper"/>  
    25.  </bean>  
    (2)数据映射器UserMapper,代码如下:
    1. public interface UserMapper {  
    2.       @Select("SELECT * FROM user WHERE id = #{userId}")   
    3.       User getUser(@Param("userId") long id);   
    4. }  
     (3) dao接口类UserDao,代码如下:
    1. public interface UserDao {  
    2.     public User getUserById(User user);  
    3. }  
    (4)dao实现类UserDaoImpl2,,代码如下:
    1. public class UserDaoImpl2 implements UserDao {  
    2.        private UserMapper userMapper;  
    3.        public void setUserMapper(UserMapper userMapper) {   
    4.            this.userMapper = userMapper;   
    5.        }   
    6.        public User getUserById(User user) {  
    7.           return userMapper.getUser(user.getId());   
    8.        }  
    9.    }  
     2、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate
        mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。MyBatis- Spring 中,使用了SqlSessionFactoryBean来替代。SqlSessionFactoryBean有一个必须属性 dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
       (1)Spring配置文件:
    1.   <!-- 创建SqlSessionFactory,同时指定数据源-->  
    2.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    3.       <property name="dataSource" ref="dataSource" />   
    4.       <!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->  
    5.       <property  name="configLocation"  value="classpath:sqlMapConfig.xml"/>  
    6.       <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation 有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->  
    7.       <!- - <property  name="mapperLocations"  value="classpath*:com/xxt/ibatis/dbcp/**/*.xml"/>  -->  
    8.    </bean>  
    9. <bean id="sqlSession"     class="org.mybatis.spring.SqlSessionTemplate">   
    10.       <constructor-arg index="0" ref="sqlSessionFactory" />   
    11. </bean>  
    12. <bean id="UserDaoImpl " class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl">  
    13.    <!--注入SqlSessionTemplate实例 -->  
    14.    <property name="sqlSessionTemplate" ref="sqlSession" />  
    15. -->  
    16. </bean>  
        (2)mybatis总配置文件sqlMapConfig.xml:
    1. <configuration>  
    2.   <typeAliases>  
    3.     <typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" />  
    4.  </typeAliases>  
    5.   <mappers>  
    6.      <mapper resource="com/xxt/ibatis/dbcp/domain/user.map.xml" />  
    7.     </mappers>  
    8. </configuration>  
    (3)实体类映射文件user.map.xml:
    1. <mapper namespace="com.xxt.ibatis.dbcp.domain.User">  
    2.      <resultMap type="User" id="userMap">  
    3.         <id property="id" column="id" />  
    4.         <result property="name" column="name" />  
    5.         <result property="password" column="password" />  
    6.         <result property="createTime" column="createtime" />  
    7.      </resultMap>  
    8.      <select id="getUser" parameterType="User" resultMap="userMap">  
    9.        select * from user where id = #{id}  
    10.      </select>  
    11. <mapper/>  
    (4)dao层接口实现类UserDaoImpl:
    1. public class UserDaoImpl implements  UserDao  {  
    2.    public SqlSessionTemplate sqlSession;  
    3.    public User getUserById(User user) {  
    4.        return (User)sqlSession.selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);  
    5.    }  
    6.    public void setSqlSession(SqlSessionTemplate sqlSession) {  
    7.         this.sqlSession = sqlSession;  
    8.    }  
    9.  }  
     3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
       (1)spring配置文件:
    1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    2.    <property name="dataSource" ref="dataSource" />  
    3.    <property  name="configLocation"  value="classpath:sqlMapConfig.xml"/>  
    4.    <!-- <property  name="mapperLocations"  value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/   >  -->  
    5. </bean>  
    6.  <bean id="sqlSession"     class="org.mybatis.spring.SqlSessionTemplate">   
    7.       <constructor-arg index="0" ref="sqlSessionFactory" />   
    8. </bean>  
    9. <bean id="userDaoImpl3" class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3">  
    10.    <!--注入SqlSessionTemplate实例 -->  
    11.    <property name="sqlSessionTemplate" ref="sqlSession" />   
    12.    <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->  
    13.    <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    14. -->  
    15. </bean>  
     (2) dao层接口实现类UserDaoImpl3:
    1. public class UserDaoImpl3 extends SqlSessionDaoSupport implements UserDao {     
    2.   public User getUserById(User user) {     
    3.      return (User) getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);    
    4.   }     
    5. }  
     4、采用org.mybatis.spring.mapper.MapperFactoryBean或者org.mybatis.spring.mapper.MapperScannerConfigurer
    1. <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    2.         <beans:property name="dataSource" ref="simpleDataSource" />  
    3.         <beans:property name="configLocation"  
    4.             value="classpath:conf/core/mybatis-config.xml" />  
    5.     </beans:bean>  
    6.   
    7.     <beans:bean id="sqlSessionFactory_contact" class="org.mybatis.spring.SqlSessionFactoryBean">  
    8.         <beans:property name="dataSource" ref="simpleDataSource_contact" />  
    9.         <beans:property name="configLocation"  
    10.             value="classpath:conf/core/mybatis-config-contact.xml" />  
    11.     </beans:bean>  
    12.   
    13.     <beans:bean id="transactionManager"  
    14.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    15.         <beans:property name="dataSource" ref="simpleDataSource" />  
    16.     </beans:bean>  
    17.   
    18.     <beans:bean id="transactionManager_contact"  
    19.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    20.         <beans:property name="dataSource" ref="simpleDataSource_contact" />  
    21.     </beans:bean>  
    22.       
    23.   
    24.     <!--  <tx:annotation-driven transaction-manager="transactionManager" />   
    25.     <tx:annotation-driven transaction-manager="transactionManager_contact" />  
    26.     -->     
    27. <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    28.         <property name="mapperInterface" value="com.mybatis.UserDao"></property>  
    29.         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
    30.     </bean>  
    31.   
    32.     <bean id="userService" class="com.mybatis.UserServiceImpl">  
    33.         <property name="userDao" ref="userDao"></property>  
    34.     </bean>  
    35.   
    36.     <!-- 加载配置文件 -->  
    37.     <beans:bean name="mapperScannerConfigurer"  
    38.         class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    39.         <beans:property name="basePackage" value="com.elong.hotel.crm.data.mapper" />  
    40.         <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></beans:property>  
    41.     </beans:bean>  
    42.     <beans:bean name="mapperScannerConfigurer_contact"  
    43.         class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    44.         <beans:property name="basePackage"  
    45.             value="com.elong.hotel.crm.data.contact.mapper" />  
    46.         <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_contact"></beans:property>  
    47.     </beans:bean>  
    48.   
    49.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    50.         <tx:attributes>  
    51.             <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->  
    52.             <tx:method name="save*" propagation="REQUIRED" />  
    53.             <tx:method name="insert*" propagation="REQUIRED" />  
    54.             <tx:method name="add*" propagation="REQUIRED" />  
    55.             <tx:method name="del*" />  
    56.             <tx:method name="update*" />  
    57.             <tx:method name="find*" read-only="true" />  
    58.             <tx:method name="get*" read-only="true" />  
    59.             <tx:method name="search*" read-only="true" />  
    60.         </tx:attributes>  
    61.     </tx:advice>  
    62.       
    63.     <tx:advice id="txAdvice_contact" transaction-manager="transactionManager_contact">  
    64.         <tx:attributes>  
    65.             <!-- name表示以什么开始的方法名,比如 add*表示add开头的方法 propagation表示事务传播属性,不写默认有 -->  
    66.             <tx:method name="save*" propagation="REQUIRED" />  
    67.             <tx:method name="insert*" propagation="REQUIRED" />  
    68.             <tx:method name="add*" propagation="REQUIRED" />  
    69.             <tx:method name="del*" />  
    70.             <tx:method name="update*" />  
    71.             <tx:method name="find*" read-only="true" />  
    72.             <tx:method name="get*" read-only="true" />  
    73.             <tx:method name="search*" read-only="true" />  
    74.         </tx:attributes>  
    75.     </tx:advice>  
    76.     <!-- 配置事务切面 -->  
    77.     <aop:config>  
    78.         <aop:pointcut expression="execution(* com.elong.hotel.crm.service..*.*(..))"  
    79.             id="pointcut" />  
    80.         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />  
    81.         <aop:advisor advice-ref="txAdvice_contact" pointcut-ref="pointcut" />  
    82.     </aop:config>  
  • 相关阅读:
    Python 学习日记 第七天
    Python 学习日记 第六天
    Python 学习日记 第五天
    Python 学习日记 第四天
    Redis 中的数据类型及基本操作
    Asp.net mvc 中View 的呈现(二)
    Asp.net mvc 中View的呈现(一)
    Asp.net mvc 中Action 方法的执行(三)
    Asp.net mvc 中Action 方法的执行(二)
    Asp.net mvc 中Action 方法的执行(一)
  • 原文地址:https://www.cnblogs.com/tengqiuyu/p/7705113.html
Copyright © 2020-2023  润新知