package com.learn.dao;public interface AccountDao {//加钱void increaseMoney(Integer id,Double money);//减钱void decreaseMoney(Integer id,Double money);
}
package com.learn.dao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {@Overridepublic void increaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);}@Overridepublic void decreaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);}}
package com.learn.service;public interface AccountService {//转账方法void transfer(Integer from,Integer to,Double money);}
package com.learn.service;import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.learn.dao.AccountDao;@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {private AccountDao ad ;@Overridepublic void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);//加钱ad.increaseMoney(to, money);}public void setAd(AccountDao ad) {this.ad = ad;}}
package com.learn.tx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);}
}

package com.learn.service;import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.learn.dao.AccountDao;@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {private AccountDao ad ;@Overridepublic void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}public void setAd(AccountDao ad) {this.ad = ad;}}
package com.learn.tx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);}
}

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!