Spring和Mybatis的集成

搭建

  1. 需要spring和mybatis的集成包 mybatis-spring-1.2.2.jar

  2. 在spring容器中加載sqlSessionFactory類 org.mybatis.spring.SqlSessionFactoryBean
        a.需要加載mybatis的配置文件 configLocation 字符串注入 mybatis配置文件地址
        b.需要加載數據源 ref注入第三方數據源(自帶的數據源一般用於測試環境不用於生產環境)
        c.配置別名 typeAliasesPackage 注入包的字符串

  3. 第三放數據源 druid,c3p0,dbcp…每種數據源都有自己注入的屬性
    一般加載第三方數據源需要從配置文件中加載連接的數據

  4. 加載數據庫的配置文件 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
    (spring提供的外部屬性加載的類,此類在整個容器中只能有一個,先加載的先用)

  5. 配置spring的事務
         註解式spring事務
            a.用spring自帶的事務類 org.springframework.jdbc.datasource.DataSourceTransactionManager (需要注入數據源)
            b.打開spring事務註解支持 tx:annotation-driven 設置spring的事務爲默認的事務 transaction-manager指定成spring的事務
            c.在service層需要增加事務的方法上加@Transactional 當前方法就支持事務

        aop配置service層事務
            a.用spring自帶的事務類 org.springframework.jdbc.datasource.DataSourceTransactionManager
            需要注入數據源
            b.aop聲明式事務使用的是around通知
            配置切入點,配置通知 aop:advisor 指向tx:advice
            配置tx:advice 攔截響應地 方法
            事務的傳播屬性propagation(瞭解)
                    REQUIRED 支持當前事務,如果B沒有事務,新建一個事務
                    SUPPORTS 支持當前事務,如果B沒有事務,以非事務方法執行
                    MANDATORY 支持當前事務,如果B沒有事務,直接拋出異常
                    REQUIRED_NEW 新建事務,如果A有事務,A會先掛起,執行B
                    NOT_SUPPORTED 非事務運行,如果A 有事務,A掛起,B不帶事務
                    NEVER 只有有事務就出現異常
        兩種方式的優缺點
            1.註解式事務,配置簡單,使用靈活,aop配置配置繁瑣,而且方法必須要按規範命名
            2.註解式事務不能統一給service配置事務,aop配置能統一給業務層加事務

示例

applicationContext.xml

<?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">

    <!--掃描service層-->
    <context:component-scan base-package="com.igeek.service"></context:component-scan>

    <!--引用外部配置文件-->
    <!--<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>-->
    <bean id="propertiesConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"></property>
    </bean>

    <!--配置第三方數據源 druid數據源-->
    <bean id="dataSource-mysql" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>



    <!--加載sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加載mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--加載第三方的數據源-->
        <property name="dataSource" ref="dataSource-mysql"></property>
        <!--別名-->
        <property name="typeAliasesPackage" value="com.igeek.entity"></property>
    </bean>

    <!--
        通過mybatis和spring集成的類實現
        1.創建dao層的實現類
        2.並且把這個類放到spring容器中去
    -->
    <bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--掃描mapper並且註冊-->
        <property name="basePackage" value="com.igeek.dao"></property>
    </bean>

    <!--配置事務-->
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource-mysql"></property>
    </bean>

    <!--aop式事務管理-->
    <!--<aop:config>
        &lt;!&ndash;聲明切入點&ndash;&gt;
        <aop:pointcut id="txPoint" expression="execution(* com.igeek.service.*.*(..))"/>
        &lt;!&ndash;聲明通知&ndash;&gt;
        &lt;!&ndash;advice-ref通知的配置&ndash;&gt;
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
    </aop:config>-->

    <!--spring事務的通知配置-->
    <!--<tx:advice id="txAdvice" transaction-manager="tx">
        <tx:attributes>
            &lt;!&ndash;對不同的方法都執行事務的通知&ndash;&gt;
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="do*" propagation="REQUIRED"/>
            &lt;!&ndash;......自定義方法的通知了&ndash;&gt;
        </tx:attributes>
    </tx:advice>-->


    <!--註解式配置事務-->
    <tx:annotation-driven transaction-manager="tx"></tx:annotation-driven>
</beans>

mybatis-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--相關的設置-->
    <settings>
        <!--mybatis log4j的日誌 -->
        <setting name="logImpl" value="LOG4J" />
    </settings>
</configuration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章