mybatis plus

常用注解

官方文档写的很清楚了
https://baomidou.com/guide/annotation.html#tablename

package com.rainbow.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.time.LocalDateTime;

@TableName(value = "user")
public class User {
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    @TableField(value = "username")
    private String username;

    @TableField(value = "password")
    private String password;

    @TableField(value = "age")
    private Integer age;

    @TableLogic
    @TableField(value = "is_delete")
    private Integer isDelete;

    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    private LocalDateTime updateTime;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", isDelete=" + isDelete +
                ", createTime=" + createTime +
                ", updateTime=" + 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 getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }

    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;
    }
}

mybatis plus

逻辑删除

文档也写的很清楚了
https://baomidou.com/guide/logic-delete.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95
添加一is_delete个字段用于标识逻辑删除
mybatis plus

通用service

首先service接口继承通用service接口 泛型为实体类

public interface UserService extends IService<User> {
}

然后service实体类基础通用service实体类 第一个泛型为mapper接口 mapper接口中必须继承通用maper 第二个泛型为实体类

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
}

控制台输出SQL语句

application.yml中配置

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

自动填充

实体类中添加相关注解
mybatis plus
Handle实现 MetaObjectHandler类
不知道为何一边查询一边更新无法自动填充。。是因为事务吗?
官方文档写的很清楚了 https://baomidou.com/guide/auto-fill-metainfo.html

package com.rainbow.handle;

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

import java.time.LocalDateTime;

@Component
public class MetaObjectHandle implements MetaObjectHandler {
    private Logger logger = LoggerFactory.getLogger(MetaObjectHandle.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());


    }

    public void updateFill(MetaObject metaObject) {
        logger.info("更新自动填充中....");
        this.strictUpdateFill(
                metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
    }
}

优化

可以判断是否有字段 如果有在进行自动填充

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        if (metaObject.hasSetter("auth")) {
            this.strictInsertFill(metaObject, "auth", LocalDateTime.class, LocalDateTime.now());
        }

    }
上一篇:JDK.1.8新特性


下一篇:获取当天最大或最小日期