当前位置:   article > 正文

Spring5框架 JdbcTemplate 注解方式-对数据库操作事务_springframework jdbctemplate怎么注解

springframework jdbctemplate怎么注解

事务操作(事务概念)

1、什么事务

(1)事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操 作都失败

(2)典型场景:银行转账 * lucy 转账 100 元 给 mary * lucy 少 100,mary 多 100

2、事务四个特性(ACID)

(1)原子性

(2)一致性

(3)隔离性

(4)持久性

 

create table t_user(id int,username varchar(300),money double);

 

  1. @Service
  2. public class UserService {
  3. //注入 dao
  4. @Autowired
  5. private UserDao userDao;
  6. }
  7. @Repository
  8. public class UserDaoImpl implements UserDao {
  9. @Autowired
  10. private JdbcTemplate jdbcTemplate;
  11. }
  12. 3、在 dao 创建两个方法:多钱和少钱的方法,在 service 创建方法(转账的方法)
  13. @Repository
  14. public class UserDaoImpl implements UserDao {
  15. @Autowired
  16. private JdbcTemplate jdbcTemplate;
  17. //lucy 转账 100 给 mary
  18. //少钱
  19. @Override
  20. public void reduceMoney() {
  21. String sql = "update t_account set money=money-? where username=?";
  22. jdbcTemplate.update(sql,100,"lucy");
  23. }
  24. //多钱
  25. @Override
  26. public void addMoney() {
  27. String sql = "update t_account set money=money+? where username=?";
  28. jdbcTemplate.update(sql,100,"mary");
  29. }
  30. }
  31. @Service
  32. public class UserService {
  33. //注入 dao
  34. @Autowired
  35. private UserDao userDao;
  36. //转账的方法
  37. public void accountMoney() {
  38. //lucy 少 100
  39. userDao.reduceMoney();
  40. //mary 多 100
  41. userDao.addMoney();
  42. }
  43. }
  44. 4、上面代码,如果正常执行没有问题的,但是如果代码执行过程中出现异常,有问

编程时事务:一般不用这个原因为不灵活。

 

 事务操作(Spring 事务管理介绍)

1、事务添加到 JavaEE 三层结构里面 Service 层(业务逻辑层)

2、在 Spring 进行事务管理操作

(1)有两种方式:编程式事务管理和声明式事务管理(使用)

3、声明式事务管理

(1)基于注解方式(使用)

(2)基于 xml 配置文件方式

4、在 Spring 进行声明式事务管理,底层使用 AOP 原理

5、Spring 事务管理 API

