`
- 浏览:
60962 次
- 性别:
- 来自:
湖北
-
vo层
java 代码
- package org.show;
-
-
-
-
-
-
-
- @SuppressWarnings("serial")
- public class Account implements java.io.Serializable {
-
-
-
-
- private Integer accountid;
- private String accountname;
- private Integer money;
-
-
-
-
-
- public Account() {
- }
-
-
-
- public Account(String accountname, Integer money) {
- this.accountname = accountname;
- this.money = money;
- }
-
-
-
-
- public Integer getAccountid() {
- return this.accountid;
- }
-
- public void setAccountid(Integer accountid) {
- this.accountid = accountid;
- }
-
- public String getAccountname() {
- return this.accountname;
- }
-
- public void setAccountname(String accountname) {
- this.accountname = accountname;
- }
-
- public Integer getMoney() {
- return this.money;
- }
-
- public void setMoney(Integer money) {
- this.money = money;
- }
-
-
- }
DAO层
java 代码
- package org.show;
-
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
- public class AccountDAO extends HibernateDaoSupport {
-
- public void addMoney(Integer accountId,int money){
-
- Account account = (Account)getHibernateTemplate().get(Account.class,accountId);
- account.setMoney(account.getMoney() + money);
- getHibernateTemplate().saveOrUpdate(account);
- }
-
- public void subMoney(Integer accountId,int money){
-
- Account account = (Account)getHibernateTemplate().get(Account.class,accountId);
- account.setMoney(account.getMoney() - money);
- getHibernateTemplate().saveOrUpdate(account);
- }
- }
Service层
java 代码
- package org.show;
-
- public class AccountService {
-
- private AccountDAO accountDAO;
- public void setAccountDAO(AccountDAO _accountDAO){
-
- accountDAO = _accountDAO;
- }
- public void transfer(Integer fromAccountId, Integer toAccountId,int money){
-
- accountDAO.subMoney(fromAccountId, money);
- accountDAO.addMoney(toAccountId, money);
- }
- }
测试方法
java 代码
- package org.show;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
-
- public class Main {
-
-
-
-
- public static void main(String[] args) {
-
- ApplicationContext act = new FileSystemXmlApplicationContext("src/applicationContext.xml");
- AccountService accountService = (AccountService)act.getBean("accountService");
- try{
-
- accountService.transfer(1, 2, 1);
-
- }catch(Exception e){
-
- System.out.println("转帐失败!");
- }
- }
-
- }
Account.hbm.xml映射文件
xml 代码
- <!---->xml version="1.0"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
- <class name="org.show.Account" table="account" >
- <id name="accountid" type="java.lang.Integer">
- <column name="accountid" />
- <generator class="native" />
- id>
- <property name="accountname" type="java.lang.String">
- <column name="accountname" length="20" not-null="true" />
- property>
- <property name="money" type="java.lang.Integer">
- <column name="money" not-null="true" />
- property>
- class>
- hibernate-mapping>
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <!---->>
-
- <beans>
-
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
-
- <property name="driverClassName">
- <value>com.mysql.jdbc.Drivervalue>
- property>
-
- <property name="url">
- <value>jdbc:mysql://localhost:3306/thjvalue>
- property>
-
- <property name="username">
- <value>rootvalue>
- property>
-
- <property name="password">
- <value>1234value>
- 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="hibernate.show_sql">trueprop>
- props>
- property>
- <property name="mappingResources">
- <list>
- <value>org/show/Account.hbm.xmlvalue>
- list>
- property>
- bean>
-
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
-
- <property name="sessionFactory" ref="sessionFactory">property>
- bean>
-
- <bean id="accountDAO" class="org.show.AccountDAO">
- <property name="sessionFactory" ref="sessionFactory">property>
- bean> Spring配置文件
-
- <bean id="accountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
-
- <property name="transactionManager" ref="transactionManager">property>
-
- <property name="target">
-
- <bean class="org.show.AccountService">
- <property name="accountDAO" ref="accountDAO">property>
- bean>
- property>
-
- <property name="transactionAttributes">
- <props>
-
- <prop key="transfer">PROPAGATION_REQUIREDprop>
- props>
- property>
- bean>
- beans>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
MySql示例2:未使用事务进行转账.zip MySql示例2:未使用事务进行转账.zip MySql示例2:未使用事务进行转账.zip
MySql示例3:使用事务进行转账.zip MySql示例3:使用事务进行转账.zip MySql示例3:使用事务进行转账.zip
SQL Server事务银行转账,转账时间,卡号密码等,实现银行转账存储过程事务实现,jiandnayidong
mysql事务处理的意义 事务处理机制在程序开发过程中有着非常重要的作用,它可以使整个系统更加安全,例如...采用事务处理机制,一旦在转账过程中发生意外,则程序将回滚,不做任何处理。 MYSQL的事务处理主要有两种方法
银行转账,事务处理,触发器,银行转账,事务处理,触发器
通过转账案例讲解了三层和事务,以及容易出现问题的关键点
本文实例讲述了PHP+Mysql基于事务处理实现转账功能的方法。分享给大家供大家参考。具体如下: <?php header("Content-Type:text/html;charset=utf-8"); $mysqli=new mysqli("localhost","root","","test"); ...
主要介绍了php+Mysqli利用事务处理转账问题的方法,实例分析了php+mysqli处理事务的提交与回滚的技巧,需要的朋友可以参考下
本文实例讲述了php+Mysqli利用事务处理转账问题的方法。分享给大家供大家参考 <?php header(Content-type:text/html; charset=utf-8); $mysqli = new mysqli(localhost, root, 064319, php); $mysqli->set_...
简单编写了一个银行转帐的事务管理程序,建立数据库表的sql亦包含在压缩包中,由于上传的限制,jar包需要自己导入一下...如果转账成功,事务正常结束,只要在转账过程中有一个错误,事务出错,便回滚,帐号上的钱数不变
主要介绍了Android数据库中事务操作方法之银行转账,以具体的银行转账为例分析了Android数据库操作中事务的使用与回滚相关操作技巧,需要的朋友可以参考下
简单的银行转账,就是实现事务管理,要成功一起成功,要失败一起失败
本文实例讲述了PHP使用pdo实现事务处理操作。分享给大家供大家参考,具体如下: 使用事务的好处: 举个例子:银行用户A向用户B转账100元,这个操作被分为两个步骤: (1)A的账户余额被扣去100。 (2)B的账户余额增加...
提供支付宝开放平台的单笔转账到支付宝账户接口JAVA版本demo 望大家共同进步
模拟实现多线程处理银行的实时转账交易,代码完整,可以完美运行~
银行转账事务的小练习,简单的简单表述了事务的四大特性(原子性:要么全部完成,要么全部不完成;一致性:事务开始之前和事务结束以后,数据库的完整性没有被破坏;持久性:事务完成以后,该事务对数据库的更改持久保存在...