= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

4. Disassemble CRUD in detail

Some important points to note before testing, such as adding the following code blocks to the YML configuration file as shown in the drop code. This will print SQL statements on the console for reference when testing:

//# print SQL statements on the console
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Copy the code
Methods in baseMapper: insert deleteById deleteByMap delete deleteBatchIds updateById update selectById selectBatchIds selectByMap selectOne selectCount selectList selectMaps selectObjs selectPage selectMapsPageCopy the code
/** ** paging query test */
@Test
public void selectPageLoads(a) {
    Page<User> page = new Page<>(1.5);
    IPage<User> lstUser = mapper.selectPage(page, null);//null represents a conditional constructor
    System.out.println("return selectPageLoads value = "+ lstUser); } To implement mybatis-plus paging first need to make sure that the paging plug-in configuration method is configured/** ** paging plug-in */
@Bean
public PaginationInterceptor paginationInterceptor(a) {
    return newPaginationInterceptor(); } Add the above beans to the configuration class or to the startup classCopy the code
BaseMapper source code is introduced to the basic CRUD:package com.baomidou.mybatisplus.core.mapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;

/ * * * < p > * Mapper after the interface inheritance, don't need to write Mapper. The XML file, can obtain the CRUD function * * < / p > < p > * this Mapper support id generic * < / p > * *@author hubin
 * @sinceThe 2016-01-23 * /
public interface BaseMapper<T> {

    /** * <p> * Insert a record * </p> **@paramEntity Entity object */
    int insert(T entity);/ /)

    /** * <p> * delete * </p> ** by ID@paramId Primary key ID */
    int deleteById(Serializable id);/ /)

    /** ** <p> * Drop the record * </p> ** according to the columnMap condition@paramColumnMap Table field map object */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);/ /)

    /** ** <p> * Delete record * </p> ** according to entity condition@paramThe queryWrapper entity object encapsulates the action class (which can be null) */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** ** <p> * delete (by ID) * </p> **@paramIdList List of primary key ids (cannot be null or empty) */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);/ /)

    /** * <p> * Changes according to ID * </p> **@paramEntity Entity object */
    int updateById(@Param(Constants.ENTITY) T entity);/ /)

    /** ** <p> * Updates the record * </p> ** according to the whereEntity condition@paramEntity Entity object (set conditional value, cannot be null) *@paramThe updateWrapper entity object encapsulates the action class (which can be null, where the entity is used to generate the WHERE statement) */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /** * <p> * query by ID * </p> **@paramId Primary key ID */
    T selectById(Serializable id);/ /)

    /** * <p> * query (by ID) * </p> **@paramIdList List of primary key ids (cannot be null or empty) */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);/ /)

    /** * <p> * Query (based on columnMap condition) * </p> **@paramColumnMap Table field map object */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);/ /)

    /** ** <p> * query a record based on the entity condition * </p> **@paramQueryWrapper entity object */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** ** <p> * Query the total number of records according to the Wrapper condition * </p> **@paramQueryWrapper entity object */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** ** <p> ** query all records according to entity condition * </p> **@paramThe queryWrapper entity object encapsulates the action class (which can be null) */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * <p> * According to the Wrapper condition, query all records * </p> **@paramThe queryWrapper entity object encapsulates the action class (which can be null) */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** * <p> * According to the Wrapper condition, query all records * Note: only return the value of the first field * </p> **@paramThe queryWrapper entity object encapsulates the action class (which can be null) */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /** ** <p> ** query all records according to entity condition * </p> **@paramPage paging query criteria (can be rowbound.default) *@paramThe queryWrapper entity object encapsulates the action class (which can be null) */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);/ /)

    /** ** <p> * According to the Wrapper condition, query all records (and turn pages) * </p> **@paramPage paging query criteria *@paramThe queryWrapper entity object encapsulates the action class */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}
Copy the code
The original link: https://blog.csdn.net/m0_37034294/article/details/82908527Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Powerful conditional constructors queryWrapper and updateWrapper

1. Introduction to conditional constructor relations

AbstractLambdaWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractWrapper: AbstractLambdaWrapper: Lambda syntax uses Wrapper to uniformly handle parsing Lambda to get column. LambdaUpdateWrapper: Lambda update Wrapper QueryWrapper: LambdaQueryWrapper: LambdaUpdateWrapper The Entity object encapsulates the action class instead of using the lambda syntax UpdateWrapper: Update condition for the Entity object Update operationCopy the code

Conditional constructor expressions

The original link: https://blog.csdn.net/m0_37034294/article/details/82917234Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Vi. Achieve multiple tables linked paging 3.X version

MP recommends using a PaginationInterceptor, a PaginationInterceptor, that is, a pagination plug-in that the Team has packaged itself

