美文网首页
Spring事务的实现

Spring事务的实现

作者: 凡哥爱丽姐 | 来源:发表于2020-10-25 12:55 被阅读0次

    1、添加jar包

     <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46</version>
            </dependency>
    
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
    
    
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.13</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    
        </dependencies>
    

    2、User实体类

    package com.fan.entity;
    
    public class User {
        private int id;
        private String name;
        private String sex;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }
    
    

    3、UserDao类和UserDaoImpl类

    package com.fan.dao;
    
    import com.fan.entity.User;
    
    public interface UserDao {
        //更新数据库中的数据
        public int update(User user);
    }
    
    package com.fan.dao.impl;
    
    import com.fan.dao.UserDao;
    import com.fan.entity.User;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
    
        public int update(User user) {
            String sql="update users set name=?,sex=? where id=?";
            int update = getJdbcTemplate().update(sql, user.getName(), user.getSex(), user.getId());
            System.out.println(5/0);
            return update;
        }
    }
    

    4、UserService类和UserServiceImpl类

    package com.fan.service;
    
    import com.fan.entity.User;
    
    public interface UserService {
        //更新数据库中的数据
        public int update(User user);
    }
    
    package com.fan.service.impl;
    
    import com.fan.dao.UserDao;
    import com.fan.entity.User;
    import com.fan.service.UserService;
    
    public class UserServiceImpl implements UserService {
        private UserDao userDao;
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public int update(User user) {
          //userDao里面报错,那么根据事务的原子性,数据更新就不会成功,回滚
           return userDao.update(user);
        }
    }
    

    5、applicationContext.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:tx="http://www.springframework.org/schema/tx"
           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/context
                               http://www.springframework.org/schema/context/spring-context.xsd
                                http://www.springframework.org/schema/aop
                               http://www.springframework.org/schema/aop/spring-aop.xsd
                               http://www.springframework.org/schema/tx
                               http://www.springframework.org/schema/tx/spring-tx.xsd
    ">
    
    
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <!--1、指定数据源-->
        <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driver}"></property>
            <property name="jdbcUrl" value="${url}"></property>
            <property name="user" value="${uname}"></property>
            <property name="password" value="${password}"></property>
        </bean>
        <!--2、创建jdbcTemplate-->
        <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg index="0" ref="datasource"></constructor-arg>
        </bean>
    
        <!--3、创建dao层对象-->
        <bean id="dao1" class="com.fan.dao.impl.UserDaoImpl">
            <property name="jdbcTemplate" ref="template"></property>
        </bean>
        <!--3、创建service层对象-->
        <bean id="service" class="com.fan.service.impl.UserServiceImpl">
            <property name="userDao" ref="dao1"></property>
        </bean>
    
    
        <!-- 添加声明式事务-->
        <!--1、创建事务管理器对象-->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="datasource"/>
        </bean>
    
        <!-- 2、定义哪些方法需要事务管理,添加tx命名空间 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为:propagation  不是必须的,默认值是REQUIRED  -->
                <!-- REQUIRED:如果有事务,则在事务中执行;如果没有事务,则开启一个新的事务 -->
                <!-- SUPPORTS:如果有事务,则在事务中执行;如果没有事务,则不会开启事务 -->
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
    
        <!--将接口和方法绑定在一起,一般与service层接口绑定-->
        <aop:config>
             <!--切入点-->
            <aop:pointcut id="txPointCut" expression="execution(* com.fan.service.*.*(..))" />
        
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
        </aop:config>
    </beans>
    

    6、测试类

    import com.fan.entity.User;
    import com.fan.service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test1 {
        public static void main(String[] args) {
            ApplicationContext applicationContext=
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService =(UserService)applicationContext.getBean("service");
            User user=new User();
            user.setId(2);
            user.setName("aaa");
            user.setSex("男");
            int update = userService.update(user);
            System.out.println(update);
    
        }
    }
    
        因为5/0是错误运算,所以这里面不能实现数据库的更改

    7、注解方式添加事务

    applicationContext.xml配置
        <!--1、创建事务管理器对象-->
        <bean id="tx"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="datasource"/>
        </bean>
    
        <!--① 对标注@Transactional注解的Bean进行加工处理,以织入事物管理切面 -->
        <tx:annotation-driven transaction-manager="tx" />
    
    在需要添加事务的类或方法上添加该注解
    @Transactional(propagation = Propagation.REQUIRED)
    

    相关文章

      网友评论

          本文标题:Spring事务的实现

          本文链接:https://www.haomeiwen.com/subject/rotsahtx.html