上一篇“SpringBoot集成Mysql、Mybatis、Mybatis-Plus,實現(xiàn)增刪改查”
一、前言
Mybatis-Plus是Mybatis增強工具,除了封裝了基本的增刪改查之外,還提供了一些好玩的東西,如邏輯刪除配置和自動填入默認值。
- 記?。核械膭h除都是邏輯刪除,所以數(shù)據(jù)庫必須有一個字段,一般是 is_deleted 0-表示未刪除 1-表示已刪除
二、sql語句
CREATE TABLE `user_info` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '用戶id',
`username` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '用戶名' COLLATE 'utf8mb4_general_ci',
`password` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '密碼' COLLATE 'utf8mb4_general_ci',
`is_deleted` INT(2) NOT NULL DEFAULT '0' COMMENT '是否刪除 0-未刪除 1-已刪除',
`create_time` DATETIME NOT NULL COMMENT '創(chuàng)建時間',
`update_time` DATETIME NOT NULL COMMENT '更新時間',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;
用戶表,主要就以下字段:
- id 用戶id 主鍵
- username 用戶名
- password 密碼
- is_deleted 是否刪除 0-未刪除 1-已刪除
- create_time 創(chuàng)建時間
- update_time 更新時間
三、邏輯刪除配置
application.properties
# 應(yīng)用名稱
spring.application.name=spring-boot-demo
server.port=8888
# 數(shù)據(jù)庫相關(guān)
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=0320
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis相關(guān)
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
# mybatis-plus相關(guān)
# 邏輯刪除
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=0
配置邏輯刪除字段 isDeleted(注意是類成員變量而不是數(shù)據(jù)庫字段is_deleted)
已刪除的值:1 未刪除的值:0
# 邏輯刪除
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=0
這樣調(diào)用Mybatis-Plus的刪除方法會去更改is_deleted為1,查詢方法只查詢is_deleted = 0的數(shù)據(jù)。
四、自動填入默認值
MyMetaObjectHandler 類
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) {
// 自動填入創(chuàng)建時間
if (metaObject.hasGetter(CREATE_TIME)) {
this.fillStrategy(metaObject, CREATE_TIME, LocalDateTime.now());
}
// 自動填入更新時間
if (metaObject.hasGetter(UPDATE_TIME)) {
this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now());
}
}
@Override
public void updateFill(MetaObject metaObject) {
// 自動填入更新時間
if (metaObject.hasGetter(UPDATE_TIME)) {
this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now());
}
}
}
- insertFill 插入數(shù)據(jù)的時候數(shù)據(jù)庫字段create_time update_time自動填入當前時間
- updateFill 更新數(shù)據(jù)的時候數(shù)據(jù)庫字段update_time自動填入當前時間
MyConfiguration 類
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();
}
}
- 配置類,自動注入Bean。
在類成員變量加上注解@TableField(fill = FieldFill.INSERT)和@TableField(fill = FieldFill.INSERT_UPDATE),表示需要自動填入。
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
UserInfo 類
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() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(Integer isDeleted) {
this.isDeleted = isDeleted;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", isDeleted=" + isDeleted +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
- 然后插入操作就可以自動填入默認值了。
五、結(jié)語
- 關(guān)于Mybatis-Plus的配置和使用請看上一篇文章。
- Mybatis-Plus官網(wǎng)地址: https://mp.baomidou.com/