首页 > 其他 > 详细

mybatis plus

时间:2021-06-09 18:40:46      阅读:42      评论:0      收藏:0      [点我收藏+]

常用注解

官方文档写的很清楚了
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;
    }
}

技术分享图片

逻辑删除

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

通用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

自动填充

实体类中添加相关注解
技术分享图片
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());
        }

    }

分页插件

配置 文档写的很清楚了
https://baomidou.com/guide/page.html

@Configuration
public class MybatisPlusPageConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor =
                new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        return mybatisPlusInterceptor;
    }
}

调用时构造一个IPage对象查询计科

        IPage<User> page = new Page<>(0, 2);

        IPage<User> page1 = userDao.selectPage(page, null);

mybatis plus

原文:https://www.cnblogs.com/lyraHeartstrings/p/14866387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!