使用案例代码:
#数据模型层
package com.sunline.entity;
/**
* Account entity. @author MyEclipse Persistence Tools
*/
public class Account implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Double money;
// Constructors
/** default constructor */
public Account() {
}
/** minimal constructor */
public Account(String name) {
this.name = name;
}
/** full constructor */
public Account(String name, Double money) {
this.name = name;
this.money = money;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return this.money;
}
public void setMoney(Double money) {
this.money = money;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.sunline.entity.Account" table="account" catalog="association">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="money" type="java.lang.Double">
<column name="money" precision="10" />
</property>
</class>
</hibernate-mapping>
#配置文件
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: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>
<!-- ==================================2.使用XML配置声明式的事务管理(原始方式)=============================================== -->
<!-- 配置业务层的代理 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置目标对象 -->
<property name="target" ref="accountBizTwo" />
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 注入事务的属性 -->
<property name="transactionAttributes">
<props>
<!--
prop的格式:
* PROPAGATION :事务的传播行为
* ISOTATION :事务的隔离级别
* readOnly :只读
* -EXCEPTION :发生哪些异常回滚事务
* +EXCEPTION :发生哪些异常不回滚事务
-->
<prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop>
<!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> -->
<!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> -->
</props>
</property>
</bean>
</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});
}
}
#业务逻辑层
AccountBiz2.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 com.sunline.dao.AccountDao;
@Service(value="accountBizTwo")
public class AccountBiz2 {
/*
* 转账
*/
@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.AccountBiz2;
public class TestTwo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
/*
* 一定要注入代理类:因为代理类进行增强的操作
*/
AccountBiz2 accountBiz2 = (AccountBiz2) ctx.getBean("accountServiceProxy");
accountBiz2.transfer("海哥", "杨旭", 200d);
}
}