MybatisPlus自定義Sql實(shí)現(xiàn)多表查詢的示例
前言
前段時間看同事的代碼,發(fā)現(xiàn)他用Layui+MybatisPlus做分頁查詢做得很規(guī)整,認(rèn)真看了下代碼發(fā)現(xiàn)這種方式不僅適用于與Layui做分頁查詢,在任何時候需要多表聯(lián)查的時候都可以用到。 以下以Layui分頁查詢作為參考,在實(shí)際應(yīng)用中可以靈活使用。
分頁查詢VO對象
@Data@AllArgsConstructor@NoArgsConstructorpublic class LayuiData { private Integer code=0; private Long count; private String msg='ok'; private Object data;}
Controller
這里的“keyWord”和“registerTime”是后臺頁面可以查詢的字段,也就是普通的參數(shù),可以靈活變通。
@GetMapping('/getClientList')@ResponseBodypublic LayUIResult getAll( @RequestParam(name = 'page', required = true, defaultValue = '1') int num, @RequestParam(name = 'limit', required = true, defaultValue = '10') int size, String keyWord, String registerTime){ IPage<Map<String, Object>> listPage = clientService.findClientPage(num, size, keyWord,registerTime); //返回總數(shù)和數(shù)據(jù) return new LayuiData (listPage.getTotal(),listPage.getRecords());}
Service
IPage<Map<String, Object>> findClientPage(Integer num, Integer size, String keyWord, String registerTime);
ServiceImpl
這里的QueryWrapper內(nèi)的實(shí)例是<Map<String, Object>,不是平常的實(shí)體類,返回的也是Map。
@Overridepublic IPage<Map<String, Object>> findClientPage(Integer num, Integer size, String keyWord, String registerTime) { //創(chuàng)建QueryWrapper搜索對象,判斷參數(shù)不為空則傳入?yún)?shù) QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>(); if (StringUtils.isNotEmpty(keyWord)) { wrapper.like('c.real_name', keyWord).or().like('c.phone', keyWord); } if (StringUtils.isNotEmpty(registerTime)) { String stime = registerTime.substring(0, 20); String etime = registerTime.substring(22, 41); wrapper.ge('c.register_time', stime).le('c.register_time', etime); } //創(chuàng)建分頁對象 Page<Map<String, Object>> page = new Page<>(num, size); return clientMapper.findClientPage(page, wrapper);}
Mapper
格式要求,QueryWrapper前面加上@param,括號里的Constants.WRAPPER內(nèi)容就是'ew',對應(yīng)xml文件里的ew
IPage<Map<String, Object>> findClientPage(Page<Map<String, Object>> page,@Param(Constants.WRAPPER) QueryWrapper<Map<String, Object>> wrapper);
XML內(nèi)容
重點(diǎn)在于我們用${ew.customSqlSegment}放在sql語句里,它可以直接把我們的wrapper里的查詢數(shù)據(jù)等同于where查詢添加進(jìn)去
<select resultType='java.util.Map'> SELECT c.id,c.real_name,c.phone,c.`status`, //實(shí)現(xiàn)將時間轉(zhuǎn)換成固定格式 DATE_FORMAT(c.register_time,’%Y-%m-%d %H:%i:%s’) registerTime, SUM(re.money) rechargeMoney, SUM(wi.withdrawal_money) withdrawalMoney,SUM(buy_money) orderMoney, wa.balance FROM client c LEFT JOIN recharge re ON c.id=re.client_id LEFT JOIN withdrawal wi ON c.id=wi.client_id LEFT JOIN wallet wa ON c.id=wa.client_id LEFT JOIN order_position ord on c.id=ord.client_id //重點(diǎn)是這里會插入wrapper的搜索語句 ${ew.customSqlSegment} GROUP BY c.id</select>
總結(jié)
這種方式相當(dāng)于在業(yè)務(wù)層已經(jīng)做好了參數(shù)判斷,不用再在xml文件內(nèi)用“if”標(biāo)簽判斷了。 除了在與Layui做分頁查詢外,在別的需要參數(shù)請求的地方也都可以變通的用這種方法,在使用MybatisPlus時使用這種方式可以使代碼更簡潔,更清晰。 除此之外,在需要多表聯(lián)查的時候,這種方式是非常適用的。
MybatiPlus文檔
官方文檔里面也做介紹,版本需要大于3.0.7官方鏈接:使用 Wrapper 自定義SQL
到此這篇關(guān)于MybatisPlus自定義Sql實(shí)現(xiàn)多表查詢的示例的文章就介紹到這了,更多相關(guān)MybatisPlus 多表查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Navicat Premium操作MySQL數(shù)據(jù)庫(執(zhí)行sql語句)2. 數(shù)據(jù)庫相關(guān)的幾個技能:ACCESS轉(zhuǎn)SQL3. SQL Server 2008新功能─傳遞表值參數(shù)4. navicat for mysql導(dǎo)出sql文件的方法5. navicat for mysql導(dǎo)出數(shù)據(jù)庫的方法6. Windows下不能啟動mysql服務(wù)--錯誤總結(jié)7. 淺談Mysql連接數(shù)據(jù)庫時host和user的匹配規(guī)則8. Access數(shù)據(jù)庫安全的幾個問題9. SQL Server下7種“數(shù)據(jù)分頁”方案全網(wǎng)最新最全10. mybatis plus代碼生成工具的實(shí)現(xiàn)代碼
