Home > Back-end >  Mybatis - plus automatic filling is how to match the class attribute createTime and field names crea
Mybatis - plus automatic filling is how to match the class attribute createTime and field names crea

Time:12-27

 @ Data 
Public class User {
private Long id; Don't write Long Long//pit
private String name;
Private Integer age;
Private String email;
@ TableField (fill=FieldFill. INSERT)
Private Date createTime;
@ TableField (fill=FieldFill INSERT_UPDATE)
private Date updateTime;
}

 @ Slf4j 
@ Component
Public class CustomMetaObjectHandler implements MetaObjectHandler {
@ Override
Public void insertFill (MetaObject MetaObject) {
The log. The info (" start insert the fill... ");
Enclosing setFieldValByName (" createTime ", new Date (), metaObject);
Enclosing setFieldValByName (" updateTime ", new Date (), metaObject);
}

@ Override
Public void updateFill (MetaObject MetaObject) {
The info (" start update the fill... ");
Enclosing setFieldValByName (" updateTime ", new Date (), metaObject);
}
}
 @ Test 
Public void testInsert () {
User user=new User();
User. SetAge (18);
User. Elegantly-named setName (" yx ");
User. SetEmail (" [email protected] ");
Int result=userMapper. Insert (user);
System. The out. Println (" affect the number of lines: "+ result);
System. The out. Println (" id: "+ user);
}

 Creating a new SqlSession 
SqlSession [org. Apache. Ibatis. Session. Defaults. DefaultSqlSession @ f2276c9] was not registered for synchronization because synchronization is not active
15:51:32 2020-12-26. 8336-964 the INFO [main]. Com myd. Handler. CustomMetaObjectHandler: start insert the fill...
The 2020-12-26 15:51:32. 8336-970 the INFO [main] com. Zaxxer. Hikari. HikariDataSource: HikariPool - 1 - Starting...
The 15:51:33 2020-12-26. 8336-159 the INFO [main] com. Zaxxer. Hikari. HikariDataSource: HikariPool - 1 - Start completed.
The JDBC Connection [HikariProxyConnection @ 1343838143 wrapping com. Mysql. Cj. JDBC. ConnectionImpl @ 5 d1d9d73] will not be managed by the Spring
==& gt; Preparing: INSERT INTO user (id, name, age, email, create_time, update_time) VALUES (?,?,?,?,? ,? ,? ,? ,? ,? )
==& gt; The Parameters: 1342739812944273410 (Long), yx (String), 18 (Integer), [email protected] (String), the 2020-12-26 15:51:32. 964 (Timestamp), the 2020-12-26 15:51:32. 964 (Timestamp)
<1
==Updates:Closing non transactional SqlSession [org. Apache. Ibatis. Session. Defaults. DefaultSqlSession @ f2276c9]
Impact of rows: 1
Id: the User (id=1342739812944273410, name=yx, age=18, [email protected], createTime=Sat Dec 26 15:51:32 CST 2020, updateTime=Sat Dec 26 15:51:32 CST 2020)
The 2020-12-26 15:51:33. 8336-226 the INFO [extShutdownHook] com. Zaxxer. Hikari. HikariDataSource: HikariPool - 1 - Shutdown initiated...
The 2020-12-26 15:51:33. 8336-232 the INFO [extShutdownHook] com. Zaxxer. Hikari. HikariDataSource: HikariPool - 1 - Shutdown completed.

Mybatis - plus perform insert statement is how I knew my database field is create_time, update_time, how determined createTime attribute of the user class with the database field name create_time match? O great god answer
  • Related