This is the 16th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

The new Version of MyBatis-Plus 3.0, based on JDK8, provides lambda calls. Using lambda expressions, you can use entity field names by means of method references, so as to avoid miswriting database table field names.

Notes:

When using, be sure to pay attention to the design of the field name is standard. Field names starting with is GET are not allowed. MyBatis-Plus is responsible for an error when compiling lambda expressions

1. The way to create Lambda expressions

1.1 Basic Operations

  1. LambdaQueryWrapper < T >

  2. QueryWrapper< Entity >().lambda() method

  3. Wrappers.< entities >lambdaQuery() method

/ * * * constructor lambda conditions * * of the generated SQL statement to SELECT id, name, age, email, manager_id, create_time the FROM user WHERE the name LIKE? AND age < ? * /
@Test
public void selectLambda01(a) {
    /* Three ways to create a lambda conditional constructor */ 
    // LambdaQueryWrapper<User> lambda1 = new LambdaQueryWrapper<>();
    // LambdaQueryWrapper<User> lambda2 = new QueryWrapper<User>().lambda();
    LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
    lambda3.like(User::getName, "Rain").lt(User::getAge, 40);
    List<User> users = userMapper.selectList(lambda3);
    users.forEach(System.out::println);
}
Copy the code
/** * SELECT name like '%' and (age <40 or email in not NULL) * SELECT name like' %' and (age <40 or email in not null id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ? * /
@Test
public void selectLambda02(a) {
    LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
    lambda3.likeRight(User::getName, "The king").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));
    List<User> users = userMapper.selectList(lambda3);
    users.forEach(System.out::println);
}
Copy the code

1.2 Customizing SQL Statements

Annotation (not recommended for individuals)

/** * custom SQL statements using * Dao(Mapper) layer code: *@Select("select * from ${ew.customSqlSegment}")
 * List<User> selectAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper); * /
@Test
public void selectLambda03(a) {
    LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
    lambda3.likeRight(User::getName, "The king").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));

    List<User> users = userMapper.selectAll(lambda3);
    users.forEach(System.out::println);
}
Copy the code

Custom SQL statements: XML mode, XML mode custom SQL here is just a single demonstration application. Yml added

mybatis-plus:  
	mapper-locations:    
		- com/mp/mapper/*
Copy the code

Persistence layer interface code, UserMapper interface adding method

List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
Copy the code

Map file userpper. XML code


      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mp.dao.UserMapper">
    <select id="selectAll" resultType="com.mp.entity.User">
        select * from user ${ew.customSqlSegment}
    </select>
</mapper>
Copy the code

2.Entities LambdaQueryChainWrapper < > (xxxxMapper)way

/ * * * constructor lambda conditions a fourth way create new way of MP 3.0.7 * of the generated SQL statements SELECT id, name, age, email, manager_id, create_time the FROM user WHERE the name  LIKE ? AND age >= ? * this example can be seen that the code more concise * generated SQL statements SELECT id, name, age, email, manager_id, create_time the FROM user WHERE the name LIKE? AND age >= ? * /
@Test
public void selectLambda04(a) {
    List<User> users = new LambdaQueryChainWrapper<User>(userMapper)
            .like(User::getName, "Rain").ge(User::getAge, 20).list();
    users.forEach(System.out::println);
}
Copy the code

3. The use of lambda in paging

3.1 MP paging operation

The BaseMapper interface provides two methods for physical paging.

The first return entity object allows null

The second person to return a map object is used to specify a field to put back, to avoid null values for the specified field

IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
Copy the code

3.2 Paging Needs to be configured with interceptors

@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(a) {
        return newPaginationInterceptor(); }}Copy the code

3.3 Test Code

/** * paged MP paged plugin */
@Test
public void selectPage(a) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
    queryWrapper.ge("age".26);
    // False indicates that the total number of records is not queried
    Page<User> userPage = new Page<>(1.2.false);
  /* IPage
      
        iPage = userMapper.selectPage(userPage, queryWrapper); System.out.println(" total page "+iPage. GetPages ()); System.out.println(" total number of records "+iPage.getTotal()); List
       
         records = iPage.getRecords(); records.forEach(System.out::println); * /
       
    IPage<Map<String, Object>> iPage = userMapper.selectMapsPage(userPage, queryWrapper);
    System.out.println("Total pages" + iPage.getPages());
    System.out.println("Total number of records" + iPage.getTotal());
    List<Map<String, Object>> records = iPage.getRecords();

    records.forEach(System.out::println);

}
Copy the code

3.4 Lambda paging tests

  • Define the UserMapper interface
IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER)Wrapper<User> wrapper);
IPage<Map<String, Object>> selectUserMapperPage(Page<Map<String, Object>> page,@Param(Constants.WRAPPER)Wrapper<User> wrapper);
Copy the code
  • Define the userpper. XML file
<select id="selectUserPage" resultType="com.mp.entity.User">
    select * from user ${ew.customSqlSegment}
</select>
<select id="selectUserMapperPage" resultType="map">
    select * from user ${ew.customSqlSegment}
</select>
Copy the code
  • The test code
/** * Custom paging method */
@Test
public void selectPage01(a) {
    LambdaQueryWrapper<User> qw = Wrappers.<User>lambdaQuery();
    qw.ge(User::getAge, 18);
    Page<User> page = new Page<>(1.2);
    Page<User> iPage = userMapper.selectUserPage(page, qw);
    System.out.println("Total pages:" + iPage.getPages());
    System.out.println("Total:" + iPage.getTotal());
    System.out.println("Number of pages:" + iPage.getSize());
    System.out.println(Page number: + iPage.getCurrent());
    List<User> records = iPage.getRecords();
    records.forEach(System.out::println);
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    Page page1 = new Page(1.2);
    IPage<Map<String, Object>> iPage1 = userMapper.selectUserMapperPage(page1, qw);
    System.out.println("Total pages:" + iPage1.getPages());
    System.out.println("Total:" + iPage1.getTotal());
    System.out.println("Number of pages:" + iPage1.getSize());
    System.out.println(Page number: + iPage1.getCurrent());
}
/** * Total number of paging not queried */
@Test
public void selectPage02(a) {
    LambdaQueryWrapper<User> qw = Wrappers.<User>lambdaQuery();
    qw.ge(User::getAge, 20);
    Page<User> page = new Page<>(1.2.false);
    Page<User> iPage = userMapper.selectUserPage(page, qw);
    List<User> users = iPage.getRecords();
    users.forEach(System.out::println);
}
Copy the code