- liliang_xf
- 等级: 初级会员
- 性别:
- 文章: 43
- 积分: 30
- 来自: 湖北
|
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>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|