Spring 声明式事务管理----基于注解方式

来源:互联网 发布:ca检测网络检测未通过 编辑:程序博客网 时间:2024/06/05 11:08

 使用案例代码:

#数据模型层

[java] view plain copy
  1. package com.sunline.entity;  
  2.   
  3. /** 
  4.  * Account entity. @author MyEclipse Persistence Tools 
  5.  */  
  6.   
  7. public class Account  implements java.io.Serializable {  
  8.   
  9.   
  10.     // Fields      
  11.   
  12.      private Integer id;  
  13.      private String name;  
  14.      private Double money;  
  15.   
  16.   
  17.     // Constructors  
  18.   
  19.     /** default constructor */  
  20.     public Account() {  
  21.     }  
  22.   
  23.     /** minimal constructor */  
  24.     public Account(String name) {  
  25.         this.name = name;  
  26.     }  
  27.       
  28.     /** full constructor */  
  29.     public Account(String name, Double money) {  
  30.         this.name = name;  
  31.         this.money = money;  
  32.     }  
  33.   
  34.      
  35.     // Property accessors  
  36.   
  37.     public Integer getId() {  
  38.         return this.id;  
  39.     }  
  40.       
  41.     public void setId(Integer id) {  
  42.         this.id = id;  
  43.     }  
  44.   
  45.     public String getName() {  
  46.         return this.name;  
  47.     }  
  48.       
  49.     public void setName(String name) {  
  50.         this.name = name;  
  51.     }  
  52.   
  53.     public Double getMoney() {  
  54.         return this.money;  
  55.     }  
  56.       
  57.     public void setMoney(Double money) {  
  58.         this.money = money;  
  59.     }  
  60.      
  61. }  

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.sunline.entity.Account" table="account" catalog="association">  
  9.         <id name="id" type="java.lang.Integer">  
  10.             <column name="id" />  
  11.             <generator class="native"></generator>  
  12.         </id>  
  13.         <property name="name" type="java.lang.String">  
  14.             <column name="name" length="20" not-null="true" />  
  15.         </property>  
  16.         <property name="money" type="java.lang.Double">  
  17.             <column name="money" precision="10" />  
  18.         </property>  
  19.     </class>  
  20. </hibernate-mapping>  

#配置文件

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
原创粉丝点击