MyBatisPlus-常用注解


一、@TableName

映射數據庫的表名

package com.md.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.md.enums.StatusEnum;
import lombok.Data;

import java.util.Date;

/**
 * @author md
 * @Desc
 * @date 2020/10/26 15:38
 */

@Data
@TableName(value = "student")
public class Student {
    private Integer id;
    private String name;
    private Integer age;
}

二、@TableId

設置主鍵映射,value 映射主鍵字段名

type 設置主鍵類型,主鍵的生成策略

AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4),
/** @deprecated */
@Deprecated
ID_WORKER(3),
/** @deprecated */
@Deprecated
ID_WORKER_STR(3),
/** @deprecated */
@Deprecated
UUID(4);
描述
AUTO 數據庫自增
NONE MP set 主鍵,雪花算法實現
INPUT 需要開發者手動賦值
ASSIGN_ID MP 分配 ID,Long、Integer、String
ASSIGN_UUID 分配 UUID,Strinig

INPUT 如果開發者沒有手動賦值,則數據庫通過自增的方式給主鍵賦值,如果開發者手動賦值,則存入該值。

AUTO 默認就是數據庫自增,開發者無需賦值。

ASSIGN_ID MP 自動賦值,雪花算法。

ASSIGN_UUID 主鍵的數據類型必須是 String,自動生成 UUID 進行賦值

	// 自己賦值
    //@TableId(type = IdType.INPUT)
    // 默認使用的雪花算法,長度比較長,所以使用Long類型,不用自己賦值
    @TableId
    private Long id;

測試

@Test
    void save(){
        // 由於id加的有注解,這里就不用賦值了
        Student student = new Student();
        student.setName("天明");
        student.setAge(18);
        mapper.insert(student);
    }

三、@TableField

映射非主鍵字段,value 映射字段名

exist 表示是否為數據庫字段 false,如果實體類中的成員變量在數據庫中沒有對應的字段,則可以使用 exist,VO、DTO

select 表示是否查詢該字段

fill 表示是否自動填充,將對象存入數據庫的時候,由 MyBatis Plus 自動給某些字段賦值,create_time、update_time

自動填充

1、給表添加 create_time、update_time 字段

2、實體類中添加成員變量

package com.md.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.md.enums.StatusEnum;
import lombok.Data;
import java.util.Date;

@Data
@TableName(value = "student")
public class Student {
    @TableId
    private Long id;

    // 當該字段名稱與數據庫名字不一致
    @TableField(value = "name")
    private String name;

    // 不查詢該字段
    @TableField(select = false)
    private Integer age;

    // 當數據庫中沒有該字段,就忽略
    @TableField(exist = false)
    private String gender;

    // 第一次添加填充
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    // 第一次添加的時候填充,但之后每次更新也會進行填充
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

}

3、創建自動填充處理器

注意:不要忘記添加 @Component 注解

package com.md.handler;

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

import java.util.Date;

/**
 * @author md
 * @Desc 對實體類中使用的自動填充注解進行編寫
 * @date 2020/10/26 17:29
 */
// 加入注解才能生效
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}

4、測試

@Test
    void save(){
        // 由於id加的有注解,這里就不用賦值了
        Student student = new Student();
        student.setName("韓立");
        student.setAge(11);
        // 時間自動填充
        mapper.insert(student);
    }

5、更新

當該字段發生變化的時候時間會自動更新

@Test
    void update(){
        Student student = mapper.selectById(1001);
        student.setName("韓信");
        mapper.updateById(student);
    }

四、@Version

樂觀鎖

標記樂觀鎖,通過 version 字段來保證數據的安全性,當修改數據的時候,會以 version 作為條件,當條件成立的時候才會修改成功。

version = 2

線程 1:update ... set version = 2 where version = 1

線程2 :update ... set version = 2 where version = 1

1、數據庫表添加 version 字段,默認值為 1

2、實體類添加 version 成員變量,並且添加 @Version

package com.md.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.md.enums.StatusEnum;
import lombok.Data;
import java.util.Date;

@Data
@TableName(value = "student")
public class Student {
    @TableId
    private Long id;
    @TableField(value = "name")
    private String name;
    @TableField(select = false)
    private Integer age;
    @TableField(exist = false)
    private String gender;
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    
    @Version
    private Integer version; //版本號
  
}

3、注冊配置類

在 MybatisPlusConfig 中注冊 Bean

package com.md.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author md
 * @Desc
 * @date 2020/10/26 20:42
 */
@Configuration
public class MyBatisPlusConfig {

    /**
    * 樂觀鎖
    */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
}

五、@EnumValue

1、通用枚舉類注解,將數據庫字段映射成實體類的枚舉類型成員變量

package com.southwind.mybatisplus.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

public enum StatusEnum {
    WORK(1,"上班"),
    REST(0,"休息");

    StatusEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @EnumValue
    private Integer code;
    private String msg;
}
package com.md.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.md.enums.StatusEnum;
import lombok.Data;
import java.util.Date;

@Data
@TableName(value = "student")
public class Student {
    @TableId
    private Long id;
    @TableField(value = "name")
    private String name;
    @TableField(select = false)
    private Integer age;
    @TableField(exist = false)
    private String gender;
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    @Version
    private Integer version;
  
    
    private StatusEnum status;
}

數據庫中新增一個status字段,

2、在主配置文件中

# 枚舉包掃描
mybatis-plus.type-enums-package=com.md.enums

六、@TableLogic

映射邏輯刪除,並不是真正的刪除

1、數據表添加 deleted 字段,默認是0

2、實體類添加注解

package com.md.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.md.enums.StatusEnum;
import lombok.Data;
import java.util.Date;

@Data
@TableName(value = "student")
public class Student {
    @TableId
    private Long id;
    @TableField(value = "name")
    private String name;
    @TableField(select = false)
    private Integer age;
    @TableField(exist = false)
    private String gender;
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    @Version
    private Integer version;
    private StatusEnum status;

    @TableLogic
    private Integer deleted;
}

3、主配置文件中添加配置

# 沒有刪除為0,刪除了為1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
mybatis-plus.global-config.db-config.logic-delete-value=1

4、在 MybatisPlusConfig 中注冊 Bean

配置類

@Configuration
public class MyBatisPlusConfig {

    /**
    * 樂觀鎖
    */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
    
    /**
    * 邏輯刪除
    */
    @Bean
	public ISqlInjector sqlInjector() {
		return new LogicSqlInjector();
	}
}

刪除的時候不是真正的刪除數據庫表中的數據,而是改變delete字段的值,當然了查詢的時候也是查詢delete=0,這都是框架自動實現的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM