Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
The update operation
The update to update
@test public void testUpdate(){User User = new User(); user.setId(6L); User.setname (" XXX is awesome!" ); // Note: updateById, but the argument is an object int result = usermapper.updateByid (user); System.out.println(result); System.out.println(user); }Copy the code
Automatic concatenation SQL
All SQL is automatically generated
Automatic filling
Create time, modify time! This is all done automatically and we don’t want manual updates!
Mode 1 Data Level (not recommended)
1. Add fields create_time and update_time in the table
Remember to choose the default value
2. To test the insert method again, we need to synchronize the entity class first
@tableId (type = idType.input) private Long ID; private String name; private Integer age; private String email; private Date create_time; private Date update_time;Copy the code
3. Update the results again
Approach 2: Code level (recommended)
1, delete database default value, update operation!
2. Annotate the fields of the entity class
@tableField (fill = FieldFill.INSERT) private Date create_time; @TableField(fill = FieldFill.INSERT_UPDATE) private Date update_timeCopy the code
3. Write a processor to handle this annotation!
@slf4j public class MyMetaObjectHandler implements MetaObjectHandler {// Implements an insert population policy @Override public void insertFill(MetaObject metaObject) { log.info("start insert fill...." ); this.setFieldValByName("create_time",new Date(),metaObject); this.setFieldValByName("update_time",new Date(),metaObject); } @override public void updateFill(MetaObject MetaObject) {log.info("start update fill....") ); this.setFieldValByName("update_time",new Date(),metaObject); }}Copy the code
## Tip You will inevitably encounter this annotation in your development@Component
@ControllerThe controller (injection service) is used to annotate the control layer, which is equivalent to the Action layer in Struts@ServiceServices (injected DAOs) are used to annotate the service layer and are primarily used for the logical processing of business@RepositoryUsed to annotate the data access layer, or the data access component, the DAO component.@Component(Instantiating regular POJos into the Spring container is equivalent to a configuration file) refers to components in general, meaning when our class doesn't belong to any of the categories@Controller,@ServicesWhile we wait), we can use it@ComponentTo label the class.Copy the code