MyBatisPlus通过@TableField的fill属性和实现MetaObjectHandler完成自动填充字段,更新字段失败bug修复

mybatisPlus自动填充字段,例如创建日期

平时我在写代码的时候,我们系统的大部分表都是由createTime,createBy,updateTime,updateBy等通用字段,于是我写了一个通用的抽象类,将这几个属性放到抽象类里面,让有这几个字段的数据库实体类都来继承这个抽象类,但是后来我发现一个问题,就是我每次在新增数据或者更新数据的时候都要对这几个字段手动赋值,例如:

object.setCreateTime(new Date());
object.setCreateBy("cc");

以上代码基本上再我写的每一个模块都有,并且基本上是一模一样的,于是我在查看myBatisplus的@TableField注解有哪些属性的时候就发现了一个属性——fill,该属性的意思就是填充,填满,所以我上网搜索了一下该属性的用法,发现可以解决我上述的问题,下面介绍一下这个属性的用法。

fill的值为一个枚举类——FieldFill,源码如下:

public enum FieldFill {
    DEFAULT,
    INSERT,
    UPDATE,
    INSERT_UPDATE;

    private FieldFill() {
    }
}

字段含义如下:

枚举值含义
DEFAULT默认值,不对字段做自动填充
INSERT只在执行插入操作时进行字段填充
UPDATE只在执行更新操作时进行字段填充
INSERT_UPDATE在执行插入和更新操作时都进行字段填充

用法如下:

在需要进行自动填充的字段上添加@TableField注解,填充fill属性值,填充策略根据自己的业务决定。

@Data
public abstract class BaseEntity {
    /**
     * 创建者
     */
    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新者
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateBy;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
}

实现MetaObjectHandler接口,重写insertFill方法和updateFill方法。

@Component
public class MybatisPlusHandler implements MetaObjectHandler {
    private final static String CREATE_TIME = "createTime";
    private final static String CREATE_BY = "createBy";
    private final static String UPDATE_TIME = "updateTime";
    private final static String UPDATE_BY = "updateBy";
    private final static String ADMIN = "admin";
    
    @Override
    public void insertFill(MetaObject metaObject) {
        // 新增数据时若对应字段没有值(空串或null)时,以下代码会对该字段进行自动填充
        // 时间的自动填充,时间默认填充当前时区系统时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date now = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        this.strictInsertFill(metaObject, CREATE_TIME, Date.class, now);
        this.strictInsertFill(metaObject, UPDATE_TIME, Date.class, now);

        // 操作用户名填充
        this.strictInsertFill(metaObject, CREATE_BY, String.class, ADMIN);
        this.strictInsertFill(metaObject, UPDATE_BY, String.class, ADMIN);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 修改数据时无论字段是否有值,都会对该字段进行自动填充,时间默认填充当前时区系统时间
        // 时间的自动填充,时间默认填充当前时区系统时间
        LocalDateTime localDateTime = LocalDateTime.now();
        Date now = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        this.setFieldValByName(UPDATE_TIME, now, metaObject);

        // 操作用户名填充
        this.setFieldValByName(UPDATE_BY, adminName, metaObject);
	
        this.strictUpdateFill(metaObject, "updateBy", String.class, ADMIN);
    }
}

在上述示例中,@TableField 注解的 fill 属性分别使用了 FieldFill.INSERTFieldFill.INSERT_UPDATE 两种枚举值,表示在执行数据库的插入和更新操作时,分别针对 createTimeupdateTimecreateByupdateBy字段进行相应的自动填充操作。

需要注意的是,使用FieldFill.INSERT_UPDATE这种方式进行自动填充的时候需要在insertFill方法和updateFill方法中都对使用了该属性的字段做自动填充处理,例如上面代码中的updateTime和updateBy两个字段,如果只在insertFill方法中实现那么在新增一条数据的时候updateTime和updateBy这两个字段就没有值,这是我在自己实际应用中碰到的一个奇怪的问题。

相信读者在看到上述代码时可能会提出疑问,为什么实现insertFill()方法的时候,填充数据用的是this.strictInsertFill()方法,而实现udpateFill()方法的时候,填充数据用的却是this.setFieldValByName(),为什么不用this.strictupdateFill()方法呢,原因就是使用this.strictInsertFill()和this.strictupdateFill()方法的时候,当当前设置的字段有值的时候,不会对其进行数据的填充,这对于插入数据的逻辑是正确的,但是修改数据就不对了,我们希望的是无论字段是否有值,都对该字段进行填充,例如更新时间这种字段,所以this.strictupdateFill()方法就满足不了我们的需求,需要改为this.setFieldValByName()来进行更新数据类型的数据填充。

为什么this.strictInsertFill()和this.strictupdateFill()这两个方法只能对字段值为空的数据进行填充可以参见下面MyBatisPlus的源码,这两个方法最终都会调用到这个方法,这个方法里首先根据字段名去拿了该字段的值,如果字段值为空就直接返回该对象,不设置值,如果为空,并且传入的fieldVal不为空,那就将字段值设置为我们传进去的入参!

default MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
        if (metaObject.getValue(fieldName) == null) {
            Object obj = fieldVal.get();
            if (Objects.nonNull(obj)) {
                metaObject.setValue(fieldName, obj);
            }
        }

        return this;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Introduction When I started this project, I had two requirements and I strived throughout the book to balance both of them. My first requirement comes from being an instructor and consultant for 10 years now. In that time, I have found a consistent void with most of my students and clients. It is not that clients are unwilling to implement new technologies. It is not that students are unable to learn about new technologies. The void is between those two. You learn about new technologies, but often the knowledge you gain does not provide a solid understanding of where in the network the new technology resides. You get design models, learn commands to turn features on and off, but you don’t know where to locate the device or why to implement a particular application or feature. For this reason, I have written this book in the form of a single case study that runs through the entire book. The case study revolves around a single, fictitious company that I created for the sole purpose of explaining where and why technologies should be placed in a real network. I hope that they do not become just objectives in a book for you to memo- rize. The Real World Scenarios are designed to trigger your thought process and allow you to find practical applications in your own networks. Speaking of objectives, this brings me to the second requirement for the book. That requirement is to fill a hole in having a single source of information, a place to learn about all of the common technologies used by network engineers today. To provide an outline for those common technologies, I used the objectives in place as of January 2009 for the Cisco Certified Network Professional (CCNP) certification. It would be difficult to cover every single objective from this certification track in one book, but you will find I have covered a vast majority of the objectives. My hope is that you will find this book a valuable supplemental guide in your studies as you endeavor to attain the coveted CCNP certification. The challenge was getting as many technologies into the book with enough detail so you would to know where and how to use them. There is not enough room in a single book to cover every possible solution or every single command and option you could use to accomplish a task. I do recommend some of the best and most common ways to accomplish the tasks. On that note, I hope that my coverage of wireless technologies in the last two chapters of the book will pique your interest in the exciting new technologies in wireless LANs. If you want a more in-depth fundamental look at how wireless networks operate and all of the fun, new toys (I mean wireless devices) that you can use to implement them, then watch for the new CCNA wireless book that Todd Lammle and I are currently writing for Sybex. Who Should Read This Book I highly recommend to anyone reading this book to have their CCNA certification or a firm understanding of the objectives and concepts covered. I put so many technologies into this one book, and covered as much of the CCNP material as possible that I didn’t have the space required to review all of the CCNA material. 83605book.indd 25 3/26/09 11:26:31 AM xxvi Introduction How to Use This Book This book not only covers many exciting and complex networking topics but shows you the steps required to design a full corporate internetwork. If you follow the chapters in order, I walk you not only through building single VLANs and subnets but through the security, voice, QoS, and wireless technologies you need to implement an entire campus network. How This Book Is Organized In Chapter 1, I provide for you an explanation of Cisco’s current design methodologies. This includes a discussion on Cisco’s Enterprise Composite Design Model and how that model has evolved over the years. Even a little bit about where it may go in the future. Following the design section of Chapter 1, I break down for you in detail what you can expect to accomplish in each chapter of the book and explain why I organized the book the way I did. After that, I describe for you the case study that is the framework for the book. This includes background of FutureTech, Inc., the network layout that the company has, and the technologies you are going to implement over the course of the book. You will be acting as the senior network engineer for the company (or the highly paid expert consultant that helps them through the process, if that sounds better to you). The last thing that I cover in Chapter 1 is the equipment and lab setup you can use to test and practice the technologies and topics you go through in the book. I will give you a breakdown of the topology that I will be using and supplemental equipment that can be used in exchange for the equipment that I have in my setup. With those details out of the way, I jump right into helping you build your network. Chapter 2 provides the lowdown on switching. Here you get a look at Layer 1 and Layer 2 functionality and access layer devices, creating a strong foundation from which to build the rest of the network. Then, I get into some Layer 3 functions with inter-VLAN routing. In Chapter 3, I walk you through controlling the topology and your connections. By the time you’ve finished Chapter 3 you will understand all of the functions of STP and how it prevents broadcast storms, multiple frame copies, and protects the stability of the MAC address table. In Chapters 4 through 7, you learn specifically about the routing process itself and how to give routers the information they require. I cover both static and dynamic routing protocols in depth, along with ways to filter and control the propagation of routing information between routers and routing domains. I also provide you with the means to verify and troubleshoot your network connections. Chapters 8 through 10 teach you about protocols and functions that make your net- work more reliable and efficient. In Chapter 8, I cover multicast. Here you learn what makes multicast work and see some of the configurations available to help you cope with increased use of applications and programs that send large amounts of data to a whole group of users. Continuing in this vein in Chapter 9, I give you the nuts and bolts of Inter- net Protocol version 6 (IPv6). In Chapter 10, I show you how to provide redundancy and load balancing features to your network using just your routers. You learn to configure and use HSRP, VRRP, and GLBP

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值