The ActiveRecord mode of mybatisPlus and the use of SimpleQuery

Table of contents

Introduction to ActiveRecord

ActiveRecord implementation 

Introduction to SimpleQuery

SimpleQuery implementation

list

map 

Group 


Introduction to ActiveRecord

ActiveRecord (Active Record, referred to as AR), is a domain model pattern, characterized in that a model class corresponds to a table in a relational database, and an instance of the model class corresponds to a row of records in the table. ActiveRecord has been widely loved by interpreted dynamic languages ​​(PHP, Ruby, etc.), by performing CRUD operations around a data object. As a quasi-static (compiled) language, Java can only admire its elegance for ActiveRecord, so MP has also carried out some exploration on the road of AR. It only needs to let the entity class inherit the Model class and implement the method of specifying the primary key to open it. AR tour.

illustrate:

  • Entity classes only need to inherit the Model class to perform powerful CRUD operations
  • BaseMapper that needs to be injected into the corresponding entity in the project

ActiveRecord mode CRUD
must have the corresponding original mapper and inherit baseMapper and can use this AR mode!!! 

ActiveRecord implementation 

Next, let's take a look at the implementation steps of ActiveRecord

1 】Let the entity class inherit the Model<User> class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User extends Model<User> {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

We can see that the Model class provides some methods for adding, deleting, modifying and checking, so that we can directly use entity class objects to call these adding, deleting, modifying and checking methods, which simplifies the syntax of the operation, but its bottom layer still needs UserMapper. So the persistence layer interface cannot be omitted

[2] Test the addition, deletion, modification and query of ActiveRecord mode

adding data

@Test

  void activeRecordAdd(){

    User user = new User();

    user.setName("wang");

    user.setAge(35);

    user.setEmail("[email protected]");

    user.insert();

}

delete data

@Test
void activeRecordDelete(){
    User user = new User();
    user.setId(8L);
    user.deleteById();
}

change the data

@Test
void activeRecordUpdate(){
    User user = new User();
    user.setId(6L);
    user.setAge(50);
    user.updateById();
}

 Query data

@Test
void activeRecordSelect(){
    User user = new User();
    user.setId(6L);
    User result = user.selectById();
    System.out.println(result);
}

Introduction to SimpleQuery

SimpleQuery can encapsulate the result of selectList query with Stream, so that it can return some specified results, which simplifies the call of API

SimpleQuery implementation

list

    @SafeVarargs
    public static <E, A> List<A> list(
    LambdaQueryWrapper<E> wrapper, 
    SFunction<E, A> sFunction, 
    boolean isParallel, 
    Consumer<E>... peeks) {
        return list2List(Db.list(wrapper.setEntityClass(getType(sFunction))), sFunction, isParallel, peeks);
    }

Params:
wrapper – conditional constructor
sFunction – required column
isParallel – whether parallel flow
peeks – follow-up operation

Demonstrates field-based encapsulation of collections

@Test
void testList2(){
    List<String> names = SimpleQuery.list(
    new LambdaQueryWrapper<User>().eq(User::getName, "Mary"),
    User::getName,
    e -> Optional.of(e.getName()).map(String::toLowerCase).ifPresent(e::setName));
    System.out.println(names);
}

map 

    @SafeVarargs
    public static <E, A, P> Map<A, P> map
    (LambdaQueryWrapper<E> wrapper, 
    SFunction<E, A> keyFunc, 
    SFunction<E, P> valueFunc, 
    boolean isParallel,
     Consumer<E>... peeks) 
    {
        return list2Map(Db.list(wrapper.setEntityClass(getType(keyFunc))), keyFunc, valueFunc, isParallel, peeks);
    }

Pass in Wrappers and keys, query the corresponding list from the database according to the conditions, and encapsulate them into a Map
Params:
wrapper – conditional constructor
keyFunc – key
valueFunc – value
isParallel – whether to stream

peeks in parallel – subsequent operations that may be required when encapsulating into a map, No need to pass

Demonstrate that all objects are encapsulated as a Map collection in the form of id and entity

@Test
void testMap(){
    //将所有元素封装为Map形式
    Map<Long, User> idEntityMap = SimpleQuery.keyMap(
            new LambdaQueryWrapper<>(), User::getId);
    System.out.println(idEntityMap);
}

Demonstrate the encapsulation of a single object as a Map collection in the form of id and entity

@Test
void testMap2(){
    //将单个元素封装为Map形式
    Map<Long, User> idEntityMap = SimpleQuery.keyMap(
            new LambdaQueryWrapper<User>().eq(User::getId,1L), User::getId);
    System.out.println(idEntityMap);
}

 The demo only wants a map composed of id and name

@Test
void testMap3(){
    //只想要只想要id和name组成的map
    Map<Long, String> idNameMap = SimpleQuery.map(
            new LambdaQueryWrapper<>(), User::getId, User::getName);
    System.out.println(idNameMap);
}

Group 

Demo grouping effect

@Test
void testGroup(){
    Map<String, List<User>> nameUsersMap = SimpleQuery.group(
                new LambdaQueryWrapper<>(), User::getName);
    System.out.println(nameUsersMap);
}

 The same name is a group, the name is used as the key of the map, and the same name is placed in a List collection as the value

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/131906399