来源:自学PHP网 时间:2020-10-09 10:33 作者:小飞侠 阅读:次
[导读] Spring如何基于xml实现声明式事务控制...
今天带来Spring如何基于xml实现声明式事务控制教程详解 一、pom.xml 4.0.0org.exampleA02spring1.0-SNAPSHOTorg.springframeworkspring-context5.2.8.RELEASEorg.springframeworkspring-jdbc5.2.8.RELEASEorg.springframeworkspring-tx5.2.8.RELEASEorg.aspectjaspectjweaver1.9.6mysqlmysql-connector-java8.0.11org.projectlomboklombok1.18.12providedorg.springframeworkspring-test5.2.8.RELEASEtestjunitjunit4.13testorg.apache.maven.pluginsmaven-compiler-plugin1.81.8 二、spring的xml配置文件 三、实体类 package com.wuxi.beans; import lombok.Data; import java.io.Serializable; @Data public class Account implements Serializable { private Integer id; private String name; private Float money; } 四、dao 1、接口 package com.wuxi.daos; import com.wuxi.beans.Account; public interface AccountDao { Account findAccountById(Integer accountId); Account findAccountByName(String accountName); void updateAccount(Account account); } 2、实现类 package com.wuxi.daos.impl; import com.wuxi.beans.Account; import com.wuxi.daos.AccountDao; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public Account findAccountById(Integer accountId) { Listaccounts = getJdbcTemplate().query("select * from account where id = ?", new BeanPropertyRowMapper(Account.class), accountId); return accounts.isEmpty() ? null : accounts.get(0); } @Override public Account findAccountByName(String accountName) { Listaccounts = getJdbcTemplate().query("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), accountName); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } @Override public void updateAccount(Account account) { getJdbcTemplate().update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId()); } } 五、service 1、接口 package com.wuxi.services; import com.wuxi.beans.Account; public interface AccountService { Account findAccounById(Integer accountId); void transfer(String sourceName, String targetName, Float money); } 2、实现类 package com.wuxi.services.impl; import com.wuxi.beans.Account; import com.wuxi.daos.AccountDao; import com.wuxi.services.AccountService; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public Account findAccounById(Integer accountId) { return accountDao.findAccountById(accountId); } @Override public void transfer(String sourceName, String targetName, Float money) { Account source = accountDao.findAccountByName(sourceName); Account target = accountDao.findAccountByName(targetName); source.setMoney(source.getMoney() - money); target.setMoney(target.getMoney() + money); accountDao.updateAccount(source); int i = 1 / 0; accountDao.updateAccount(target); } } 六、测试 package com.wuxi.tests; import com.wuxi.services.AccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:application.xml") public class MySpringTest { @Autowired private AccountService as; @Test public void testTransfer() { as.transfer("aaa", "bbb", 100f); } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学php网。 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com