一、@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 = "https://www.cnblogs.com/mengd/p/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 = "https://www.cnblogs.com/mengd/p/student")
public class Student {
@TableId
private Long id;
// 當該欄位名稱與資料庫名字不一致
@TableField(value = "https://www.cnblogs.com/mengd/p/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 = "https://www.cnblogs.com/mengd/p/student")
public class Student {
@TableId
private Long id;
@TableField(value = "https://www.cnblogs.com/mengd/p/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 = "https://www.cnblogs.com/mengd/p/student")
public class Student {
@TableId
private Long id;
@TableField(value = "https://www.cnblogs.com/mengd/p/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 = "https://www.cnblogs.com/mengd/p/student")
public class Student {
@TableId
private Long id;
@TableField(value = "https://www.cnblogs.com/mengd/p/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=https://www.cnblogs.com/mengd/p/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,這都是框架自動實作的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/237421.html
標籤:Java
上一篇:Nginx限流配置
