今天带来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);
}
}
查看更多关于Spring如何基于xml实现声明式事务控制的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did18341