spring事务管理,基于xml配置完成事务回滚;spring中数据库表中字段名和pojo中属性名不一致时候,实现RowMapper接口手动封装

梦里梦外; 2022-04-18 05:29 176阅读 0赞

声明使用JDK8,spring5.0.7,

测试说明:

service 层 声明接口进行转账,从A转账B ,然后对AB 进行更新操作,在事务中对find方法开启 只读权限,无法进行更新操作,造成事务回滚进行测试事务;

主要测试方法:* void tranfer(Long fromId , Long toId ,Double money);


数据库如下:
d

子工程结构;
目录结构


pojo 实体;

  1. package com.baidu.domain;
  2. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:38 */
  3. public class Account {
  4. private Long id;
  5. private String NAME;
  6. private Double money;
  7. // setter tostring
  8. ......
  9. }
当数据库表中filed 名字和实体类中属性名不一致时候,需要手动映射table ;

实现RowMapper接口,重写mapRow方法,手动封装数据库中table数据;
把account表中数据进行封装, 封装成Account对象;*
->如下:

  1. package com.baidu.mapper;
  2. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:41 */
  3. public class AccountMapper implements RowMapper<Account> {
  4. // 把表中的数据 封装成 account 对象
  5. @Override
  6. public Account mapRow(ResultSet resultSet, int i) throws SQLException {
  7. Account account = new Account();
  8. account.setId(resultSet.getLong("id"));
  9. account.setNAME(resultSet.getString("NAME"));
  10. account.setMoney(resultSet.getDouble("money"));
  11. return account;
  12. }
  13. }

dao 层, daoImpl 层

  1. package com.baidu.dao;
  2. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:54 */
  3. public interface AccountDao {
  4. // 通过id 查询
  5. //通过update
  6. Account findById(Long id);
  7. void update(Account account);
  8. }
  9. ------------------------------------------------------------
  10. // daoImpl 层
  11. package com.baidu.daoImpl;
  12. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 9:57 */
  13. public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
  14. // 通过id 查询
  15. @Override
  16. public Account findById(Long id) {
  17. String sql="select * from account where id=?";
  18. Account account = null;
  19. try {
  20. account = this.getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), id);
  21. } catch (DataAccessException e) {
  22. e.printStackTrace();
  23. }
  24. return account;
  25. }
  26. //通过id update用户账户信息
  27. @Override
  28. public void update(Account account) {
  29. String sql="update account set NAME=?,money=? where id=?";
  30. this.getJdbcTemplate().update(sql, account.getNAME(), account.getMoney(),account.getId());
  31. }
  32. }

service层 ,以及serviceImpl 层;

  1. package com.baidu.service;
  2. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:05 */
  3. public interface AccountService {
  4. // 转账
  5. void transfer(Long fromId,Long toId,Double money);
  6. /** * 在事务属性中定义了 find* 只读 * @param id * @return */
  7. Account findById(Long id);
  8. }
  9. ----------------------------------------------------------
  10. // serviceImpl层
  11. package com.baidu.serviceImpl;
  12. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:07 */
  13. public class AccountServiceImpl implements AccountService {
  14. // 把dao 注入 提供setter方法
  15. private AccountDao accountDao ;
  16. public void setAccountDao(AccountDao accountDao) {
  17. this.accountDao = accountDao;
  18. }
  19. @Override
  20. public void transfer(Long fromId, Long toId, Double money) {
  21. Account fromAccount = accountDao.findById(fromId);
  22. Account toAccount = accountDao.findById(toId);
  23. //进行转账
  24. fromAccount.setMoney(fromAccount.getMoney()-money);
  25. toAccount.setMoney(toAccount.getMoney()+money);
  26. // 转账完成进行更新操作
  27. accountDao.update(fromAccount);
  28. accountDao.update(toAccount);
  29. }
  30. /** * 在事务属性中定义了 find* 只读 * @param id * @return */
  31. @Override
  32. public Account findById(Long id) {
  33. Account byId = accountDao.findById(id);
  34. // 进行update 操作,事务回滚
  35. byId.setMoney(10.0);
  36. accountDao.update(byId);
  37. return byId;
  38. }
  39. }

applicationContext.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  4. <bean id="accountService" class="com.baidu.serviceImpl.AccountServiceImpl">
  5. <property name="accountDao" ref="accountDao"></property>
  6. </bean>
  7. <bean id="accountDao" class="com.baidu.daoImpl.AccountDaoImpl">
  8. <property name="dataSource" ref="dataSource"></property>
  9. </bean>
  10. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  11. <property name="driverClassName" value="${jdbc.driverClass}"></property>
  12. <property name="url" value="${jdbc.url}"></property>
  13. <property name="username" value="${jdbc.username}"></property>
  14. <property name="password" value="${jdbc.password}"></property>
  15. </bean>
  16. <!-- 配置事务 transactionManager -->
  17. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  18. <!--向事务中注入源数据-->
  19. <property name="dataSource" ref="dataSource"></property>
  20. </bean>
  21. <!-- 配置 事务的属性 、find开头的方法只读事务,其他 -->
  22. <tx:advice id="interceptor" transaction-manager="transactionManager">
  23. <tx:attributes>
  24. <tx:method name="find*" read-only="true"/>
  25. <tx:method name="*"/>
  26. </tx:attributes>
  27. </tx:advice>
  28. <!--配置事务的切面-->
  29. <aop:config>
  30. <!-- 设置 pointcut 表达式,并且把定义好事务属性的应用到切入点 -->
  31. <aop:pointcut id="pt1" expression="execution(* com.baidu.serviceImpl.AccountServiceImpl.*(..))"></aop:pointcut>
  32. <aop:advisor advice-ref="interceptor" pointcut-ref="pt1"/>
  33. </aop:config>
  34. </beans>

测试;

  1. /** * @auther SyntacticSugar * @data 2018/11/8 0008下午 10:24 */
  2. @RunWith(SpringJUnit4ClassRunner.class)
  3. @ContextConfiguration("classpath:applicationContext.xml")
  4. public class TxTest {
  5. @Autowired
  6. private AccountService accountService;
  7. @Test
  8. public void test01(){
  9. accountService.transfer(1L,2L ,10.0 );
  10. }
  11. /** * 在事务属性中定义了 find* 只读 * 进行 update 事务会 回滚 */
  12. @Test
  13. public void test02(){
  14. Account byId = accountService.findById(1L);
  15. System.out.println(byId);
  16. }
  17. }

运行如下:
swe

发表评论

表情:
评论列表 (有 0 条评论,176人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring事务

    我们做项目的时候一般情况,事务扫描的是serviceimpl,很多增删改多的业务逻辑都要写在实现层中,但如果catch了异常没有抛出,那spring是不会回滚数据库的改动的