This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

SelectThe query

// Test the query
@Test
public void testSelect(a) {
    // Query by id
    User user = userMapper.selectById(1L);
    System.out.println(user);

    // Batch query
    List<User> users = userMapper.selectBatchIds(Arrays.asList(2L.3L));
    users.forEach(System.out::println);

    // Query map conditions
    Map<String, Object> map = new HashMap<>();
    map.put("name"."zbc");
    List<User> users1 = userMapper.selectByMap(map);
    users1.forEach(System.out::println);
}
Copy the code
Preparing: SELECT ID,name,age,email,create_time,update_time,version FROM user WHERE ID =? ==> Parameters: 1(Long) // Batch query ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id IN ( ? ,?) ==> Parameters: 2(Long), 3(Long) SELECT id,name,age,email,create_time,update_time,version FROM user WHERE name = ? ==> Parameters: zbc(String)Copy the code

Paging query

implementation

  • The original paginglimit
  • Third-party plug-inspageHelper
  • MyBatisPlusBuilt-in paging

Using paging queries

  • Add a paging plug-in, which needs to be added before the optimistic lock plug-in
@Configuration
@EnableTransactionManagement
public class MyBatisPlusConfig {

    /** * to configure mybatisPlus plugin **@returnThe interceptor * /
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(a) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // Paging plug-in, note the type of database
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // Optimistic lock plugin
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        returninterceptor; }}Copy the code
  • Direct use ofPageObjects can be
// Test paging queries
@Test  
public void testPage(a) {  
    Parameter 1: page number, parameter 2: number of records per page
    Page<User> page = new Page<>(1.5);  
    userMapper.selectPage(page, null);  
    page.getRecords().forEach(System.out::println);  
}
Copy the code
Preparing: SELECT COUNT(*) AS total FROM user ==> Parameters: <== Columns: total <== Row: 12 <== total: Preparing: SELECT ID,name,age,email,create_time,update_time,version FROM user LIMIT? ==> Parameters: User(id=1, name=test2, age=25, [email protected], createTime=null, updateTime=Sun Nov 07 18:46:37 CST 2021, version=3) User(id=2, name=Jack, age=20, [email protected], createTime=null, updateTime=Sat Nov 06 22:28:53 CST 2021, version=1) User(id=3, name=Tom, age=28, [email protected], createTime=null, updateTime=Sat Nov 06 22:28:53 CST 2021, version=1) User(id=4, name=Sandy, age=21, [email protected], createTime=null, updateTime=Sat Nov 06 22:28:53 CST 2021, version=1) User(id=5, name=Billie, age=24, [email protected], createTime=null, updateTime=Sat Nov 06 22:28:53 CST 2021, version=1) 12Copy the code

deletedelete

// Test delete
@Test
public void testDelete(a) {
    // Delete a single ID
    userMapper.deleteById(1456795944390905863L);

    // Delete the batch ID
    userMapper.deleteBatchIds(Arrays.asList(1456795944390905861L.1456795944390905862L));

    // Delete map conditionally
    Map<String, Object> map = new HashMap<>();
    map.put("name"."123");
    userMapper.deleteByMap(map);
}
Copy the code
Preparing: DELETE FROM user WHERE ID =? Parameter: 1456795944390905863(Long) <== Updates: 1 // Preparing: DELETE FROM user WHERE id IN (? ,?) ==> Parameters: 1456795944390905861(Long), 1456795944390905862(Long) <== Updates: 2 // Condition delete map, ==> Preparing: DELETE FROM user WHERE name = ? ==> Parameters: 123(String) <== Updates: 1Copy the code

Logic to delete

  • Physical delete: Deletes the database directly
  • Logical deletion: It is not deleted from the database, but invalidated by a variable

Application scenario: Administrators can view deleted records to prevent data loss.

use

  • increasedeletedfield
ALTER TABLE `table_name`  
ADD COLUMN `deleted` INT(1) DEFAULT 1 COMMENT 'Logical delete';
Copy the code
  • The entity class adds attributes
@TableLogic  
private Integer deleted;
Copy the code
  • Added logical deletion configuration
# logical delete
TableLogic (since 3.3.0, @tablelogic)
mybatis-plus.global-config.db-config.logic-delete-field=flag
# Logical deleted value (default: 1)
mybatis-plus.global-config.db-config.logic-delete-value=1
# Logical undeleted value (default: 0)
mybatis-plus.global-config.db-config.logic-not-delete-value=0
Copy the code
  • test
// Test logical deletion
@Test
public void testLogicDelete(a){
    userMapper.deleteById(1456795944390905864L);
    User user = userMapper.selectById(1456795944390905864L);
    System.out.println(user);
}
Copy the code
==>  Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 1456795944390905864(Long)
<==    Updates: 1


==>  Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE id=? AND deleted=0
==> Parameters: 1456795944390905864(Long)
<==      Total: 0

null
Copy the code

Essentially an update operation is not a delete operation, but a modify operation. When we query, we find that we can no longer find it