Spring 声明式事务管理----基于注解方式
来源:互联网 发布:ca检测网络检测未通过 编辑:程序博客网 时间:2024/06/05 11:08
使用案例代码:
#数据模型层
#配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 引用外部文件 db.properties读取数据库配置--> <context:property-placeholder location="classpath:db.properties"/> <!-- schemaLocation后面两个命名空间是扫描该包必须有的 --> <!-- 扫描com.sunline包以及所有子包,为所有加了注解的类创建bean --> <context:component-scan base-package="com.sunline"> </context:component-scan><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"value="${driverClassName}"></property><property name="url"value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="dialect"> org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">true</prop> <prop key="hibernate.show_sql">true</prop> </props></property><property name="mappingResources"><list><value>com/sunline/entity/Account.hbm.xml</value></list></property> </bean> <!-- 配置Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- ==================================4.使用注解配置声明式事务============================================ --><!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>
#数据访问层
AccountDao.java
package com.sunline.dao;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;@Repository(value="accountDao")public class AccountDao extends HibernateDaoSupport {/* * 使用注解必须添加以下方式 */ @Resource public void setSessionFacotry(SessionFactory sessionFacotry) { super.setSessionFactory(sessionFacotry); } /* * @param out :转出账号 * @param money :转账金额 */public void outMoney(String out, double money){String hsql ="update Account set money = money-? where name = ?";System.out.println("成功转出金额!");this.getHibernateTemplate().bulkUpdate(hsql, new Object[]{money,out});}/* * @param in:转入账号 * @param money:转账金额 */public void inMoney(String in, Double money) {String hsql = "update Account set money = money+? where name = ?";System.out.println("成功转入金额!");this.getHibernateTemplate().bulkUpdate(hsql, new Object[]{money,in});}}#业务逻辑层
AccountBiz4.java
package com.sunline.biz;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.sunline.dao.AccountDao;/* *@Transactional中的的属性 *propagation:事务的传播行为 *isolation:事务的隔离级别 *readOnly:只读 *rollbackFor:发生哪些异常回滚 *noRollbackFor:发生哪些异常不回滚 *rollbackForClassName 根据异常类名回滚 */@Transactional@Service(value="accountBizFour")public class AccountBiz4 {/* * 转账 */@Autowired@Qualifier("accountDao") //使用@Qualifier注解来说明使用哪一个实现类 AccountDao accountDao;/* * @param out:转出账号 * @param in:转入账号 * @param money:转账金额 */public void transfer(String out, String in, Double money) {accountDao.outMoney(out, money);int i = 1/0;accountDao.inMoney(in, money);}}
#测试类
package com.sunline.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.sunline.biz.AccountBiz4;public class TestFour {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountBiz4 accountBiz4 = (AccountBiz4) ctx.getBean("accountBizFour"); accountBiz4.transfer("海哥", "杨旭", 200d);}}
阅读全文
0 0
- Spring 声明式事务管理----基于注解方式
- Spring声明式事务管理(基于注解的方式)
- 基于@Transactional注解方式的spring声明式事务管理
- Spring声明式事务管理(基于注解方式实现)
- spring声明式事务管理:基于注解的方式
- spring声明式事务管理(基于注解)
- spring 声明式事务管理注解方式实现
- Spring 4.16 声明式事务管理方式--注解
- Spring事务管理(注解式声明事务管理)
- Spring事务管理(注解式声明事务管理)
- Spring事务管理(注解式声明事务管理)
- Spring声明式事务管理(XML与注解方式)
- spring中注解方式实现声明式事务管理
- Spring 基于注解方式声明式事务
- Spring声明式事务管理(基于xml的方式)
- spring声明式事务管理(基于TransactionProxyFactoryBean的方式)
- spring声明式事务管理(基于AspectJ的xml方式)
- Spring声明式事务管理(基于XML方式实现)
- PAT --- 1008. 数组元素循环右移问题 (20)
- leetcode
- SpringAop实现原理 --- 动态代理、cglib
- iOS NSRegularExpression 正则表达式的编写和使用
- LTE帧结构简介
- Spring 声明式事务管理----基于注解方式
- 正则表达式
- 2016ACM/ICPC亚洲区沈阳站 C Recursive sequence (矩阵快速幂)
- js事件
- tensorflow入门(简单神经网络实现)
- 程序启动时自动选择独显
- java学习笔记(1)-基础知识记录
- 深度学习在推断阶段的硬件实现方法概述
- Python 多进程默认不能共享全局变量