MyBatis-Plus實現連表查詢的方法實例

mybatis-plus-join

  • gitee
  • github

使用方法

安裝

在項目中添加依賴

<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join</artifactId>
    <version>1.2.2</version>
</dependency>

或者clone代碼到本地,執行mvn install,再引入以上依賴

註意: mybatis plus version >= 3.4.0

使用

  • mapper繼承MPJBaseMapper (必選)
  • service繼承MPJBaseService (可選)
  • serviceImpl繼承MPJBaseServiceImpl (可選)

核心類 MPJLambdaWrapper和MPJQueryWrapper

MPJLambdaWrapper用法

MPJLambdaWrapper示例

簡單的3表查詢

class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class,
                new MPJLambdaWrapper<UserDO>()
                        .selectAll(UserDO.class)
                        .select(UserAddressDO::getTel)
                        .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));
    }
}

對應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 > ?)

說明:

  • UserDTO.class 查詢結果返回類(resultType)
  • selectAll() 查詢指定實體類的全部字段
  • select() 查詢指定的字段,支持可變參數,同一個select隻能查詢相同表的字段
  • 故將UserAddressDO和AreaDO分開為兩個select()
  • selectAs() 字段別名查詢,用於數據庫字段與業務實體類屬性名不一致時使用
  • leftJoin() 參數說明
  • 第一個參數: 參與連表的實體類class
  • 第二個參數: 連表的ON字段,這個屬性必須是第一個參數實體類的屬性
  • 第三個參數: 參與連表的ON的另一個實體類屬性
  • 默認主表別名是t,其他的表別名以先後調用的順序使用t1,t2,t3…
  • 條件查詢,可以查詢主表以及參與連接的所有表的字段,全部調用mp原生的方法,正常使用沒有sql註入風險

分頁查詢

class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        IPage<UserDTO> iPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class,
                new MPJLambdaWrapper<UserDO>()
                        .selectAll(UserDO.class)
                        .select(UserAddressDO::getTel)
                        .selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)
                        .select(AreaDO::getProvince, AreaDO::getCity)
                        .leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId)
                        .leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId));
    }
}

對應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
LIMIT ?,?

MPJQueryWrapper

簡單的3表查詢

class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class,
                new MPJQueryWrapper<UserDO>()
                        .selectAll(UserDO.class)
                        .select("addr.tel", "addr.address", "a.province")
                        .leftJoin("user_address addr on t.id = addr.user_id")
                        .rightJoin("area a on addr.area_id = a.id")
                        .like("addr.tel", "1")
                        .le("a.province", "1"));
    }
}

對應sql

SELECT 
    t.id,
    t.name,
    t.sex,
    t.head_img,
    addr.tel,
    addr.address,
    a.province
FROM 
    user t
    LEFT JOIN user_address addr on t.id = addr.user_id
    RIGHT JOIN area a on addr.area_id = a.id
WHERE (
    addr.tel LIKE ?
    AND a.province <= ?)

說明:

  • UserDTO.class 查詢結果類(resultType)
  • selectAll(UserDO.class) 查詢主表全部字段(主表實體類)默認主表別名 “t”
  • select() mp的select策略是覆蓋,以最後一次為準,這裡的策略是追加,可以一直select
  • 主表字段可以用lambda,會自動添加表別名,主表別名默認是 t ,非主表字段必須帶別名查詢
  • leftJoin() rightJoin() innerJoin() 傳sql片段 格式 (表 + 別名 + 關聯條件)
  • 條件查詢,可以查詢主表以及參與連接的所有表的字段,全部調用mp原生的方法,正常使用沒有sql註入風險

分頁查詢

class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        IPage<UserDTO> page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
                new MPJQueryWrapper<UserDO>()
                        .selectAll(UserDO.class)
                        .select("addr.tel", "addr.address")
                        .select("a.province")
                        .leftJoin("user_address addr on t.id = addr.user_id")
                        .rightJoin("area a on addr.area_id = a.id"));
    }
}

對應sql

SELECT 
    t.id,
    t.name,
    t.sex,
    t.head_img,
    addr.tel,
    addr.address,
    a.province
FROM 
    user t
    LEFT JOIN user_address addr on t.id = addr.user_id
    RIGHT JOIN area a on addr.area_id = a.id 
LIMIT ?,?

還可以這麼操作,但不建議

class test {
    @Resource
    private UserMapper userMapper;

    void testJoin() {
        List<UserDTO> list = userMapper.selectJoinList(UserDTO.class,
                new MPJQueryWrapper<UserDO>()
                        .selectAll(UserDO.class)
                        .select("addr.tel", "addr.address")
                        //行列轉換
                        .select("CASE t.sex WHEN '男' THEN '1' ELSE '0' END AS sex")
                        //求和函數
                        .select("sum(a.province) AS province")
                        //自定義數據集
                        .leftJoin("(select * from user_address) addr on t.id = addr.user_id")
                        .rightJoin("area a on addr.area_id = a.id")
                        .like("addr.tel", "1")
                        .le("a.province", "1")
                        .orderByDesc("addr.id"));
    }
}

對應sql

SELECT 
    t.id,
    t.name,
    t.sex,
    t.head_img,
    addr.tel,
    addr.address,
    CASE t.sex WHEN '男' THEN '1' ELSE '0' END AS sex,
    sum(a.province) AS province
FROM 
    user t
    LEFT JOIN (select * from user_address) addr on t.id = addr.user_id
    RIGHT JOIN area a on addr.area_id = a.id
WHERE (
    addr.tel LIKE ?
    AND a.province <= ?)
ORDER BY
    addr.id DESC

總結

到此這篇關於MyBatis-Plus實現連表查詢的文章就介紹到這瞭,更多相關MyBatis-Plus實現連表查詢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: