Five, MyBatis-Plus when the "ActiveRecord pattern" and "SimpleQuery tool class" (detailed practice)
@
- Five, MyBatis-Plus when the "ActiveRecord pattern" and "SimpleQuery tool class" (detailed practice)
- 1. ActiveRecord mode
-
2. Introduction to ActiveRecord
- 2.1 ActiveRecord Implementation
-
3. SimpleQuery Toolkit
- 3.1 Introduction to SimpleQuery
- 3.2 list
- 3.3 map
- 3.4 Group
- 4. Finally:
1. ActiveRecord mode
2. Introduction to ActiveRecord
ActiveRecord (AR), is a domain modeling pattern characterized by a model class corresponding to a table in a relational database, and an instance of the model class corresponding to a row of records in the table.ActiveRecord, which has been popular with interpreted dynamic languages (PHP, Ruby, etc.), is used for CRUD operations by wrapping a data object around an operations around a data object. Java as a quasi-static (compiled language), for ActiveRecord often can only sigh its elegance , so MP also in the AR road to explore some , just need to let the entity class inherit the Model class and the implementation of the primary key specification method , you can start the AR journey .
2.1 ActiveRecord Implementation
Next, let's look at the steps for implementing ActiveRecord
To use the ActiveRecord schema, you need to make the corresponding entity class (Java Bean)extends Model
Class.
We can see that the Model class provides some additions, deletions, modifications and checks, in which case we can directly use the entity class object to call these additions, deletions, modifications and checks, which simplifies the syntax of the operation, but his bottom layer still needs UserMapper, so the persistence layer interface can not be omitted.
Testing ActiveRecord Schema additions and deletions.
Add Data
import ;
import ;
import ;
@SpringBootTest
public class ActiveRecordTest {
//public class User extends Model<User>, Requires Inheritance extends Model<User>
// Add operation
@Test
void activeRecordAdd() {
User user = new User();
("zhang");
(28);
("zhang@");
();
}
}
Delete data
import ;
import ;
import ;
@SpringBootTest
public class ActiveRecordTest {
//public class User extends Model<User>, Requires Inheritance extends Model<User>
// Delete operation
@Test
void activeRecordDelete() {
User user = new User();
("1837781440184680449");
();
}
}
Modify data
import ;
import ;
import ;
@SpringBootTest
public class ActiveRecordTest {
//public class User extends Model<User>, Requires Inheritance extends Model<User>
// Modify operation
@Test
void activeRecordUpdate() {
User user = new User();
("3");
(1);
();
}
}
Query Data
import ;
import ;
import ;
@SpringBootTest
public class ActiveRecordTest {
//public class User extends Model<User>, Requires Inheritance extends Model<User>
// query operation
@Test
void activeRecordSelect() {
User user = new User();
("7");
User result = ();
(result);
}
}
3. SimpleQuery Toolkit
3.1 Introduction to SimpleQuery
SimpleQuery can encapsulate the result of a selectList query with a Stream, so that it can return some specified results, which simplifies the api call.
3.2 list
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testList() {
List<String> list = (new LambdaQueryWrapper<User>().eq(User::getName, "Mary"), User::getId);
(list);
}
}
Perform lambda operations on the encapsulated fields.
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testList2() {
List<String> list = (new LambdaQueryWrapper<User>().eq(User::getName, "Mary"), User::getName,
new Consumer<User>() {
@Override
public void accept(User user) {
(()).map(String::toLowerCase).ifPresent(user::setName);
}
});
(list);
}
}
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
// utilizationlabdisplayed formula
@Test
void testList3() {
List<String> list = (new LambdaQueryWrapper<User>().eq(User::getName, "Mary"), User::getName,
user -> (()).map(String::toLowerCase).ifPresent(user::setName));
(list);
}
}
3.3 map
Encapsulate all objects as a Map collection in terms of ids and entities.
、
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testMap() {
Map<String, User> map = (new LambdaQueryWrapper<User>(), User::getId);
(map);
}
}
Encapsulate a single object as a Map collection in terms of id, entity
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testMap2() {
Map<String, User> map = (new LambdaQueryWrapper<User>().eq(User::getId, 1L), User::getId);
(map);
}
}
You only want a map consisting of id and name.
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testMap3() {
Map<String, String> map = (new LambdaQueryWrapper<User>(), User::getId, User::getName);
(map);
}
}
3.4 Group
Group is a simple grouping effect.
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
public class SimpleQueryTest {
@Resource
private UserMapper userMapper;
@Test
void testGroup() {
Map<String, List<User>> map = (new LambdaQueryWrapper<User>(), User::getName);
(map);
}
}
4. Finally:
"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."