Simple Application of Mybatis plus in Project

  • 2021-10-15 10:37:14
  • OfStack

Directory paging plug-in logical deletion automatic filling optimistic lock multi-data source general enumeration id generation and primary key query LambdaQueryWrapper

This article is an essay, which records some scenarios applied in the project.

Mybatis-plus is a powerful weapon of OOM in Spring framework, which is easy to use and can be quickly used by referring to official website documents. mp. baomidou. com/guide/

p6spy performs SQL analysis and printing, and only needs to add dependencies and configuration files to have perfect sql printing. Can't be used on performance loss line

Paging plug-in

Just inject plug-ins, which is too convenient.


//  Latest edition 
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

Logical deletion

After global configuration, it is no longer necessary to delete fields logically. Custom sql in xml needs to process logical fields by itself


mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: flag  #  Entity field name for global logical deletion (since 3.3.0, You can ignore the step of not configuring after configuration 2)
      logic-delete-value: 1 #  Logically deleted value ( Default to  1)
      logic-not-delete-value: 0 #  Logical undeleted value ( Default to  0)

@TableLogic
private Integer deleted;

Autofill

Mom doesn't have to worry about create_time and update_time anymore. The default injection field value is null, that is, it will not be injected after the value is set manually. Pay special attention to the value problem at beanUtil. copy.
You can obtain the current user information from header, shiro and Security, and update createUser and updateUser


@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); //  Initial version  3.3.0( Recommended use )
      String user = "anonymous";
      //  From header shiro security Get from user Information 
        this.strictInsertFill(metaObject, "createUser", String.class, user); 
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); //  Initial version  3.3.0( Recommend )
      String user = "anonymous";
      //  From header shiro security Get from user Information 
      this.strictUpdateFill(metaObject, "updateUser", String.class, user);
        //  Or 
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); //  Initial version  3.3.3( Recommend )
        //  Or 
        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); //  You can also use the (3.3.0  The method has bug)
    }
}

Optimistic lock

There are few direct applications. For special situations, some interfaces will be processed by version.


@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}

@Version
private Integer version;

Multiple data sources

Sometimes, it is used to switch data sources through annotations. Reference article: https://www.ofstack.com/article/199726. htm

Universal enumeration

Through the @ EnumValue tag value, the enumeration value in the database can be converted into no enumeration type, and one step less conversion can be done. Reference article: https://www.ofstack.com/article/194192. htm

id Generation and Primary Key

Distributed services are basically based on business to divide databases, and one micro-service basically corresponds to one library. For most applications, there is no need to divide tables, so bigint self-adding id is enough.

Query LambdaQueryWrapper


List<User> list = userService.list(Wrappers.<User>lambdaQuery()
.eq(User::getUserName,"123")
)

List<User> list = userService.lambdaQuery()
.eq(User::getUserName,"123")
.list();

Logically only 1 queries 1 value, false does not throw an exception when there are multiple values.


LambdaQueryWrapper queryWrapper = Wrappers.<User>lambdaQuery()
.eq(User::getUserName,"123");
User user = userService.getOne(queryWrapper,false);

Related articles: