使用方法 安裝 Maven <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-plus-join</artifactId> <version>1.2.4</version> </dependency> G ...
使用方法
安裝
Maven
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join</artifactId>
<version>1.2.4</version>
</dependency>
Gradle
implementation 'com.github.yulichang:mybatis-plus-join:1.2.4'
或者clone代碼到本地執行 mvn install, 再引入以上依賴
註意: mybatis plus version >= 3.4.0
推薦一個開源免費的 Spring Boot 最全教程:
使用
- mapper繼承MPJBaseMapper (必選)
- service繼承MPJBaseService (可選)
- serviceImpl繼承MPJBaseServiceImpl (可選)
核心類 MPJLambdaWrapper和MPJQueryWrapper
MPJLambdaWrapper用法
簡單的三表查詢
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
作者:陽宗德
鏈接:https://juejin.cn/post/7110405284811522085
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
4.別再寫滿屏的爆爆爆炸類了,試試裝飾器模式,這才是優雅的方式!!
覺得不錯,別忘了隨手點贊+轉發哦!