Extension of MyBatis-Plus-Join multi-table query


website

Instructions

Install

  • Maven

    <dependency>
        <groupId>com.github.yulichang</groupId>
        <artifactId>mybatis-plus-join-boot-starter</artifactId>
        <version>1.4.5</version>
    </dependency>
    
  • Gradle

     implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.4.5'
    

    Or clone the code to execute mvn install locally, and then introduce the above dependencies

    Note: mybatis plus version >= 3.3.0

use

  • mapper inherits MPJBaseMapper (required)
  • service inherits from MPJBaseService (optional)
  • serviceImpl inherits from MPJBaseServiceImpl (optional)

Lambda form usage (MPJLambdaWrapper)

Simple join table query

class test {
    
    
    @Resource
    private UserMapper userMapper;

    void testJoin() {
    
    
        //和Mybatis plus一致,MPJLambdaWrapper的泛型必须是主表的泛型,并且要用主表的Mapper来调用
        MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
                .selectAll(UserDO.class)//查询user表全部字段
                .select(UserAddressDO::getTel)//查询user_address tel 字段
                .selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)//别名
                .select(AreaDO::getProvince, AreaDO::getCity)
                .leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId)
                .leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId)
                .eq(UserDO::getId, 1)
                .like(UserAddressDO::getTel, "1")
                .gt(UserDO::getId, 5);

        //连表查询 返回自定义ResultType
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);

        //分页查询 (需要启用 mybatis plus 分页插件)
        Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
    }
}

corresponding to sql

SELECT  
    t.id, t.name, t.sex, t.head_img, 
    t1.tel, t1.address AS userAddress,
    t2.province, t2.city 
FROM 
    user t 
    LEFT JOIN user_address t1 ON t1.user_id = t.id 
    LEFT JOIN area t2 ON t2.id = t1.area_id 
WHERE (
    t.id = ? 
    AND t1.tel LIKE ? 
    AND t.id > ?)

illustrate:

  • UserDTO.class query result return class (resultType)
  • selectAll() Query all fields of the specified entity class
  • select() Query the specified fields, support variable parameters, the same select can only query the fields of the same table
  • selectAs() Field alias query, used when the database field is inconsistent with the property name of the business entity class
  • leftJoin() Parameter description
    The first parameter: the entity class class participating in the joining table
    The second parameter: the ON field of the joining table, this attribute must be the attribute of the first parameter entity class
    The third parameter: the ON participating in the joining table Another entity class property of
  • The default main table alias is t, other table aliases use t1, t2, t3...
  • Conditional query, you can query the fields of the main table and all tables participating in the connection, all call the native method of mp, there is no risk of sql injection in normal use

one-to-many query

class test {
    
    
    @Resource
    private UserMapper userMapper;

    @Test
    void testResultMap() {
    
    
        MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<>(User.class)
                .selectAll(UserDO.class)
                //对多查询
                .selectCollection(AddressDO.class, UesrDTO::getAddressList)
                //对一查询
                .selectAssociation(AddressDO.class, UesrDTO::getAddress)
                .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId);

        List<UserDTO> dtoList = userMapper.selectJoinList(UserDTO.class, wrapper);

        //关于对多分页查询
        //由于嵌套结果方式会导致结果集被折叠,因此分页查询的结果在折叠后总数会减少,所以无法保证分页结果数量正确。
    }
}

MPJLambdaWrapper other functions

Guess you like

Origin blog.csdn.net/xiaohuihui1400/article/details/131996095