(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类

 事务操作(生命式 事务管理 注解)

1.在sping框架中:配置xml文件配置事务管理器 

  1. <!-- 数据库连接池 -->
  2. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  3. destroy-method="close">
  4. <property name="url" value="jdbc:mysql://10.0.0.8:3306/test" />
  5. <property name="username" value="ecc_event_management" />
  6. <property name="password" value="94020212" />
  7. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  8. </bean>
  9. <!-- JdbcTemplate 对象 -->
  10. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  11. <!--注入 dataSource-->
  12. <property name="dataSource" ref="dataSource"></property>
  13. </bean>
  14. <!-- 组件扫描 -->
  15. <context:component-scan base-package="sun.jdbctemp"></context:component-scan>
  16. <!--配置数据库事务 此处为新增-->
  17. <!--创建事务管理器 此处为新增-->
  18. <bean id="transactionManager"
  19. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  20. <!--注入数据源-->
  21. <property name="dataSource" ref="dataSource"></property>
  22. </bean>
  23. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

2.配置sping配置文件,开启注解

(1)在sping配置文件中引入名称空间 tx

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.alibaba.com/schema/stat
  11. http://www.alibaba.com/schema/stat.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd">

(2)开启事务注解

  1. <!--开启事务注解 此处为新增-->
  2. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

(3)在service的类上面(获取service类里面的方法上)添加事务注解:

@Transactional事务注解

可以添加到类上面:表示类中的所有方法都有事务

也可以添加到方法上:表示这个方法添加事务

@Service
@Transactional
public class UserService

这里附上完整模拟转账的代码:

数据库数据的显示:

pojo层:

  1. package sun.jdbctemp.pojo;
  2. import org.springframework.stereotype.Repository;
  3. /**
  4. * @author sunyc
  5. * @create 2022-04-13 17:22
  6. */
  7. @Repository
  8. public class User {
  9. private int id;
  10. private String username;
  11. private double money;
  12. public User() {
  13. }
  14. public User(int id, String username, double money) {
  15. this.id = id;
  16. this.username = username;
  17. this.money = money;
  18. }
  19. public int getId() {
  20. return id;
  21. }
  22. public void setId(int id) {
  23. this.id = id;
  24. }
  25. public String getUsername() {
  26. return username;
  27. }
  28. public void setUsername(String username) {
  29. this.username = username;
  30. }
  31. public double getMoney() {
  32. return money;
  33. }
  34. public void setMoney(double money) {
  35. this.money = money;
  36. }
  37. }

dao 层 接口以及实现类:

  1. package sun.jdbctemp.dao;
  2. import org.springframework.stereotype.Repository;
  3. import sun.jdbctemp.pojo.User;
  4. /**
  5. * @author sunyc
  6. * @create 2022-04-13 17:25
  7. */
  8. @Repository
  9. public interface UserDao {
  10. //添加用户
  11. int addUser(User user);
  12. //增加收入
  13. int addMoney(String username,double money);
  14. //减少收入
  15. int reduceMoney(String username,double money);
  16. }
  17. ----------------------------------------------------------------------------
  18. 实现接口的类
  19. package sun.jdbctemp.dao;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.jdbc.core.JdbcTemplate;
  22. import org.springframework.stereotype.Repository;
  23. import sun.jdbctemp.pojo.User;
  24. /**
  25. * @author sunyc
  26. * @create 2022-04-13 17:27
  27. */
  28. @Repository
  29. public class UserDaoImpl implements UserDao{
  30. @Autowired
  31. private JdbcTemplate jdbcTemplate;
  32. @Override //添加用户
  33. public int addUser(User user) {
  34. String sql="insert into t_user values(?,?,?)";
  35. int update = jdbcTemplate.update(sql, user.getId(), user.getUsername(), user.getMoney());
  36. return update;
  37. }
  38. //加钱
  39. @Override
  40. public int addMoney(String username, double money) {
  41. String sql="update t_user set money=money+? where username=?";
  42. return jdbcTemplate.update(sql, money, username);
  43. }
  44. //减钱
  45. @Override
  46. public int reduceMoney(String username, double money) {
  47. String sql="update t_user set money=money-? where username=?";
  48. return jdbcTemplate.update(sql, money, username);
  49. }
  50. }

service层:

  1. package sun.jdbctemp.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Isolation;
  5. import org.springframework.transaction.annotation.Propagation;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import org.springframework.transaction.reactive.TransactionContextManager;
  8. import sun.jdbctemp.dao.UserDao;
  9. import sun.jdbctemp.pojo.User;
  10. /**
  11. * @author sunyc
  12. * @create 2022-04-13 17:31
  13. 3、在 service 类上面(或者 service 类里面方法上面)添加事务注解
  14. (1)@Transactional,这个注解添加到类上面,也可以添加方法上面
  15. (2)如果把这个注解添加类上面,这个类里面所有的方法都添加事务
  16. (3)如果把这个注解添加方法上面,为这个方法添加事务
  17. */
  18. @Service
  19. @Transactional
  20. public class UserService {
  21. @Autowired
  22. private UserDao userDao;
  23. //添加用户数据
  24. public void addUser(User user){
  25. System.out.println(userDao.addUser(user));
  26. }
  27. //加钱
  28. public void addMoney(String username,double money){
  29. userDao.addMoney(username,money);
  30. }
  31. //减钱
  32. public void reduceMoney(String username,double money){
  33. userDao.addMoney(username,money);
  34. }
  35. //赚钱 addMoneytname 为加钱 reduceMoneyname 减钱 money 金额
  36. public void accountMoney(String addMoneytname,String reduceMoneyname,double money){
  37. //给 addMoneytname用户加钱
  38. userDao.addMoney(addMoneytname,money);
  39. int a=10/0;
  40. //给 reduceMoneyname 用户-钱
  41. userDao.reduceMoney(reduceMoneyname,money);
  42. }
  43. }

测试类:

  1. package sun.jdbctemp.test;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import sun.jdbctemp.pojo.User;
  6. import sun.jdbctemp.service.UserService;
  7. import static org.junit.jupiter.api.Assertions.*;
  8. /**
  9. * @author sunyc
  10. * @create 2022-04-13 17:37
  11. */
  12. class UserServiceTest {
  13. @Test //初始化 --添加用户
  14. void addUser() {
  15. ApplicationContext context=new ClassPathXmlApplicationContext("mysqlbean.xml");
  16. UserService userService = context.getBean("userService", UserService.class);
  17. userService.addUser(new User(1,"sunchao",1000));
  18. userService.addUser(new User(2,"xiaoming",1000));
  19. }
  20. @Test//测试添加事务的转账功能
  21. void accountMoney() {
  22. ApplicationContext context=new ClassPathXmlApplicationContext("mysqlbean.xml");
  23. UserService userService = context.getBean("userService", UserService.class);
  24. userService.accountMoney("sunchao","xiaoming",100);
  25. }
  26. }

数据结果显示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/576768
推荐阅读
相关标签
  

闽ICP备14008679号