您好,登錄后才能下訂單哦!
這篇文章跟大家分析一下“MyBatis-Plus如何實現連表查詢”。內容詳細易懂,對“MyBatis-Plus如何實現連表查詢”感興趣的朋友可以跟著小編的思路慢慢深入來閱讀一下,希望閱讀后能夠對大家有所幫助。下面跟著小編一起深入學習“MyBatis-Plus如何實現連表查詢”的知識吧。
在項目中添加依賴
<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示例
簡單的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 ?,?
簡單的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如何實現連表查詢就分享到這里啦,希望上述內容能夠讓大家有所提升。如果想要學習更多知識,請大家多多留意小編的更新。謝謝大家關注一下億速云網站!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。