Spring (8)-declarative transaction

1 review business

Affairs is very important in the process of project development, and it involves data consistency issues.
Transaction management is a must-have technology in enterprise-level application development to ensure data integrity and consistency.
A transaction is to treat a series of actions as an independent unit of work. These actions are either all completed or all do not work.
The four attributes of the transaction ACID
atomicity (atomicity)
transaction is an atomic operation, composed of a series of actions, the atomicity of the transaction ensures that the actions are either completely completed or completely ineffective.
Consistency
Once all transaction actions are completed, the transaction is To be submitted. Data and resources are in a consistent state that satisfies business rules.
Isolation
may be that multiple transactions process the same data at the same time. Therefore, each transaction should be isolated from other transactions to prevent data damage.
Durability )
Once the transaction is completed, no matter what error occurs in the system, the result will not be affected. Under normal circumstances, the result of the transaction is written to persistent storage

1 test

In the previous case, we added two methods to the userMapper interface, delete and add users

package com.zs.mapper;

import com.zs.pojo.User;
import java.util.List;

public interface UserMapper {
    
    
    public List<User> selectUser();
    //添加一个用户
    int addUser(User user);

    //根据id删除用户
    int deleteUser(int id);
}

mapper file, we deliberately write deletes wrong, test

<?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.zs.mapper.UserMapper">

    <select id="selectUser" resultType="user">
        select * from mybatis_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>

Write the implementation class of the interface, in the implementation class, we go to operate a wave

package com.zs.mapper;

import com.zs.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    
    
    @Override
    public List<User> selectUser() {
    
    
        User user = new User(4,"zk","zk");

        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        mapper.deleteUser(4);
        return mapper.selectUser();
    }

    @Override
    public int addUser(User user) {
    
    
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.addUser(user);
        return res;
    }

    @Override
    public int deleteUser(int id) {
    
    
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.deleteUser(id);
        return res;
    }
}

test

import com.zs.mapper.UserMapper;
import com.zs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext
                        ("applicationContext.xml");
        UserMapper userMapper =
                (UserMapper) context.getBean("userMapper");
        List<User> users = userMapper.selectUser();
        System.out.println(users);

    }
}

Report an error: SQL is abnormal, delete is wrong.
Result: Insert successfully!
There is no management of affairs; we want them to succeed before they succeed, and if there is one failure, all fail, we should need affairs!
In the past, we all needed to manage affairs manually, which was very troublesome!
But Spring provides us with transaction management, we only need to configure

2 Transaction Management in Spring

Declarative transaction management: AOP
programmatic transaction management: embed transaction management code into business methods to control the commit and rollback
of transactions. Declarative transaction management is
generally better than programmatic transactions
. Separate transaction management code from business methods Come out and implement transaction management in a declarative way
. Treat transaction management as a cross-cutting concern and modularize it through the aop method. Spring supports declarative transaction management xml file configuration through the Spring AOP framework

<!--    配置声明事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--    结合aop实现事务的切入-->
<!--    配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--        给哪些方法配置事务-->
<!--        配置事务的传播特性-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

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

Why do you need to configure transactions?
If it is not configured, we need to manually submit the control transaction; the
transaction is very important in the project development process, and it involves the problem of data consistency and should not be sloppy

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/113060843