标签:pac ntb driver framework float name ntc return HERE
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置业务层-->
<bean id="accountService" class="com.hope.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置持久层bean-->
<bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easy"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!--配置事务-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--配置aop-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt" expression="execution(* com.hope.service.impl.*.*(..))"></aop:pointcut>
<!--建立切入点表达式和通知之间的关联-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
package com.hope.service.impl;
import com.hope.dao.IAccountDao;
import com.hope.domain.Account;
import com.hope.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author newcityman
* @date 2019/11/20 - 16:45
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
public void transfer(String sourceName, String targetName, Float money) {
Account source = accountDao.findAccountByName(sourceName);
Account targe = accountDao.findAccountByName(targetName);
source.setMoney(source.getMoney() - money);
targe.setMoney(targe.getMoney() + money);
accountDao.updateAccount(source);
int i = 1 / 0;
accountDao.updateAccount(targe);
}
}
package com.hope.dao.impl;
import com.hope.dao.IAccountDao;
import com.hope.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
/**
* @author newcityman
* @date 2019/11/23 - 23:38
*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{
public Account findAccountById(int id) {
List<Account> accounts =getJdbcTemplate().query("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class),id);
return (accounts==null)?null:accounts.get(0);
}
public Account findAccountByName(String username) {
List<Account> accounts = getJdbcTemplate().query("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class),username);
if(accounts==null){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果不唯一,无法定位唯一的结果");
}
return accounts.get(0);
}
public void updateAccount(Account account) {
getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
标签:pac ntb driver framework float name ntc return HERE
原文地址:https://www.cnblogs.com/newcityboy/p/11926981.html