SSM框架集成.中(供本身練習查閱用)

mybatis框架SqlSessionFactory對象建立spring

* 問題: UserMapper 代理對象如何建立?sql

* 答 :使用 SqlSession 操做對象建立 !數據庫

* express

* 問題 : SqlSession 對象如何建立?mybatis

* app

* : SqlSessionFactory 工廠對象建立?框架

* ui

* 問題: SqlSessionFactory 對象如何建立url

* spa

* 1,和Spring框架集成以前

* MyBatis框架本身讀取配置文件中的相關配置去建立

* 2, 和Spring框架集成以後

* 交個Spring容器來建立

* 問題: 如何在Spring框架中配置,建立出來SqlSessionFactory對象?

* mybatisspring集成的類查閱 橋樑包

* org.mybatis.spring.SqlSessionFactoryBean 建立 SqlSessionFactory

*

*/

 

 

建立MyBatis框架工廠對象的 類在mybatis-spring1.2.1.jar 橋樑包中的

org.mybatis.spring.SqlSessionFactoryBean類 以下圖

 

 

 

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">


    <!-- 配置讀取 db.properties 數據庫配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置數據源鏈接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
    </bean>
    
    
    <!-- 
        配置MyBatis框架的 SqlSessionFactoryBean 類,建立
        
         SqlSessionFactory 工廠對象
     -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 1.注入數據源 -->
        <property name="dataSource" ref="dataSource"/>
        
        
        <!-- 2.配置映射文件 -->
        <property name="mapperLocations">
            <array>
                <!-- <value>classpath:cn/zj/mybatis/mapper/UserMapper.xml</value> -->
                <!-- 能夠使用通配符 * 讀取 目錄下面全部的配置文件 -->
                <value>classpath:cn/zj/mybatis/mapper/*Mapper.xml</value>
            </array>
        </property>
<!-- 3. 配置別名使用包掃描 -->
        <property name="typeAliasesPackage" value="cn.zj.mybatis.pojo"/>
        
        <!-- 4.讀取mybat-config.xml配置文件,此配置文件可能還會配一些mybatis框架的
            其餘個性化配置
            實際項目開發可能不用配置
         -->
         <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
</beans>

建立mybatis的mapper接口代理對象

使用橋樑包 org.mybatis.spring.mapper.MapperFactoryBean<T> 建立 UserMapper代理對象

此種方式每個Mapper接口須要單獨配置,若是Mapper過多,建立Mapper可能形成配置代碼過多

<!-- 建立UserMapper代理對象-建立單個Mapper對象
使用橋樑包 org.mybatis.spring.mapper.MapperFactoryBean<T> 建立 UserMapper代理對象
      -->
      
      <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
              
              <!-- 注入SqlSessionFacotry對象 -->
              <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
              
              <!-- 注入UserMapper接口類型:底層建立UserMapper的代理對象 -->
              <property name="mapperInterface" value="cn.zj.mybatis.mapper.UserMapper"/>
      
      </bean>

試用包掃描建立mybatis的mapper接口代理對象

<!-- 批量建立Mapper代理對象 ,使用包掃描建立Mapper代理對象
  使用橋樑包
  org.mybatis.spring.mapper.MapperScannerConfigurer
  -->
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <!-- 配置須要建立Mapper接口代理對象對應的包 -->
          <property name="basePackage" value="cn.zj.mybatis.mapper"/>
          
          <!-- 配置SqlSessionFactoryBean 的名稱,不是引用 -->
          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  </bean>

 

mybatis事務管理器配置

通常開發,事務的管理都會使用aop切入到業務層

 <!-- 配置事務管理器 -->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <!-- 注入數據源 -->
          <property name="dataSource" ref="dataSource"/>
      </bean>
      
      <!-- spring事務配置 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
          
          <!-- 事務屬性配置 -->
          <tx:attributes>
              <!-- DQL :查詢操做,配置只讀事務 -->
              <tx:method name="get*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>
              <tx:method name="select*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>
              <tx:method name="find*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>
              <tx:method name="query*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>
              
              <!-- 其餘 SQL :非只讀事務 -->
              <tx:method name="*" read-only="false" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>
              
          </tx:attributes>
          
      </tx:advice>
      
      <!-- 配置AOP 切入事務 -->
      
      <aop:config>
          <!-- 切入點 -->
          <aop:pointcut expression="execution(* cn.zj.mybatis.service..*.*(..))" id="pt"/>
          
          <!-- 切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
      </aop:config>
相關文章
相關標籤/搜索