spring---配置事物

两种方式

  声明式事物:aop

  编程式事物:就是在代码中手动添加事物

 

声明式事物

只需要在spring-dao.xml配置即可(注意需要假如相关约束)

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

<!--    导入外部jdbc的配置文件-->
    <context:property-placeholder location="classpath:jdbcConfig.properties" system-properties-mode="FALLBACK"></context:property-placeholder>
    <!--    1.DataSource,使用spring的配置源替换mybatis的配置-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--    2.创建sqlsessionfaction-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--        绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--        配置sql语句包的位置-->
        <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"></property>
    </bean>


    <!--    3.使用spring提供的SqlSessionTemplate,和sqlsession相同-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--        只能使用构造器注入,因为没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>


<!--    配置声明式事物-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

<!--    结合aop实现事物的植入-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
<!--        给哪些方法配置事物:

            属性propagation:默认为REQUIRED
            参数:REQUIRED
            表示支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

            属性:read-only;默认为false
            true:表示只能读信息,不能有其他操作
-->
            <tx:method name="addUser" propagation="REQUIRED"/>
            <tx:method name="selectUser" read-only="true"/>
            <tx:method name="deleteUser" propagation="REQUIRED"/>
<!--        给所有方法配置事物;这个配置了就不需要一个一个赔了
            <tx:method name="*" propagation="REQUIRED"/>
-->
        </tx:attributes>
    </tx:advice>
<!--    配置事物切入-->
    <aop:config>
<!--        切入点-->
        <aop:pointcut id="txPointCut" expression="execution(* com.kuang.mapper.*.*(..))"/>
<!--
        advisor:表示切入
        advice-ref:切入方法的id
        pointcut-ref:切入点的id
-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor>
    </aop:config>
</beans>

 

.....

 

posted @ 2021-11-27 10:49  江南0o0  阅读(44)  评论(0)    收藏  举报