1. Primary key policy
@tabelid (type = idtype.assign_id)
- ASSIGN_ID is adopted by twitter
Snowflakes algorithm
, a randomly generated 19-bit ID - AUTO Indicates the AUTO increase policy
2. Automatic filling
@ TableField (FieldFill. INSERT)
- Create time can use this
@ TableField (FieldFill INSERT_UPDATE)
- Change the time with this
Label the class @compoent
Usage:
-
Create a handler package, create a class and implement the MetaObjectHandler interface
-
There are two methods in the interface
-
InsertFill (MetaObject) This method is called when an operation is added
- Call the method this.setFieldValByName(“createTime”, new Date(), metaObject)
- Call the method this.setFieldValbyName (“updateTime”, new Date(), metaObject)
-
Update (MetaObject) This method is invoked when MetaObject is performing modification operations
- Call the method this.setFieldValbyName (“updateTime”, new Date(), metaObject)
-
3. Optimistic locking
Add the Version field, which defaults to 1
- The first session
Compares the current modified data version with the database version
After the operation is complete, the data version number is updated from 1 to 2
- The second session
If you get an incorrect version number, the operation fails
3.1 Code Implementation
-
Add a field to the table as the version number, and add attributes to the corresponding entity class
-
Add @vesion and @tablefeild (fieldfill.insert) to the attributes of the entity class
-
Added again in the insertFill method of the interface
- this.setFieldValByName(“version”, 1, metaObject)
-
Add Configuration classes to the Configuration package and add @Configuration and @mapperscan (” “)
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(a){
return new OptimisticLockerInterceptor();
}
Copy the code
4. Query operations
4.1 Batch Querying Multiple ids
List list = Mapper.selectBatchIds(Arrays.asList(1.2.3))
// Query information based on ids 1, 2, and 3
Copy the code
4.2 Querying Information Based on Conditions
new HashMap();
map.put("name"."fq")
map.put("age".20)
Mapper.selectByMap(map);
Copy the code
4.3 Paging Query
Add the plug-in
Write paging code
- Plugin Page object, passing in two parameters (current Page, record Page per Page)
- Call mp method for paging
Page<Fq> page = new Page(1.3);
Page<Fq> fqPage = Mapper.selectPage(page,null);
long Pages = fqPage.getPages();/ / the total number of pages
long current = fqPage.getcurrent();/ / the current page
List<Fq> recodes = fqPage.getRecords();// Query data set
Long total = fqPage.getTotal();// Total number of records
boolean hasNext = fqPage.hasNext();// Is there a next page
boolean hasPrevious = fqPage.hasPrevious();// check if there is a previous page
Copy the code
5. Delete the vm
5.1 Logical Deletion
Add a field to the table as a logical drop flag, changing the flag bit each time it is deleted
- 0 indicates that the data is not deleted
- 1 indicates logical deletion
5.2 Logical Deletion Operations
- Add fields to the table
delete
- Add attributes to the entity class
delete
And add @tablelogic and @tablefeild (fill = FieldFill.INSERT) - Add the initial value this.setFieldValByName(“delete”, 0, metaObject) to insertFill in Hanlder
The update method is used underneath