zoukankan      html  css  js  c++  java
  • SSM框架集成.中(供自己练习查阅用)

    mybatis框架SqlSessionFactory对象创建

    * 问题: UserMapper 代理对象如何创建?

    * 答 :使用 SqlSession 操作对象创建 !

    *

    * 问题 : SqlSession 对象如何创建?

    *

    * : SqlSessionFactory 工厂对象创建?

    *

    * 问题: SqlSessionFactory 对象如何创建

    *

    * 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>

     

     

     

  • 相关阅读:
    周4早上搜索引擎分析 crmim.com| MSCRM开发者之家
    Bat命令学习
    sqlserver日期函数
    ubunto应用软件
    sql for xml
    win7x64 连接oracle 客户端 vs 2010调试 提示“ORA12154: TNS: 无法解析指定的连接标识符 ”ORA06413 问题(转)
    CentOS Rsync服务端与Windows cwRsync客户端实现数据同步
    怎么引导2岁孩子洗手问题
    Libnfcinstallation
    Asterisk资料
  • 原文地址:https://www.cnblogs.com/zhangxiong-tianxiadiyi/p/11172162.html
Copyright © 2011-2022 走看看