当前位置:   article > 正文

Mybatis-plus乐观锁使用_mybatisplus 乐观锁使用

mybatisplus 乐观锁使用

多事务环境下如何保证数据库操作安全,常用的一种解决方案就是对操作数据表进行加锁处理。根据实现思路不同分:悲观锁与乐观锁2种。

悲观锁:悲观的认为多事务操作同一数据是及其不安全的,所以A事务在操作数据时,其他任何事务不允许对该数据进行修改,只能等待A事务操作结束后才可以执行。

乐观锁:乐观的认为A事务在操作数据时,期间不会有其他事务进行干扰,能顺利完成事务操作。

 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.7.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>mybatis-pluslgs</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>mybatis-pluslgs</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <!-- <dependency>-->
  29. <!-- <groupId>org.mybatis.spring.boot</groupId>-->
  30. <!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
  31. <!-- <version>3.0.0</version>-->
  32. <!-- </dependency>-->
  33. <dependency>
  34. <groupId>com.baomidou</groupId>
  35. <artifactId>mybatis-plus-boot-starter</artifactId>
  36. <version>3.4.2</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-devtools</artifactId>
  41. <scope>runtime</scope>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>mysql</groupId>
  46. <artifactId>mysql-connector-java</artifactId>
  47. <scope>runtime</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.projectlombok</groupId>
  51. <artifactId>lombok</artifactId>
  52. <optional>true</optional>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59. </dependencies>
  60. <build>
  61. <plugins>
  62. <plugin>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-maven-plugin</artifactId>
  65. <configuration>
  66. <excludes>
  67. <exclude>
  68. <groupId>org.projectlombok</groupId>
  69. <artifactId>lombok</artifactId>
  70. </exclude>
  71. </excludes>
  72. </configuration>
  73. </plugin>
  74. </plugins>
  75. </build>
  76. </project>

 

添加配置类 开启乐观锁

 

 

 

 

MyBatis-Plus会自动讲乐观锁逻辑加载到sql中

使用Mybatis-Plus注意:

乐观锁支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime 仅支持 updateById(id)update(entity, wrapper) 方法 另外,每次操作前都是先查询,替换,再更新,否则乐观锁无效

 

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

闽ICP备14008679号