(傲娇的白狐) 添加事务和注解方式注解

事务和注解方式注解

使用mybatis 方式 用bean注入的方式:

配置文件:

applieationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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
    http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="spring-dao.xml"/>

    <bean id="UsermapperImpl" class="com.zyk.mapper.UsermapperImpl">
        <property name="sqlSession" ref="sqlsessionTempLate"/>
    </bean>

    <!--配置动态代理 ,支持aop的注解-->
    <aop:aspectj-autoproxy/>
    <!--开启注解扫描-->
    <context:component-scan base-package="com.zyk"/>
    <!--配置jdbcTemplate模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSouce"/>
    </bean>




    <!--开启事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSouce"/>
    </bean>


    <!--2.开启事务注解配置-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


//  非注解方式使用下面两个

    <!--&lt;!&ndash;配置事务通知&ndash;&gt;-->
    <!--<tx:advice id="txadvice" transaction-manager="transactionManager">-->
        <!--<tx:attributes>-->
            <!--<tx:method name="*" propagation="REQUIRED"/>-->
        <!--</tx:attributes>-->
    <!--</tx:advice>-->



    <!--&lt;!&ndash;利用aop切入事务&ndash;&gt;-->
    <!--<aop:config>-->
        <!--<aop:pointcut id="aoptext" expression="execution(* com.zyk.mapper.*.*(..))"/>-->
        <!--<aop:advisor advice-ref="txadvice" pointcut-ref="aoptext"/>-->
    <!--</aop:config>-->



</beans>

spring-dao.xml   mybatis连接数据库

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">


    <!--连接数据库-->
    <bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/user?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>


    </bean>

    <bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSouce"/>

        <!--连接mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="com/zyk/mapper/*.xml"/>
    </bean>


    <bean id="sqlsessionTempLate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlsessionfactory"/>
    </bean>
</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>

    <typeAliases>
        <package name="com.zyk.pojo"/>
    </typeAliases>
</configuration>

在需要的  xxxxxImpl 里进行添加一个 错误 进行测试  转账测试

UsermapperImpl


private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}


@Override @Transactional
//注解式事务 public void updatecom(Bili bili) {

//
Usermapper  是mapper类
Usermapper mapper = sqlSession.getMapper(Usermapper.class); // updatecom(bili); 
//  减少
mapper.updatecom(bili);
bili.setOrderId(
1002);

System.out.println(
1/0); // 特意写一个 错误看看事务不起作用
// 增加
this.uipdajia(bili);
}

最后结果 当启用事务时     执行减少方法 然后遇到自己定义的一个错误。项目报错,但并没有修改到数据库。注解方式一样可用

 

posted @ 2020-08-12 14:45  傲娇的白狐  阅读(220)  评论(0)    收藏  举报