MyBatisPlus configuration log, use of CRUD

Configuration log

All our sql is invisible in mybatisplus, so we need to configure logs during development. After the development is completed, we can cancel the logs, because logs also consume resources

#Configuration log
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #Console output only logs

After configuring the log, you need to pay attention to the automatically generated SQL in the later study. I believe you will like MybatisPlus

CRUD usage

Insert insert

    //Test insertion
    public void testInsert(){
        Air air = new Air();
        air.setLastModifyTime(new Date());
        air.setMonitoringStation("Ha ha ha ha ha ha");
        air.setMonitorTime(new Date());

Here we can see that the system automatically generates an id for us

Because my database settings are self incrementing, I cancel self incrementing

Take another look at the insertion results and find that they are marked in red. Because the id with 0 as the primary key will be inserted by default, I had a 0 before, so the insertion failed

If someone's id is of Lang type, a long number may be inserted as the id by default. Here is the random number generated by the snowflake algorithm, which is globally unique (globally unique id)
Corresponding to the primary key in the database (uuid, auto increment id, snowflake algorithm, redis,zookeeper)

Primary key generation strategy

Twitter's snowflake algorithm (snowflake algorithm)

Twitter's flag ID is the result of an open-source algorithm. Its core idea is to use 41bit as the number of milliseconds, 10bit as the machine ID (five bits are the data center and five bit machine ID), 12bit as the serial number within milliseconds (meaning that each node can generate 4096 IDS per millisecond), and finally there is a symbol bit, which is always 0.

To set the method of adding the primary key, you can add the annotation @ TableId. Because it is an enumeration class, there are many methods later

update operation

 //Test update
    public void testUpdate(){
        Air air = new Air();
        air.setLastModifyTime(new Date());
        air.setMonitorTime(new Date());

Because new Date() appears in the code, which is very low and consumes resources, we can use annotation:

Auto fill

The creation time and modification time are generally completed automatically. You do not want to update them manually
Alibaba R & D Manual: all database tables: GMT_ create,gmt_ Modified (GMT)

Method 1: database method (it is generally not allowed to modify the database in work, because there are many complex problems involved, especially easy to find bug s)

  1. The two tables in the table are the creation and modification time
  2. Check to automatically update the current time

If you have your own small partners who write dates, you don't have to add them one by one in the future

Code level

  1. Delete database defaults
  2. The annotation @ TableField needs to be added to the field attribute of the entity class

The source code is as follows. You can see that the fill method is the fill method

public class Air {
  @TableId(type = IdType.ASSIGN_ID)
  private Long id;
  private long districtId;
  @TableField(fill = FieldFill.INSERT) //Update the insertion time when inserting
  private Date monitorTime;
  private long pm10;
  private long pm25;
  private String monitoringStation;
  @TableField(fill = FieldFill.INSERT_UPDATE) //The time is updated both during update and insertion
  private Date lastModifyTime;

Of course, the filling policy can be rewritten to customize the filling policy

First, create a MyMetaObjectHandler class (whatever its name)

@Slf4j  //Grab log
@Component  //For the annotation of SpringBoot, don't forget to add the processor to the IOC container
public class MyMetaObjectHandler implements MetaObjectHandler {

    public void insertFill(MetaObject metaObject) {"start insert fill");
//Source code: setfieldvalbyname (string fieldname, object fieldval, metaobject metaobjec)
        this.setFieldValByName("monitor_time",new Date(),metaObject);
        this.setFieldValByName("last_modify_time",new Date(),metaObject);

    public void updateFill(MetaObject metaObject) {"Start update fill....");
        this.setFieldValByName("last_modify_time",new Date(),metaObject);



to update

Tags: MybatisPlus

Posted by Bike Racer on Thu, 26 May 2022 01:56:14 +0300