Bank 实体类及映射文件
package cn.entity; import java.util.HashSet; import java.util.Set; public class Bank implements java.io.Serializable { private String cardId; private String userName; private Double money; private Set transInfos = new HashSet(0); public Bank() { } public Bank(String userName, Double money) { this.userName = userName; this.money = money; } public Bank(String userName, Double money, Set transInfos) { this.userName = userName; this.money = money; this.transInfos = transInfos; } public String getCardId() { return this.cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public Double getMoney() { return this.money; } public void setMoney(Double money) { this.money = money; } public Set getTransInfos() { return this.transInfos; } public void setTransInfos(Set transInfos) { this.transInfos = transInfos; } }
<?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="cn.entity.Bank" table="BANK" schema="BANKDB"> <id name="cardId" type="java.lang.String"> <column name="CARD_ID" length="50" /> <generator class="sequence"> <param name="sequence">seq_bank</param> </generator> </id> <property name="userName" type="java.lang.String"> <column name="USER_NAME" length="30" not-null="true" /> </property> <property name="money" type="java.lang.Double"> <column name="MONEY" precision="126" scale="0" not-null="true" /> </property> <set name="transInfos" inverse="true"> <key> <column name="TRANS_CARD_ID" length="50" not-null="true" /> </key> <one-to-many class="cn.entity.TransInfo" /> </set> </class> </hibernate-mapping>
TransInfo 实体类及映射文件
package cn.entity; import java.util.Date; public class TransInfo implements java.io.Serializable { private Long id; private Bank bank; private Double transMoney; private Date transDate; public TransInfo() { } public TransInfo(Bank bank, Double transMoney, Date transDate) { this.bank = bank; this.transMoney = transMoney; this.transDate = transDate; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public Bank getBank() { return this.bank; } public void setBank(Bank bank) { this.bank = bank; } public Double getTransMoney() { return this.transMoney; } public void setTransMoney(Double transMoney) { this.transMoney = transMoney; } public Date getTransDate() { return this.transDate; } public void setTransDate(Date transDate) { this.transDate = transDate; } }
<?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="cn.entity.TransInfo" table="TRANS_INFO" schema="BANKDB"> <id name="id" type="java.lang.Long"> <column name="ID" precision="10" scale="0" /> <generator class="sequence"> <param name="sequence">seq_trans_info</param> </generator> </id> <many-to-one name="bank" class="cn.entity.Bank" fetch="select"> <column name="TRANS_CARD_ID" length="50" not-null="true" /> </many-to-one> <property name="transMoney" type="java.lang.Double"> <column name="TRANS_MONEY" precision="126" scale="0" not-null="true" /> </property> <property name="transDate" type="java.util.Date"> <column name="TRANS_DATE" length="11" not-null="true" /> </property> </class> </hibernate-mapping>
BankDao 数据访问层接口及实现
package cn.dao; import cn.entity.Bank; /** * 账户 DAO * */ public interface BankDao { /** * 更新账户 * @param bank */ public void update(Bank bank); /** * 通过卡号到一个账户信息 * @param cardId * @return */ public Bank get(String cardId); }
package cn.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.dao.BankDao; import cn.entity.Bank; /** * 账户 DAO */ public class BankDaoImpl extends HibernateDaoSupport implements BankDao { public void update(Bank bank) { super.getHibernateTemplate().update(bank); } public Bank get(String cardId) { return super.getHibernateTemplate().get(Bank.class, cardId); } }
TransInfoDao 数据访问层接口及实现
package cn.dao; import cn.entity.TransInfo; /** * 交易信息 DAO * */ public interface TransInfoDao { /** * 添加一条交易信息 * @param info */ public void save(TransInfo info); }
package cn.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.dao.TransInfoDao; import cn.entity.TransInfo; public class TransInfoDaoImpl extends HibernateDaoSupport implements TransInfoDao { public void save(TransInfo info) { super.getHibernateTemplate().save(info); } }
BankBiz 业务逻辑层接口及实现
package cn.biz; /** * 银行业务类 */ public interface BankBiz { /** * 转帐的业务方法 * @param fromCardId 从哪个卡 * @param toCardId 转到哪个卡 * @param money 钱 */ public void transferAccount(String fromCardId,String toCardId,double money); /** * 存钱业务方法 * @param cardId 卡号 * @param money 钱 */ public void deposit(String cardId,double money); /** * 取钱业务方法 * @param cardId 卡号 * @param money 钱 */ public void withdraw(String cardId,double money); }
package cn.biz.impl; import java.util.Date; import cn.dao.BankDao; import cn.dao.TransInfoDao; import cn.entity.Bank; import cn.entity.TransInfo; import cn.biz.BankBiz; public class BankBizImpl implements BankBiz { private BankDao bankDao;//账户DAO private TransInfoDao transInfoDao;//交易DAO public void setBankDao(BankDao bankDao) { this.bankDao = bankDao; } public void setTransInfoDao(TransInfoDao transInfoDao) { this.transInfoDao = transInfoDao; } /** * 转账 */ public void transferAccount(String fromCardId, String toCardId, double money) { System.out.println("---转账操作---"); //一个账号存入钱 this.deposit(toCardId, money); //另一个账号取钱 this.withdraw(fromCardId, money); } /** * 存钱 */ public void deposit(String cardId, double money) { //通过 ID 得到账户信息 Bank bank =bankDao.get(cardId); bank.setMoney(bank.getMoney()+money); //更新了表 bankDao.update(bank); //往交易表记录流水账 TransInfo t =new TransInfo(); t.setBank(bank); t.setTransDate(new Date()); t.setTransMoney(money); //加入了一条记录 transInfoDao.save(t); System.out.println("卡号:"+cardId+"存入:"+money+"余额是:"+bank.getMoney()); } /** * 取钱 */ public void withdraw(String cardId, double money) { //通过 ID 得到账户信息 Bank bank=bankDao.get(cardId); bank.setMoney(bank.getMoney()-money); //更新了记录 bankDao.update(bank); //添加交易信息 TransInfo t=new TransInfo(); t.setBank(bank); t.setTransDate(new Date()); t.setTransMoney(-money); //添加了记录 transInfoDao.save(t); System.out.println("卡号:"+cardId+"取出:"+money+"余额是:"+bank.getMoney()); } }
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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"> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle11" /> <property name="username" value="bankDB" /> <property name="password" value="123456" /> </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.Oracle10gDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>cn/entity/Bank.hbm.xml</value> <value>cn/entity/TransInfo.hbm.xml</value> </list> </property> </bean> <!-- dao --> <bean id="bankDao" class="cn.dao.impl.BankDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transDao" class="cn.dao.impl.TransInfoDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 业务类同时注入2个 Dao --> <bean id="bankBiz" class="cn.biz.impl.BankBizImpl"> <property name="bankDao" ref="bankDao" /> <property name="transInfoDao" ref="transDao" /> </bean> <!-- 1.配置事物管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 2.事物规则 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 可以比较加上这句和删除这句转账的区别,默认为 REQUIRED,可省 --> <tx:method name="transferAccount" propagation="REQUIRED" /> <tx:method name="deposit" propagation="REQUIRED" /> <tx:method name="withdraw" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" propagation="SUPPORTS" /> <!-- 查询操作采用只读操作 --> <tx:method name="*" rollback-for="Exception" no-rollback-for="NullPointerException,ClassCastException" /> </tx:attributes> </tx:advice> <!-- 3.把业务规则和类关联起来 --> <aop:config> <aop:pointcut id="bankBizMethods" expression="execution(* cn.biz..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bankBizMethods" /> </aop:config> </beans>
Test 测试数据
package cn.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.biz.BankBiz; public class Test { public static void main(String[] args) { ApplicationContext tx =new ClassPathXmlApplicationContext("applicationContext.xml"); BankBiz bankBiz=(BankBiz) tx.getBean("bankBiz"); //存钱 //bankBiz.deposit("11111", 10000); //转账 //bankBiz.transferAccount("22222", "33333", 6000); //取钱 bankBiz.withdraw("11111", 12000); } }