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 to use @ TableField to automatically fill in dates in Mybatis-Plus

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "Mybatis-Plus how to use @ TableField to automatically fill dates". The content is detailed, the steps are clear, and the details are handled properly. I hope this "Mybatis-Plus how to use @ TableField to achieve automatic date filling" article can help you solve your doubts.

I. Preface

We often use ORM frameworks, such as Mybatis, tk.Mybatis, and Mybatis-Plus, in our daily development. But the most widespread is Mybatis-Plus. Some of our tables have creation time, update time, creator, and update person.

Com.baomidou mybatis-plus-boot-starter 3.5.1 3. Create entity class @ Datapublic class Test implements Serializable {private static final long serialVersionUID = 337361630075002456L; @ TableId (type= IdType.ASSIGN_ID) private long id; private String name; private String gender; private String phone; @ TableField (value = "create_date", fill = FieldFill.INSERT) private LocalDateTime createDate @ TableField (value = "update_date", fill = FieldFill.UPDATE) private LocalDateTime updateDate;} IV. Custom implementation class MyMetaObjectHandler@Slf4j@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler {@ Override public void insertFill (MetaObject metaObject) {log.info ("start insert fill.."); this.strictInsertFill (metaObject, "createDate", LocalDateTime.class, LocalDateTime.now ()) } @ Override public void updateFill (MetaObject metaObject) {log.info ("start update fill...."); this.strictUpdateFill (metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now ());}} V. Controller Test @ RestController@RequestMapping ("/ test") @ Slf4j@RequiredArgsConstructorpublic class TestController {@ NonNull private TestMapper testMapper; @ GetMapping ("/ insert") public Result insert (@ RequestBody Test test) {testMapper.insert (test) Return Result.success ("insert successfully");} @ PutMapping ("/ update") public Result update (@ RequestBody Test test) {testMapper.updateById (test); return Result.success ("insert successfully");}}

Friendly hint: this @ RequiredArgsConstructor note is not clear to read my article! -- > @ RequiredArgsConstructor comment

VI. Test add

Request address: localhost:8089/test/insert request content:

{"name": "Wang", "gender": "male", "phone": "123"}

We can see that without entering a date, it is automatically created!

VII. Test updates

Request address: localhost:8089/test/update request content:

{"id": 14, "name": "Wang", "gender": "male", "phone": "123"}

After reading this, the article "how Mybatis-Plus uses @ TableField to automatically fill in dates" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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