spring-编程式事务管理

简介:
一、创建spring项目
    项目名称:spring101310
二、在项目上添加jar包
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加spring支持
        com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
        com.springsource.org.aopalliance-1.0.0.jar
        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        mysql-connector-java-5.1.18-bin.jar
        spring-aop-3.2.0.RELEASE.jar
        spring-aspects-3.2.0.RELEASE.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-jdbc-3.2.0.RELEASE.jar
        spring-tx-3.2.0.RELEASE.jar
三、在项目中添加配置文件与属性文件
    1.在项目中创建conf目录
    2.在conf目录下添加属性文件
        属性文件名称:jdbc.properties
        属性文件内容:
        jdbc.url=jdbc:mysql://localhost:3306/spring
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.username=root
        jdbc.password=root
    2.在conf目录下添加spring核心配置文件
        配置文件名称: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"
                       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">
                
                <!-- 1.加载属性文件 -->
                <context:property-placeholder location="classpath:jdbc.properties"/>
                
                <!-- 2.配置数据库连接池 -->
                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="jdbcUrl" value="${jdbc.url}"></property>
                    <property name="driverClass" value="${jdbc.driver}"></property>
                    <property name="user" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </bean>
        </beans>
四、实现bean设计
    1.在src目录下创建实体bean的包
        包名:cn.jbit.spring101310.domain
    2.在包下创建实体bean
        public class Account {
            private Integer id;
            private String name;
            private Double money;
            //省略get and set
        }
五、设计Dao层
    1.在src目录下创建dao层的包
        包名:cn.jbit.spring101310.dao
    2.在包下创建dao层的接口与实现类
        1)接口设计
            接口名称:IAccountDao.java
            接口内容:
            public interface IAccountDao {
                /**
                 * 转出
                 */
                public void outMoney(Account outaccount);
                /**
                 * 转入
                 */
                public void inMoney(Account inaccount);
            }
        2)接口实现类设计
            实现类名称:AccountDaoImpl.java
            实现类内容:
            public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
                /**
                 * 转入
                 */
                @Override
                public void inMoney(Account inaccount) {
                    String sql = "update account set money = money + ? where id = ?";
                    this.getJdbcTemplate().update(sql,inaccount.getMoney(),inaccount.getId());
                }
            
                /**
                 * 转出
                 */
                @Override
                public void outMoney(Account outaccount) {
                    String sql = "update account set money = money - ? where id = ?";
                    this.getJdbcTemplate().update(sql,outaccount.getMoney(),outaccount.getId());        
                }
            }
六、在核心配置文件中配置Dao
    <!-- 3.配置Dao -->
    <bean id="accountDao" class="cn.jbit.spring101310.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

七、在核心配置文件中配置事务相关信息
    <!-- 4.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.配置事务模板 -->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>
八、业务层设计
    1.在src目录下创建业务层的包
        包名:cn.jbit.spring101310.service
    2.在包下创建业务层的接口与实现类
        1)接口设计
            接口名称:AccountService.java
            接口内容:
            public interface AccountService {
                public void transfer(Account outAccount,Account inAccount);
            }
        2)接口实现类设计
            实现类名称:AccountServiceImpl.java
            实现类内容:
            public class AccountServiceImpl implements AccountService {
            
                private IAccountDao accountDao;
                private TransactionTemplate transactionTemplate;
                
                @Override
                public void transfer(final Account outAccount, final Account inAccount) {
                    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                        
                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                            //转出
                            accountDao.outMoney(outAccount);
                            int a = 1/0;
                            //转入
                            accountDao.inMoney(inAccount);
                        }
                    });
                }
                //省略get and set
            }
九、在核心配置文件中配置业务层
    <!-- 6.配置Service -->
    <bean id="accountService" class="cn.jbit.spring101310.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
        <property name="transactionTemplate" ref="transactionTemplate"></property>
    </bean>
十、测试
    1.在项目上创建test目录
        /test
    2.在test目录下创建测试包
        包名:cn.jbit.spring101310.service
    3.在测试包下创建测试类
        测试类名:AccountServiceTest.java
        测试类的内容:
        public class AccountServiceTest {
            @Test
            public void testTranser(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                AccountService accountService = (AccountService) context.getBean("accountService");
                Account outAccount = new Account();
                outAccount.setId(1);
                outAccount.setMoney(500D);
                Account inAccount = new Account();
                inAccount.setId(2);
                inAccount.setMoney(500D);
                accountService.transfer(outAccount, inAccount);
            }

        }


本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1563389

相关文章
|
24天前
|
XML 监控 前端开发
Spring Boot中的WebFlux编程模型
Spring WebFlux 是 Spring Framework 5 引入的响应式编程模型,基于 Reactor 框架,支持非阻塞异步编程,适用于高并发和 I/O 密集型应用。本文介绍 WebFlux 的原理、优势及在 Spring Boot 中的应用,包括添加依赖、编写响应式控制器和服务层实现。WebFlux 提供高性能、快速响应和资源节省等优点,适合现代 Web 应用开发。
75 15
|
28天前
|
人工智能 Java API
阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手
本次分享的主题是阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手,由阿里云两位工程师分享。
阿里云工程师跟通义灵码结伴编程, 用Spring AI Alibaba来开发 AI 答疑助手
|
2月前
|
Java Spring
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
一键注入 Spring 成员变量,顺序编程
|
3月前
|
XML Java 数据库连接
Spring高手之路25——深入解析事务管理的切面本质
本篇文章将带你深入解析Spring事务管理的切面本质,通过AOP手动实现 @Transactional 基本功能,并探讨PlatformTransactionManager的设计和事务拦截器TransactionInterceptor的工作原理,结合时序图详细展示事务管理流程,最后引导分析 @Transactional 的代理机制源码,帮助你全面掌握Spring事务管理。
51 2
Spring高手之路25——深入解析事务管理的切面本质
|
4月前
|
Java Spring 容器
Spring IOC、AOP与事务管理底层原理及源码解析
【10月更文挑战第1天】Spring框架以其强大的控制反转(IOC)和面向切面编程(AOP)功能,成为Java企业级开发中的首选框架。本文将深入探讨Spring IOC和AOP的底层原理,并通过源码解析来揭示其实现机制。同时,我们还将探讨Spring事务管理的核心原理,并给出相应的源码示例。
173 9
|
4月前
|
Java 数据库连接 Spring
【2021Spring编程实战笔记】Spring开发分享~(下)
【2021Spring编程实战笔记】Spring开发分享~(下)
43 1
|
4月前
|
XML Java 数据库连接
【2020Spring编程实战笔记】Spring开发分享~(上)
【2020Spring编程实战笔记】Spring开发分享~
66 0
|
6月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
5月前
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
|
6月前
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式