domain
/**
* 賬戶的實體類
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
持久層
/**
* 賬戶的持久層接口
*/
public interface IAccountDao {
/**
* 根據Id查詢賬戶
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 根據名稱查詢賬戶
* @param accountName
* @return
*/
Account findAccountByName(String accountName);
/**
* 更新賬戶
* @param account
*/
void updateAccount(Account account);
}
=========================================================================================
/**
* 賬戶的持久層實現類
*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), accountId);
return accounts.isEmpty() ? null : accounts.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(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) {
jdbcTemplate.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
}
}
業務層
/**
* @Date 2019/8/2 - 17:16
* <p>
* 賬戶的業務層接口
*/
public interface IAccountService {
/**
* 根據id查找賬戶信息
*
* @param id
* @return
*/
Account findAccountById(Integer id);
/**
*
* @param sourceName 轉出賬戶名稱
* @param targetName 轉入賬戶名稱
* @param money 金額
*/
void transfer(String sourceName, String targetName, Float money);
}
=========================================================================================
/**
* 賬戶的業務層實現類
* <p>
* 事務控制應該都是在業務層
*/
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根據名稱查詢轉出賬戶
Account source = accountDao.findAccountByName(sourceName);
//2.2根據名稱查詢轉入賬戶
Account target = accountDao.findAccountByName(targetName);
//2.3轉出賬戶減錢
source.setMoney(source.getMoney() - money);
//2.4轉入賬戶加錢
target.setMoney(target.getMoney() + money);
//2.5更新轉出賬戶
accountDao.updateAccount(source);
//int i = 1 / 0;
//2.6更新轉入賬戶
accountDao.updateAccount(target);
}
}
配置類
/**
* @Date 2019/8/1 - 14:34
*
* 主配置類
*/
@Configuration
@ComponentScan("com.sx")
@Import({JdbcConfig.class, TransactionConfig.class})
@PropertySource("classpath:jdbc.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
=========================================================================================
/**
* 用與創建連接
*/
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 創建JdbcTemplate對象
*
* @param dataSource
* @return
*/
@Bean(name = "jdbcTemplate")
@Scope("prototype")
public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* 創建數據源
*
* @return
*/
@Bean(name = "dataSource")
public DataSource createDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try {
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
=========================================================================================
/**
* 事務相關的配置類
*/
public class TransactionConfig {
/**
* 用於創建事務管理器對象
*
* @param dataSource
* @return
*/
@Bean(name = "transactionManager")
public PlatformTransactionManager createPlatformTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
用於創建連接的配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy_spring
jdbc.username=root
jdbc.password=123456
測試類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testTransfer() {
as.transfer("aaa", "bbb", 100f);
}
}