Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How MyBatisPlus handles the default fill time using @ TableField annotations

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how MyBatisPlus uses the @ TableField annotation to handle the default fill time. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Use the @ TableField annotation to handle the default fill time

Create time, modify time in the table of the database! These operations are generally automated, and we don't want to update them manually.

Alibaba development manual: all database tables: gmt_create,gmt_modified almost all tables should be configured! And it needs automation!

1. Fill in the default time using the database

Add a new field create_time,update_time to the table and set a current_timestamp timestamp for this field, so that the default value is the current time, but the mysql version does not support the current_timestamp keyword if it is too low

As shown below:

two。 When you insert or update a database using the auto-fill default fill

The time involved in writing the processor class in the table

First you need to write a processor, as shown in the following figure:

Note that the above processor must be annotated with @ Component to indicate that the processor is injected into the IOC container. In SpringBoot, both the processor package handler and the configuration package config need to add @ Component annotations to indicate that the classes are injected into the IOC container, otherwise the processor and configuration classes will not take effect.

Add @ TableField annotations to attributes of entity classes

Second, add the @ TableField annotation to the attributes on the corresponding entity class

As shown below:

test

Finally, write a unit test to test whether the automatic filling of createTime and updateTime can be realized when updating or inserting.

As shown below:

The result status of a successful test

The field type in the database should be set to datetime, and the data format after being successfully added

As shown below:

Type setting

The time fields create_time and update_time in the table in the database are set to the datetime type, and createTime and updateTime in the User entity and new Date () in the processor are all time types using the Date type in the java.util.Date package.

Thank you for reading! This is the end of this article on "how MyBatisPlus uses @ TableField annotations to deal with the default fill time". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report