Spring- declarative things (XI)

Affairs

Transaction: either all succeed! Either do not succeed!

Four characteristics of the transaction: ACID: atomicity, consistency, isolation, durability.

Declarative transaction

Spring supports both transaction processing mechanism:

Programmatic transaction: write the code for all transactions in the business;

Declarative transaction: Using AOP cross into; usually declarative transaction

To enable Spring transaction processing, create a Spring configuration file dataSourceTransactionManager objects:

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

Transactions with Spring notification, we need to import constraints file: tx

Transaction propagation level:

Propagation :

  key property agent should determine which method to increase transactional behavior. This property is the most important part of communication behavior. PROPAGATION_REQUIRED- support the current transaction, if no transaction, we create a new business. This is the most common choice. 

Spring configuration of things


 1. leader packet AOP

  <!--aop织入包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>

2. The business logic

   public List<User> selectAllUser() {
        UserDao mapper = sqlSession.getMapper(UserDao.class);
       User user = new User(3, "maomao", "123321");
       mapper.add(user);
       mapper.delete(5);

        return mapper.selectAllUser();

    }

3.mapper mapping file

<?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="org.west.dao.UserDao">

    <select id="selectAllUser" resultType="org.west.pojo.User">
        select *from user
    </select>

    <insert id="add" parameterType="org.west.pojo.User">
        insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd})
</insert>

<delete id="delete" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>

</mapper>

4. Transaction Manager configuration parameters requires a data source;

    <! - need to configure an object manager -> 
< the bean ID = "the transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > 
    < constructor-Arg REF = "the dataSource" /> 
</ the bean >

5. Configure declarative transaction notice

<! - Configure claims notice something -> 
    < tx: advice the above mentioned id = "txAdvice" Transaction-Manager = "transactionManager" > 
        < tx: the Attributes > 
            <! - name: refers to those issued to add transaction 
            propagation: Transaction level, generally choose this REQUIRED, if there is a transaction 
            to use this transaction, no transaction creates a transaction -> 
            < tx: Method, name = "the Add" propagation = "REQUIRED" /> 
            < tx: Method, name = "the Delete" propagation = "REQUIRED" /> 
        </ TX: Attributes > 
    </ TX:advice>

6. weaving configuration transaction aop

<aop:config>
        <!--切入点-->
        <aop:pointcut id="pointCut" expression="execution(* org.west.dao.UserDaoImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
    </aop:config>

7. Test class

public class Testya {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDaoImpl = (UserDao) context.getBean("userDaoImpl");
        List<User> users = userDaoImpl.selectAllUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

 

 

Guess you like

Origin www.cnblogs.com/xiaoqiqistudy/p/11306454.html