spring-07

spring也到最后了,还剩下一个事务管理,之前几乎没接触过。熟悉一下

事务

就是将一系列业务看成一个业务,要么全部成功,要么全部失败

四个特性:(ACID)

原子性

一致性

隔离性

持久性

反正总的来说就是事物很重要,比如银行中,你把钱取走了,但是银行卡中的数字没变,就jj了

看一下测试的东西:

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{

    User user=new User(5,"小路","123456");
    @Override
    public List<User> selectUser() {
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        mapper.addUser(user);                        //调用了add方法
        mapper.deleteUser(5);                        //调用了delete方法
        return mapper.selectUser();
    }

    @Override
    public int addUser(User user) {
        return getSqlSession().getMapper(UserMapper.class).addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.mapper.UserMapper">
    <select id="selectUser" resultType="User">
        select * from user
    </select>

    <insert id="addUser" parameterType="User">
        insert into mybatis.user (id,name,pwd)
        values (#{id},#{name},#{pwd});
    </insert>

    <delete id="deleteUser" parameterType="int">
        deletes from mybatis.user where id=#{id}           //这里的方法被故意写错了
    </delete>
</mapper>

但是在测试时,依旧能够将数据插入,虽然会报delete的错

所以,我们需要保证数据的一致性

增加事务,这里使用aop横切进去,不会对源代码产生影响

要导入约束

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

开启 Spring 的事务处理功能(官网原话)

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <constructor-arg ref="dataSource" />
</bean>

结合aop织入

<!-- 结合aop实现事务的织入   -->
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法配置事务-->
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

配置事务切入

<!--配置事务切入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.kuang.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

在进行测试,在报错时,不会有任何一项成功进行

事务管理有两种:编程式事务管理、声明式事务管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
执行mvn clean install出现[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboot-schema 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ Downloading: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/2.2.2.RELEASE/spring-boot-maven-plugin-2.2.2.RELEASE.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.419 s [INFO] Finished at: 2023-07-22T19:52:51+08:00 [INFO] Final Memory: 9M/245M [INFO] ------------------------------------------------------------------------ [ERROR] Plugin org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.springframew ork.boot:spring-boot-maven-plugin:jar:2.2.2.RELEASE: Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:2.2.2.RELEASE from/to central (http://repo.maven. apache.org/maven2): Failed to transfer file: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/2.2.2.RELEASE/spring-boot-maven-plugin-2.2.2.RELEASE. pom. Return code is: 501 , ReasonPhrase:HTTPS Required. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException PS C:\Users\16283\Desktop\springbootc8hzm>
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值