When adding new onessp_id_path
Automatic andid
To maintain consistency, it is necessary to handle the corresponding process in the entity class and insertion logic. MyBatis-Plus provides several ways to achieve this requirement:
Solution 1: Use MyBatis-Plus' autofill function
This is the most elegant solution, by implementingMetaObjectHandler
Interface, automatically set before insertionsp_id_path
Value:
import ; import ; import ; import ; @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { // Get the current entity object Object entity = (); // Only handle DsteSpConfigNew entity if (entity instanceof DsteSpConfigNew) { DsteSpConfigNew config = (DsteSpConfigNew) entity; // If sp_id_path is not set manually, it is set to the id value before insertion if (() == null) { // Note: The id may not have been generated yet and requires special processing // The following will provide specific implementation methods } } } @Override public void updateFill(MetaObject metaObject) { // Update the fill logic } }
Solution 2: Update sp_id_path after insertion
becauseid
It is a self-increment primary key, which cannot be obtained before insertion, and can be implemented in two steps:
- Insert the record first to get the generated
id
- Update now
sp_id_path
For thisid
@Service public class DsteSpConfigNewService { @Autowired private DsteSpConfigNewMapper configMapper; @Transactional public void saveConfig(DsteSpConfigNew config) { // 1. Insert the record, the id will be automatically generated at this time (config); // 2. Get the generated id Long id = (); // 3. Update sp_id_path to id value DsteSpConfigNew updateEntity = new DsteSpConfigNew(); (id); ((id)); (updateEntity); } }
Solution 3: Use database triggers (recommended)
Database-level triggers can be more reliable to ensuresp_id_path
andid
Consistency:
DELIMITER $$ CREATE TRIGGER after_dste_sp_config_new_insert AFTER INSERT ON dste_sp_config_new FOR EACH ROW BEGIN -- Will record the new sp_id_path Updated to id value UPDATE dste_sp_config_new SET sp_id_path = WHERE id = ; END$$ DELIMITER ;
This method does not require modifying Java code and is completely automatically processed by the database, but attention should be paid to the performance impact of triggers.
Entity configuration
Make sure that the entity class contains these two fields and use the correct annotation:
import ; import ; import ; @Data @TableName("dste_sp_config_new") public class DsteSpConfigNew { @TableId(type = ) private Long id; private String spName; private Long projectId; private String spIdPath; private String spNamePath; private Long parentSpId; private Integer spStatus; private LocalDateTime createAt; private LocalDateTime updateAt; private LocalDateTime deleteAt; }
This is the article about several ways to keep the values and ids of other fields when mybatisPlus are added. For more related content related to the values and ids of mybatisPlus, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!