SpringBoot integration Mysql, Mybatis, Mybatis-Plus, the implementation of add, delete, change and check

One, foreword

Mybatis-Plus is Mybatis enhancement tool, in addition to encapsulation of the basic add, delete, change, check, but also provides some fun things, such as logical delete configuration and automatic filling default values.

  • Remember: All deletions are logical deletions, so the database must have a field that is typically is_deleted 0- indicating that it was not deleted 1- indicating that it was deleted

Second, SQL statements

CREATE TABLE `user_info` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'user id',
	`username` VARCHAR(20) NOT NULL DEFAULT ' ' COMMENT 'Username' COLLATE 'utf8mb4_general_ci',
	`password` VARCHAR(100) NOT NULL DEFAULT ' ' COMMENT 'password' COLLATE 'utf8mb4_general_ci',
	`is_deleted` INT(2) NOT NULL DEFAULT '0' COMMENT 'Deleted 0- Not deleted 1- Deleted',
	`create_time` DATETIME NOT NULL COMMENT 'Creation time',
	`update_time` DATETIME NOT NULL COMMENT 'Update Time'.PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
Copy the code

User table, mainly the following fields:

  • Id User ID Primary key
  • The username username
  • “Password,” password
  • Is_deleted Whether the directory is deleted. 0 N/A Not deleted 1 n/A Deleted
  • Create_time Creation time
  • Update_time Update time

3. Delete the configuration logically

application.properties

Name =spring-boot-demo server.port=8888 spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shang hai&useSSL=false spring.datasource.username=root spring.datasource.password=0320 Spring.datasource. Driver-class-name = com.mysql.cj.jdbc.driver # mybatis Mybatis. Mapper-locations =classpath:mapper/* mybatis mybatis-plus.global-config.db-config.logic-delete-field=isDeleted mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0Copy the code

Configure logic to delete field isDeleted(note that this is a class member variable, not the database field is_deleted)

Mybatis -plus.global-config.db-config.logic-delete-field=isDeleted mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0Copy the code

Delete is_deleted = 1; delete is_deleted = 0;

4. Automatically fill in the default value

MyMetaObjectHandler class

package com.llh.springbootdemo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/ * * *@author llh
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_TIME = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        // Automatically fill in the creation time
        if (metaObject.hasGetter(CREATE_TIME)) {
            this.fillStrategy(metaObject, CREATE_TIME, LocalDateTime.now());
        }

        // Automatically fill in the update time
        if (metaObject.hasGetter(UPDATE_TIME)) {
            this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now()); }}@Override
    public void updateFill(MetaObject metaObject) {
        // Automatically fill in the update time
        if (metaObject.hasGetter(UPDATE_TIME)) {
            this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now()); }}}Copy the code
  • InsertFill Inserts data into the database field create_time update_time automatically inserts the current time
  • When updateFill updates data, the database field update_time is automatically set to the current time

MyConfiguration class

package com.llh.springbootdemo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author llh */ @Configuration public class MyConfiguration { @Bean public MyMetaObjectHandler myMetaObjectHandler() { return new MyMetaObjectHandler(); }}Copy the code
  • Configure classes to automatically inject beans.

Add annotations @tableField (fill = fieldfill.insert) and @tablefield (fill = fieldfill.insert_update) to class member variables to indicate that auto-filling is required.

@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;

@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;

Copy the code

The UserInfo class

package com.llh.springbootdemo.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;

import java.time.LocalDateTime;

/ * * *@author llh
 */
public class UserInfo {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String username;

    private String password;

    private Integer isDeleted;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    public Long getId(a) {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername(a) {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword(a) {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getIsDeleted(a) {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }

    public LocalDateTime getCreateTime(a) {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime(a) {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public String toString(a) {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\' ' +
                ", password='" + password + '\' ' +
                ", isDeleted=" + isDeleted +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '} '; }}Copy the code
  • The insert operation then fills in the default values automatically.

Five, the conclusion

  • See the previous article for the configuration and use of Mybatis-Plus. SpringBoot integration Mysql, Mybatis, Mybatis-Plus, the implementation of add, delete, change and check
  • Mybatis-Plus website: mp.baomidou.com/