提交 94a3dd22 编写于 作者: E Erwin

乐观锁,字段填充

上级 ef04f16c
# 乐观锁
添加拦截器
```
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
```
乐观锁字段注解:
```
@Version
```
# 自动填充
@TableField(.. fill = FieldFill.INSERT)
```
/**
* 默认不处理
*/
DEFAULT,
/**
* 插入填充字段
*/
INSERT,
/**
* 更新填充字段
*/
UPDATE,
/**
* 插入和更新填充字段
*/
INSERT_UPDATE
```
实现元对象处理器接口:com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
```
注意事项:
- 填充原理是直接给entity的属性设置值!!!
- 注解则是指定该属性在对应情况下必有值,如果无值则入库会是null
- MetaObjectHandler提供的默认方法的策略均为:如果属性有值则不覆盖,如果填充值为null则不填充
- 字段必须声明TableField注解,属性fill选择对应策略,该声明告知Mybatis-Plus需要预留注入SQL字段
- 填充处理器MyMetaObjectHandler在 Spring Boot 中需要声明@Component或@Bean注入
- 要想根据注解FieldFill.xxx和字段名以及字段类型来区分必须使用父类的strictInsertFill或者strictUpdateFill方法
- 不需要根据任何来区分可以使用父类的fillStrategy方法
```
还需要再研究研究
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.fengwenyi.mybatisplusexample.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
......@@ -18,7 +19,8 @@ public class MyBatisPlusConfiguration {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 分页插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
return interceptor;
}
......
......@@ -75,25 +75,25 @@ public class GoodsEntity extends Model<GoodsEntity> {
/**
* 创建时间
*/
@TableField("create_time")
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 创建人
*/
@TableField("create_by")
@TableField(value = "create_by", fill = FieldFill.INSERT)
private String createBy;
/**
* 更新时间
*/
@TableField("update_time")
@TableField(value = "update_time", fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
/**
* 修改人
*/
@TableField("update_by")
@TableField(value = "update_by", fill = FieldFill.UPDATE)
private String updateBy;
/**
......
......@@ -9,8 +9,8 @@ import lombok.Getter;
*/
@Getter
public enum GoodsFlagEnum implements IEnum<Integer> {
UP(0, "下架"),
DOWN(1, "上架"),
DOWN(0, "下架"),
UP(1, "上架"),
;
private final Integer value;
......
package com.fengwenyi.mybatisplusexample.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* @author Erwin Feng
* @since 2021-04-01
*/
@Slf4j
@Component
public class MyBatisPlusAutoFillMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
// log.info("start insert fill ....");
// this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
// 或者
// this.strictUpdateFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
// 或者
// this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
}
@Override
public void updateFill(MetaObject metaObject) {
metaObject.setValue("updateTime", LocalDateTime.now());
// log.info("start update fill ....");
// this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
// 或者
// this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); // 起始版本 3.3.3(推荐)
// 或者
// this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug)
}
}
......@@ -17,28 +17,17 @@ mybatis-plus:
# 扫描枚举类 # 支持统配符 * 或者 ; 分割
type-enums-package: com.fengwenyi.mybatisplusexample.entity.enums
global-config:
#刷新mapper 调试神器
db-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: ID_WORKER
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: NOT_EMPTY
#驼峰下划线转换
#column-underline: true
#数据库大写下划线转换
#capital-mode: true
capital-mode: true
#逻辑删除配置
logic-delete-value: 1
logic-not-delete-value: 0
db-type: mysql
refresh: true
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
#自定义SQL注入器
#sql-injector: com.baomidou.springboot.xxx
# logic-delete-field: delete_status # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑删除
logic-not-delete-value: 0 # 正常
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
logback:
appName: mybatis-plus-example
......
package com.fengwenyi.mybatisplusexample;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fengwenyi.mybatisplusexample.entity.CategoryEntity;
import com.fengwenyi.mybatisplusexample.entity.GoodsEntity;
import com.fengwenyi.mybatisplusexample.entity.enums.GoodsFlagEnum;
import com.fengwenyi.mybatisplusexample.repository.MPCategoryRepository;
import com.fengwenyi.mybatisplusexample.repository.MPGoodsRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.math.BigDecimal;
import java.util.List;
/**
* @author Erwin Feng
* @since 2021-03-08
*/
@Component
@Slf4j
public class GoodsRepositoryTests extends MybatisPlusExampleApplicationTests {
@Autowired
private MPCategoryRepository mpCategoryRepository;
@Autowired
private MPGoodsRepository mpGoodsRepository;
@Test
public void testAdd() {
LambdaQueryWrapper<CategoryEntity> categoryQueryWrapper = new LambdaQueryWrapper<CategoryEntity>()
.eq(CategoryEntity::getName, "家电")
;
List<CategoryEntity> categoryEntityList = mpCategoryRepository.list(categoryQueryWrapper);
CategoryEntity categoryEntity = categoryEntityList.get(0);
GoodsEntity goodsEntity = new GoodsEntity()
.setName("平板电脑")
.setPrice(new BigDecimal("4699.00"))
.setStockNum(10000L)
.setFlag(GoodsFlagEnum.UP)
.setCategoryId(categoryEntity.getId())
.setCreateBy("admin")
;
Assert.isTrue(mpGoodsRepository.save(goodsEntity), "save fail");
Assert.isTrue(mpGoodsRepository.removeById(goodsEntity), "deleteById fail");
mpGoodsRepository.save(goodsEntity);
}
@Test
public void testQuery() {
List<GoodsEntity> goodsEntityList = mpGoodsRepository.list();
for (GoodsEntity goodsEntity : goodsEntityList) {
log.info(goodsEntity.toString());
log.info(goodsEntity.getFlag().getValue() + "");
}
}
@Test
public void testUpdate() {
LambdaQueryWrapper<GoodsEntity> goodsQueryWrapper = new LambdaQueryWrapper<GoodsEntity>().eq(GoodsEntity::getName, "平板电脑");
List<GoodsEntity> goodsEntityList = mpGoodsRepository.list(goodsQueryWrapper);
GoodsEntity goodsEntity = goodsEntityList.get(0);
goodsEntity.setPrice(new BigDecimal("6499.00"));
mpGoodsRepository.updateById(goodsEntity);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册