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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章