Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the idea of MyBatis-Plus realizing optimistic lock through version mechanism?

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about MyBatis-Plus 's idea of realizing optimistic locks through the version mechanism. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

MyBatis-Plus implements optimistic locking through the version mechanism.

General ideas:

Take out the record and carry the current version of the record

When updating the record, compare whether the current version of the record has changed

If the version does not change, update the record and update the version, the general value + 1

If the version changes, the record is not updated.

The core idea of the version mechanism is to assume that the probability of concurrency conflicts is very low, only when the data is updated to check whether there is a conflict, and the basis for judging whether there is a conflict is whether the value of version has been changed.

Configuration

There are two steps to configure optimistic locks in MyBatis-Plus:

Instantiate OptimisticLockerInnerInterceptor and add it to the interceptor chain of MyBatis-Plus

Define the version field and annotate the version field in Entity with the @ Version annotation.

Description:

Only supported data types: int, Integer, long, Long, Date, Timestamp, LocalDateTime

NewVersion = oldVersion + 1 under integer type

NewVersion will write back to entity

Only updateById (id) and update (entity, wrapper) methods are supported

Under the update (entity, wrapper) method, wrapper cannot be reused!

The configuration is as follows:

First, instantiate the OptimisticLockerInnerInterceptor and add it to the interceptor chain:

@ Configurationpublic class MyBatisPlusConfig {/ * plug-in configuration * * @ return * / @ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor (); / / add a paging interceptor to the filter chain of MyBatis-Plus. You need to set the database type (mainly used for paging dialects) interceptor.addInnerInterceptor (new PaginationInnerInterceptor (DbType.MYSQL)) / / add optimistic lock interceptor interceptor.addInnerInterceptor (new OptimisticLockerInnerInterceptor ()); return interceptor;}}

Then, use the @ Version annotation:

@ Data@TableName ("tb_user") public class UserEntity {private Long id; private String name; private Integer age; private String email; @ TableField (value = "create_time", fill = FieldFill.INSERT) private Date createTime; @ TableField (value = "update_time", fill = FieldFill.INSERT_UPDATE) private Date updateTime @ TableLogic (value = "0", delval = "- 1") @ TableField (value = "delete_flag", fill = FieldFill.INSERT) private Integer deleteFlag; @ Version @ TableField (value = "version", fill = FieldFill.INSERT) private Integer version;}

When configuring insert, the version defaults to 1:

/ * Auto-populated fields are worth configuring * / @ Componentpublic class AutoFillFieldValueConfig implements MetaObjectHandler {@ Override public void insertFill (MetaObject metaObject) {this.strictInsertFill (metaObject, "createTime", Date.class, new Date ()); this.strictInsertFill (metaObject, "updateTime", Date.class, new Date ()); this.strictInsertFill (metaObject, "deleteFlag", Integer.class, 0) This.strictInsertFill (metaObject, "version", Integer.class, 1);} @ Override public void updateFill (MetaObject metaObject) {this.strictUpdateFill (metaObject, "updateTime", Date.class, new Date ());}} Test

1. Test new records

First of all, add a piece of data:

@ Test public void testVersionInsert () {/ / insert a new user UserEntity newUser = new UserEntity (); newUser.setId (12L); newUser.setName ("Kelly"); newUser.setAge (28); newUser.setEmail ("Kelly@163.com"); userMapper.insert (newUser);}

Console log:

= > Preparing: INSERT INTO tb_user (id, name, age, email, create_time, update_time, delete_flag, version) VALUES (?

= > Parameters: 12 (Long), Kelly (String), 28 (Integer), Kelly@163.com (String), 2021-09-25 00 14-Kelly 23.894 (Timestamp), 2021-09-25 00 14-Integer 23.896 (Timestamp), 0 (Integer), 1 (Integer)

Preparing: SELECT id,name,age,email,create_time,update_time,delete_flag,version FROM tb_user WHERE id=? AND delete_flag=0

= > Parameters: 12 (Long)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report