1. Configure the paging plug-in

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.lqf.multitable.dao.*")
public class MybatisPlusConfig {
    /** * Mybatis -plus SQL execution efficiency plugin */
    @Bean
    public PerformanceInterceptor performanceInterceptor(a) {
        return new PerformanceInterceptor();
    }

    /* * Paged plug-in, automatic identification of database type multi-tenant, please refer to the official website [plugin extension] */
    @Bean
    public PaginationInterceptor paginationInterceptor(a) {
        return newPaginationInterceptor(); }}Copy the code

Because in the case of multi-table joint lookup may appear to return different table fields, when the automatically generated table does not meet your return criteria, we need to automatically generate a return entity.

2. Create return entities

import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRoleVo {

    private Long userId;

    /** * create time */
    private LocalDateTime createTime;

    /** * Change the time */
    private LocalDateTime updateTime;

    /** * account */
    private String username;

    /** * Password */
    private String password;

    /** * name */
    private String realname;

    /** * Gender (0 for female and 1 for male) */
    private Integer sex;

    /** * Mobile phone number */
    private String mobile;

    /** * Password encryption string */
    private String passwordMd5;

    /**
     * 开账号人
     */
    private Long parentId;

    0 No 1 Yes */
    private Integer status;

    /** * Authorized region */
    private String authArea;

    /** * Authorized city */
    private String authCity;

    private Integer role;

    /** * Number of remaining cards */
    private Integer residueCardNumber;

    /** * Last login time */
    private LocalDateTime lastLoginTime;

    /** * Last login IP address */
    private String lastLoginIp;

    /**
     * 最后登录次数
     */
    private Integer lastLoginCount;

    /** * channel */
    private String authChannel;

    /** * 0 extranet 1 Intranet */
    private Integer internet;

    /** * gold */
    private Long goldCoin;

    private Long id;

    /** * state */
    private String statusId;

    /** * Role name */
    private String roleName;

    /** * Role value */
    private String roleValue;

    /** * Subordinate role value that can be added */
    private String addibleValue;
}
Copy the code

3

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqf.multitable.bean.crm.UserRoleVo;
import com.lqf.multitable.service.crm.FyUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisPlusMultiTableApplicationTests {

    @Autowired
    private FyUserService service;

    /** ** ** /
    @Test
    public void contextLoads(a) {
        // The current page, the total number of items to construct the page object
        Page<UserRoleVo> page = new Page<>(1.10); page.setRecords(service.selectUserListPage(page)); System.out.println(page); }}Copy the code

4, the service layer

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqf.multitable.bean.crm.FyUser;
import com.lqf.multitable.bean.crm.UserRoleVo;
import com.lqf.multitable.dao.crm.FyUserMapper;
import com.lqf.multitable.service.crm.FyUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/** * Service implementation class */
@Service
public class FyUserServiceImpl extends ServiceImpl<FyUserMapper.FyUser> implements FyUserService {

    @Override
    public List<UserRoleVo> selectUserListPage(Page<UserRoleVo> page) {
        return this.baseMapper.selectUserListPage(page); }}Copy the code

5, mapper layer

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqf.multitable.bean.crm.FyUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lqf.multitable.bean.crm.UserRoleVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/** * Mapper interface */
@Repository
public interface FyUserMapper extends BaseMapper<FyUser> {
    @Select("SELECT * FROM fy_user u LEFT JOIN fy_role r ON u.role = r.id")
    List<UserRoleVo> selectUserListPage(Page<UserRoleVo> pagination);
}
Copy the code

6. Test results

==> Preparing: SELECT COUNT(1) FROM fy_user u LEFT JOIN fy_role r ON u.role = r.id 
==> Parameters: 
<== Columns: COUNT(1)
<== Row: 391
==> Preparing: SELECT * FROM fy_user u LEFT JOIN fy_role r ON u.role = r.id LIMIT 0.10==> Parameters: <== Columns: user_id, create_time, update_time, username, password, realname, sex, mobile, password_md5, parent_id, status, auth_area, auth_city, role, residue_card_number, last_login_time, last_login_ip, last_login_count, auth_channel, Internet, gold_coin, ID, status_id, create_time, update_time, role_name, role_value, addible_value ********* <== Total:10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63c12e52]
com.baomidou.mybatisplus.extension.plugins.pagination.Page@5D2828c9 Time:61Ms - the ID: com. LQF. Multitable. Dao. CRM. FyUserMapper. SelectUserListPage Execute SQL: SELECT * FROM fy_user u LEFT JOIN fy_role r ON u.role = r.id LIMIT0.10
Copy the code

As can be seen from the test results, the total number of query items is 391, and the query time is 61ms after paging limit 0,10.

The original link: https://blog.csdn.net/m0_37034294/article/details/82935436Copy the code
Author: frog and Goose Source: CSDNCopy the code