Java小强个人技术博客站点    手机版
当前位置: 首页 >> 框架 >> MyBatisPlus中MetaObjectHandler的使用

MyBatisPlus中MetaObjectHandler的使用

4991 框架 | 2023-12-21

起因是公司一个同事接到需求,让把一条数据录入时createTime字段,设置为指定的字段,但是他走断点发现执行MyBatisPlus的insert之前值是对的,但是insert之后就改成当前时间了。

开始我认为是数据库字段设置为了根据当前时间录入值,即默认为当前时间戳,但是一想不对,默认是指没有设置值,这里已经设置了为什么还是不对?

后来发现,系统代码中设置了MetaObjectHandler处理,特殊处理了createTime这个字段。


mybatisplus 插件.jpg


MetaObjectHandler:元数据对象处理器

MetaObjectHandler接口是mybatisPlus为我们提供的的一个扩展接口,我们可以利用这个接口在我们插入或者更新数据的时候,为一些字段指定默认值。

使用场景:公共字段填充等,如updateTime、createTime、createUser、updateUser等公共字段多的填充。


使用方法

实体的公共字段使用@TableField注解;

1)@TebleField(fill = FieldFill.INSERT):表示此字段只在插入/新增操作时更新数据

2)@TebleField(fill = FieldFill.INSERT_UPDATE):表示此字段在修改和新增操作时都更新数据;

3)@TebleField(fill = FieldFill.UPDATE):表示此字段只在修改操作时都更新数据;


自定义元对象处理器类,实现MetaObjectHandler接口,重写insertFill、updateFill方法。


示例实体:

package com.example.springboot.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("sp_boot_user")
public class User {
    @TableId(value = "id", type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    private Integer id;
    @TableField(exist = false)
    private List<Integer> ids;

    @TableField(value = "u_name")
    private String name;

    /**
     * 余额
     */
    @TableField("balance_point")
    private Long balancePoint;

    /**
     * 总额
     */
    @TableField("total_point")
    private Long totalPoint;

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

    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @TableField(value = "update_time", fill = FieldFill.UPDATE)
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 请求参数
     */
    @TableField(exist = false)
    private Map<String, Object> params = new HashMap<>();
}


示例接口实现:

package com.example.springboot.config;

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

import java.util.Date;

@Component
public class MyMetaObjecthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", new Date());
    }
}


编写单元测试:

@Test
public void testInsert(){
	User user = new User();
	user.setName("测试1");
	user.setAddress("地址A");
	user.setTotalPoint(100L);
	user.setBalancePoint(50L);
	userMapper.insert(user);
}


查看打印:

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16f62062] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d763ae5] will not be managed by Spring
==>  Preparing: INSERT INTO sp_boot_user ( u_name, balance_point, total_point, address, create_time ) VALUES ( ?, ?, ?, ?, ? )
==> Parameters: 测试1(String), 50(Long), 100(Long), 地址A(String), 2023-12-21 11:45:31.45(Timestamp)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16f62062]


可以看到,insert时,会自动注入当前的时间。


推荐您阅读更多有关于“ MyBatisPlus MetaObjectHandler 自动注入 ”的文章

上一篇:FastJson指定Long类型序列化方式 下一篇:强制浏览器Edge、GoogleChrome、Firefox中强制启用黑色模式

猜你喜欢

发表评论:

评论:

回复 商城系统 评论于 2023-12-29 17:17
感谢分享