当前位置:   article > 正文

Mybatis-plus通过MetaObjectHandler接口拦截实现自动填充创建时间、更新时间等字段_mybatisplus拦截器处理创建时间和更新时间

mybatisplus拦截器处理创建时间和更新时间

1.添加mybatis-plus依赖:
 

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>

2.添加一个全局处理创建时间、更新时间的实体类Entity

package com.shardingsphere.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;

import java.util.Date;

public class Entity<T extends Entity<?>> extends Model<T> {

    public final static String META_CREATED = "creationTime";
    public final static String META_UPDATE = "updateTime";

    @TableField(fill = FieldFill.INSERT)
    private Date creationTime;

    @TableField(fill = FieldFill.UPDATE)
    private Date updateTime;

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

3.创建一个测试表的实体类user  并继承实体类Entity

package com.shardingsphere.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author 何志鹏
 * @Date 2022/7/15 15:02
 * @Version 1.0
 */
@Data
public class User extends Entity<User> implements Serializable {

    private static final long serialVersionUID = -94704944729049233L;

    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    private  String name;
    private Long age;
    private  String sex;
    private  String education;

}

4.创建MyBatisPlusConfig 并继承MetaObjectHandler

package com.shardingsphere.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.shardingsphere.entity.Entity;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Calendar;

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MyBatisPlusConfig implements MetaObjectHandler {


    /**
     * 创建时间
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof Entity) {
            Entity entity = (Entity) metaObject.getOriginalObject();
            if (entity.getCreationTime() == null) {
                this.setFieldValByName(Entity.META_CREATED, Calendar.getInstance().getTime(), metaObject);
            }
        }
    }

    /**
     * 更新时间
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof Entity) {
            Entity entity = (Entity) metaObject.getOriginalObject();
            if (entity.getUpdateTime() == null) {
                this.setFieldValByName(Entity.META_UPDATE, Calendar.getInstance().getTime(), metaObject);
            }
        }
    }

    /**
     * 分页
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

5.创建userMapper接口

package com.shardingsphere.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shardingsphere.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @Author 何志鹏
 * @Date 2022/7/15 15:10
 * @Version 1.0
 */
@Mapper
@Repository
public interface UserMapper  extends BaseMapper<User> {

}

6.最后写一个测试方法DemoTest

package com.shardingsphere;
import com.shardingsphere.Mapper.UserMapper;

import com.shardingsphere.entity.User;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Author 何志鹏
 * @Date 2022/7/15 15:15
 * @Version 1.0
 */
@SpringBootTest
public class DemoTest {


    @Autowired
    private UserMapper userMapper;
    

    /**
     * 测试user分库
     */
    @Test
    void contextLoads() {


   /*     User user = new User();
        user.setName("何志鹏3");
        user.setSex("2");
        user.setAge(12L);
        user.setEducation("1");
        userMapper.insert(user);*/
        
        User user = new User();
        user.setName("何志鹏3");
        user.setSex("2");
        user.setEducation("1");
        user.setId(1549220056852897794L);
        userMapper.updateById(user);
        
    }
}

测试结果:

 


                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/505524
推荐阅读
相关标签
  

闽ICP备14008679号