Spring 事务配置类,完成数据库的转账

1、完成基本的三层架构

1.1创建Account表

创建实体类 Account

1.2 Service层写入 AccountService 接口

Service层 下写 impl 包定义 AccountServiceImpl 类 实现接口 AccountService

java 复制代码
@Service
@Transactional
@RequiredArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final AccountDao accountDao;

    //    @Transactional 事务注解可以写再方法上,也可以写类上
    @Override
    public boolean transfer(String from, String to, double money) {
        //获取转入账号信息
        Account toAccount = accountDao.getAccountByAccount(to);
        if (toAccount == null) {
            System.out.println("账号不存在");
            return false;
        }
        if (toAccount.getStatus() == 1) {
            System.out.println("被禁用");
            return false;
        }
        //获取转出账号信息
        Account fromAccount = accountDao.getAccountByAccount(from);
        if (fromAccount.getMoney() <= money) {
            System.out.println("转出账号余额不足");
            return false;
        }
        //实现转账
        accountDao.addMoney(to, money);
        accountDao.subMoney(from, money);
        System.out.println("转账成功");
        return true;
    }
}

1.3 dao层写入 AccountDao 接口

dao层 下写 impl 包定义 AccountDaoImpl 类 实现接口 AccountDao

java 复制代码
@Repository
@RequiredArgsConstructor
public class AccountDaoImpl implements AccountDao {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public Account getAccountByAccount(String account) {
        String sql = "select * from account where account=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class),account);
    }

    @Override
    public int addMoney(String account, double money) {
        String sql = "update account set money=money+? where account=?";
        return jdbcTemplate.update(sql,money,account);
    }

    @Override
    public int subMoney(String account, double money) {
        //        int i=1/0;
        String sql = "update account set money=money-? where account=?";
        return jdbcTemplate.update(sql,money,account);
    }
}

2、事务配置类

2.1跟dao层同级创建config包,config包下创建SpringConfig类

java 复制代码
//@Configuration 该类式一个配置类,相当于一个applicationContext.xml配置文件
@Configuration
//@ComponentScan 表示组件扫描(会扫描basePackages包下(包含子包)所有注解)
//相当于配置文件中得<context:component-scan/>
@ComponentScan(basePackages = "com.cxd")
//@PropertySource 表示properties配置文件得资源路径
//相当于 <context:property-placeholder location="classpath*:database.properties"/>
@PropertySource(value = "classpath:database.properties")

//@EnableTransactionManagement 开启事务管理
//相当于配置文件中得 <tx:annotation-driven/> 事务管理驱动
@EnableTransactionManagement

public class SpringConfig {

    //@Value写在属性上,表示读取配置文件中值注入到属性中
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    /**
     * @Bean 写在配置类中得方法上 相当于配置文件中得 <bean></bean>标签
     * 其中得返回子就是<bean class=""></bean> class属性
     * bean在容器中得名字就是方法名
     * 数据源
     */
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource =
        new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    //事务管理器
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);

    }

}

2.2数据库的连接

3、测试类

java 复制代码
//@ContextConfiguration(locations = "classpath:applicationContext.xml")
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test01 {

    @Autowired
    AccountDao accountDao;

    @Autowired
    AccountService accountService;

  
    @Test
    public void test02() {
        accountService.transfer("888888","666666",100);
    }

}

最后测试类,查看数据库数据

4、用到的jar包

java 复制代码
@Repository
@RequiredArgsConstructor
public class AccountDaoImpl implements AccountDao {

    private final JdbcTemplate jdbcTemplate;

    @Override
    public Account getAccountByAccount(String account) {
        String sql = "select * from account where account=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class),account);
    }

    @Override
    public int addMoney(String account, double money) {
        String sql = "update account set money=money+? where account=?";
        return jdbcTemplate.update(sql,money,account);
    }

    @Override
    public int subMoney(String account, double money) {
        //        int i=1/0;
        String sql = "update account set money=money-? where account=?";
        return jdbcTemplate.update(sql,money,account);
    }
}
相关推荐
KmSH8umpK几秒前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第六篇
数据库·redis·分布式
bzmK1DTbd几秒前
SOLID原则在Java中的实践:单一职责与开闭原则
java·开发语言·开闭原则
AI进化营-智能译站4 分钟前
ROS2 C++开发系列07-高效构建机器人决策逻辑,运算符与控制流实战
开发语言·c++·ai·机器人
winner88815 分钟前
C++ 命名空间、虚函数、抽象类、protected 权限全套通俗易懂精讲(附与 Java 对比)
java·开发语言·c++
不会编程的懒洋洋15 分钟前
C# P/Invoke 基础
开发语言·c++·笔记·安全·机器学习·c#·p/invoke
直奔標竿16 分钟前
Java开发者AI转型第二十五课!Spring AI 个人知识库实战(四)——RAG来源追溯落地,拒绝AI幻觉
java·开发语言·人工智能·spring boot·后端·spring
梁萌22 分钟前
mysql使用事件做日志表数据转移
数据库·mysql
时空系23 分钟前
认识Rust——我的第一个程序 Rust中文编程
开发语言·后端·rust
lThE ANDE24 分钟前
MySQL中的TRUNCATE TABLE命令
数据库·mysql
yqcoder25 分钟前
JavaScript 柯里化:把“大餐”拆成“小炒”的艺术
开发语言·javascript·ecmascript