spring事务管理,基于xml配置完成事务回滚;spring中数据库表中字段名和pojo中属性名不一致时候,实现RowMapper接口手动封装
声明使用JDK8,spring5.0.7,
测试说明:
service 层 声明接口进行转账,从A转账B ,然后对AB 进行更新操作,在事务中对find方法开启 只读权限,无法进行更新操作,造成事务回滚进行测试事务;
主要测试方法:* void tranfer(Long fromId , Long toId ,Double money);
数据库如下:
子工程结构;
pojo 实体;
package com.baidu.domain;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:38 */
public class Account {
private Long id;
private String NAME;
private Double money;
// setter tostring
......
}
当数据库表中filed 名字和实体类中属性名不一致时候,需要手动映射table ;
实现RowMapper接口,重写mapRow方法,手动封装数据库中table数据;
把account表中数据进行封装, 封装成Account对象;*
->如下:
package com.baidu.mapper;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:41 */
public class AccountMapper implements RowMapper<Account> {
// 把表中的数据 封装成 account 对象
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getLong("id"));
account.setNAME(resultSet.getString("NAME"));
account.setMoney(resultSet.getDouble("money"));
return account;
}
}
dao 层, daoImpl 层
package com.baidu.dao;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:54 */
public interface AccountDao {
// 通过id 查询
//通过update
Account findById(Long id);
void update(Account account);
}
------------------------------------------------------------
// daoImpl 层
package com.baidu.daoImpl;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:57 */
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
// 通过id 查询
@Override
public Account findById(Long id) {
String sql="select * from account where id=?";
Account account = null;
try {
account = this.getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), id);
} catch (DataAccessException e) {
e.printStackTrace();
}
return account;
}
//通过id update用户账户信息
@Override
public void update(Account account) {
String sql="update account set NAME=?,money=? where id=?";
this.getJdbcTemplate().update(sql, account.getNAME(), account.getMoney(),account.getId());
}
}
service层 ,以及serviceImpl 层;
package com.baidu.service;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:05 */
public interface AccountService {
// 转账
void transfer(Long fromId,Long toId,Double money);
/** * 在事务属性中定义了 find* 只读 * @param id * @return */
Account findById(Long id);
}
----------------------------------------------------------
// serviceImpl层
package com.baidu.serviceImpl;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:07 */
public class AccountServiceImpl implements AccountService {
// 把dao 注入 提供setter方法
private AccountDao accountDao ;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(Long fromId, Long toId, Double money) {
Account fromAccount = accountDao.findById(fromId);
Account toAccount = accountDao.findById(toId);
//进行转账
fromAccount.setMoney(fromAccount.getMoney()-money);
toAccount.setMoney(toAccount.getMoney()+money);
// 转账完成进行更新操作
accountDao.update(fromAccount);
accountDao.update(toAccount);
}
/** * 在事务属性中定义了 find* 只读 * @param id * @return */
@Override
public Account findById(Long id) {
Account byId = accountDao.findById(id);
// 进行update 操作,事务回滚
byId.setMoney(10.0);
accountDao.update(byId);
return byId;
}
}
applicationContext.xml配置:
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="accountService" class="com.baidu.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.baidu.daoImpl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置事务 transactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--向事务中注入源数据-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 事务的属性 、find开头的方法只读事务,其他 -->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--配置事务的切面-->
<aop:config>
<!-- 设置 pointcut 表达式,并且把定义好事务属性的应用到切入点 -->
<aop:pointcut id="pt1" expression="execution(* com.baidu.serviceImpl.AccountServiceImpl.*(..))"></aop:pointcut>
<aop:advisor advice-ref="interceptor" pointcut-ref="pt1"/>
</aop:config>
</beans>
测试;
/** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:24 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TxTest {
@Autowired
private AccountService accountService;
@Test
public void test01(){
accountService.transfer(1L,2L ,10.0 );
}
/** * 在事务属性中定义了 find* 只读 * 进行 update 事务会 回滚 */
@Test
public void test02(){
Account byId = accountService.findById(1L);
System.out.println(byId);
}
}
运行如下:
还没有评论,来说两句吧...