MybatisPlus中的多表?xiàng)l件排序查詢(xún)
更新時(shí)間:2022年09月27日 09:46:32 作者:魏N來(lái)
這篇文章主要介紹了MybatisPlus中的多表?xiàng)l件排序查詢(xún),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
1.pom文件
sql支持 ${ew.customSqlSegment} 最低版本3.0.7
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.7</version> </dependency>
2.自定義返回對(duì)象
@Data @ApiModel public class SupLogUserVO { @ApiModelProperty(value="日志ID") private Long id; @ApiModelProperty(value="用戶(hù)ID") private Long userId; @ApiModelProperty(value="姓名") private String name; @ApiModelProperty(value="操作類(lèi)型") private String operation; @ApiModelProperty(value="方法") private String method; @ApiModelProperty(value="參數(shù)") private String param; @ApiModelProperty(value="客戶(hù)端類(lèi)型") private String clientType; @ApiModelProperty(value="業(yè)務(wù)系統(tǒng)") private String sysId; @ApiModelProperty(value="ip") private String ip; @ApiModelProperty(value="創(chuàng)建時(shí)間") private LocalDateTime createTime; }
3.mapper方法
IPage<SupLogUserVO> getSupLogUser(IPage<SupLogUserVO> page, @Param(Constants.WRAPPER) Wrapper<SupLogUserVO> queryWrapper);
4.xml自定義sql
<select id="getSupLogUser" resultType="bw.yth.svc.web.sysmng.vo.SupLogUserVO"> select l.id,l.user_id userId,u.`name`,l.operation,l.method,l.param, l.client_type clientType,l.sys_id sysId,l.ip,l.create_time createTime from sup_log l LEFT JOIN sys_user u on l.user_id = u.user_id ${ew.customSqlSegment} </select>
5.service方法
public IPage<SupLogUserVO> getSupLogUser(String name, String six, String order, Integer curPage, Integer limit) { QueryWrapper<SupLogUserVO> wrapper = new QueryWrapper<SupLogUserVO>().like(StringUtils.isNotBlank(name), "name", name); wrapper = QueryUtil.addOrderBy(wrapper, six, null, order, null); Page<SupLogUserVO> page = QueryUtil.getPage(curPage, limit); return supLogDao.getSupLogUser(page, wrapper); }
6.QueryUtil自定義的查詢(xún)工具
import org.apache.commons.lang3.StringUtils; import bw.common.util.BclSqlUtil; import bw.yth.svc.base.AppConst; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; /** 查詢(xún)相關(guān)的工具類(lèi) */ public class QueryUtil { /** 防SQL注入過(guò)濾。去掉 “'";\”及空格 * @return - 輸入為空或全被過(guò)濾則返回空串,一定不會(huì)返回null */ public static String filterSql(String sql) { return BclSqlUtil.filterSql(sql, true); } /** 根據(jù)分頁(yè)參數(shù)生成MP分頁(yè)對(duì)象 * @param pageNo 頁(yè)號(hào)(從1開(kāi)始) * @param pageSize 每頁(yè)條數(shù) */ public static <T> Page<T> getPage(Integer pageNo, Integer pageSize) { if(pageNo==null || pageNo<1) pageNo = 1; if(pageSize==null || pageSize<1 || pageSize>AppConst.Q_MAX_PER_PAGE) pageSize = AppConst.Q_DEFAULT_PER_PAGE; return new Page<T>(pageNo, pageSize); } /** 為查詢(xún)?cè)黾优判驐l件(可指定默認(rèn)條件) * @param wrapper 查詢(xún)對(duì)象 * @param orderField 指定的排序字段,可為空 * @param defOrderField 缺省的排序字段(無(wú)指定字段時(shí)用),可為空 * @param orderType 指定的排序方向,可為空 * @param defOrderType 缺省的排序方向(無(wú)指定方向時(shí)用),可為空 * @return 查詢(xún)對(duì)象 */ public static <T> QueryWrapper<T> addOrderBy(QueryWrapper<T> wrapper, String orderField, String defOrderField, String orderType, String defOrderType) { boolean isAsc; //先判斷排序方向 isAsc = true; if( StringUtils.isBlank(orderType) ) //無(wú)指定值 { if( AppConst.DESC.equalsIgnoreCase(defOrderType) ) //判斷默認(rèn)值 isAsc = false; } else //有指定值 { if( AppConst.DESC.equalsIgnoreCase(orderType) ) isAsc = false; } //再判斷排序字段 if( StringUtils.isBlank(orderField) ) //無(wú)指定值 { if( !StringUtils.isBlank(defOrderField) ) //有默認(rèn)值 wrapper.orderBy(true, isAsc, QueryUtil.filterSql(defOrderField)); } else //有指定值 wrapper.orderBy(true, isAsc, QueryUtil.filterSql(orderField)); return wrapper; } /** 為查詢(xún)?cè)黾优判驐l件(無(wú)默認(rèn)排序條件) * @param wrapper 查詢(xún)對(duì)象 * @param orderField 指定的排序字段,可為空 * @param orderType 指定的排序方向,可為空 * @return 查詢(xún)對(duì)象 */ public static <T> QueryWrapper<T> addOrderBy(QueryWrapper<T> wrapper, String orderField, String orderType) { return addOrderBy(wrapper, orderField, null, orderType, null); } //待刪除 /** 獲得數(shù)據(jù)庫(kù)分頁(yè)參數(shù)。返回:[0]=開(kāi)始索引(from 0) [1]=條數(shù) [2]=頁(yè)號(hào)(從1開(kāi)始) */ public static int[] getPageParam(Integer pageNo, Integer pageSize) { int[] result = new int[3]; //條數(shù) if(pageSize==null || pageSize<1) result[1] = 20; else if(pageSize > 200) result[1] = 200; else result[1] = pageSize; //開(kāi)始索引 if(pageNo==null || pageNo<1) { result[0] = 0; result[2] = 1; } else { result[0] = (pageNo-1)*result[1]; result[2] = pageNo; } return result; } }
7.常量配置
/** 項(xiàng)目的常量定義-業(yè)務(wù)相關(guān) */ public class AppConst { //==== 查詢(xún)相關(guān)的參數(shù) /** 查詢(xún)返回的最大條數(shù)。優(yōu)先級(jí)高于分頁(yè)參數(shù),防止返回?cái)?shù)據(jù)太多導(dǎo)致數(shù)據(jù)庫(kù)負(fù)擔(dān)過(guò)重 */ public static final int Q_MAX_RESULT = 1000; /** 最大的每頁(yè)大小值 */ public static final int Q_MAX_PER_PAGE = 100; /** 默認(rèn)的每頁(yè)大小值 */ public static final int Q_DEFAULT_PER_PAGE = 20; /** 導(dǎo)出數(shù)據(jù)時(shí)返回的最大條數(shù) */ public static final int Q_MAX_EXPORT = 5000; //==== 其它 public static final String ASC = "ASC"; public static final String DESC = "DESC"; public static final String PAGE = "page"; //返回給前端的分頁(yè)數(shù)據(jù)參數(shù)名 }
8.controller方法
@GetMapping("/getList") @ApiOperation(value="查詢(xún)用戶(hù)列表", notes="") public CommonResponse<Object> getList( @ApiParam(name="name", value="名稱(chēng)(模糊匹配),空表示不限。",defaultValue="超級(jí)") @RequestParam(required=false) String name, @ApiParam(name="curPage", value="開(kāi)始頁(yè)數(shù)",defaultValue="1") @RequestParam(required=false) Integer curPage, @ApiParam(name="limit", value="每頁(yè)條數(shù)",defaultValue="5") @RequestParam(required=false) Integer limit, @ApiParam(name="six", value="排序字段",defaultValue="createTime") @RequestParam(required=false) String six, @ApiParam(name="order", value="排序方向",defaultValue="desc:降序,asc:升序") @RequestParam(required=false) String order ) { CommonResponse<Object> resp = new CommonResponse<Object>(); //分頁(yè)參數(shù) IPage<SupLogUserVO> resultPage = supLogService.getSupLogUser(name,six,order,curPage,limit); resp.addData("data", resultPage.getRecords()); resp.addData("page", QueryPage.of(resultPage)); return resp; }
9.swagger
入?yún)ⅲ?/p>
出參:
10.sql表
DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶(hù)ID', `login_name` varchar(50) NOT NULL COMMENT '登錄名', `password` varchar(100) NOT NULL COMMENT '登錄密碼 加密后的密碼', `salt` varchar(50) NOT NULL COMMENT '加密用的鹽', `type` int(11) NOT NULL DEFAULT '0' COMMENT '賬戶(hù)類(lèi)別 999999=超級(jí)用戶(hù)', `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '賬戶(hù)狀態(tài) ≤0=禁用(0=凍結(jié)),>0=正常', `check_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '審核狀態(tài) ≤0=待審核,>0=審核通過(guò)', `delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '刪除標(biāo)志 0=未刪除,1=已刪除', `name` varchar(10) NOT NULL COMMENT '姓名', `sex` char(1) DEFAULT NULL COMMENT '性別 F=男,M=女', `birthday` datetime DEFAULT NULL COMMENT '生日', `id_card_num` varchar(50) DEFAULT NULL COMMENT '身份證號(hào)', `mobile_phone` varchar(50) DEFAULT NULL COMMENT '手機(jī)號(hào)', `email` varchar(50) DEFAULT NULL COMMENT '郵箱', `dept_id` int(11) NOT NULL DEFAULT '1' COMMENT '所屬部門(mén)', `position` varchar(10) DEFAULT NULL COMMENT '職務(wù)', `memo` varchar(50) DEFAULT NULL COMMENT '備注', `create_user` int(11) DEFAULT NULL COMMENT '創(chuàng)建者 此記錄的創(chuàng)建用戶(hù)', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', PRIMARY KEY (`user_id`), UNIQUE KEY `login_name_UNI` (`login_name`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='用戶(hù)'; DROP TABLE IF EXISTS `sup_log`; CREATE TABLE `sup_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `user_id` bigint(20) NOT NULL COMMENT '用戶(hù)ID', `operation` varchar(50) NOT NULL COMMENT '操作', `method` varchar(100) NOT NULL COMMENT '調(diào)用的方法', `param` varchar(500) DEFAULT NULL COMMENT '參數(shù)', `client_type` varchar(10) DEFAULT NULL COMMENT '客戶(hù)端類(lèi)型 見(jiàn)枚舉定義', `sys_id` varchar(50) DEFAULT NULL COMMENT '訪問(wèn)的業(yè)務(wù)系統(tǒng) 見(jiàn)枚舉定義', `ip` varchar(50) DEFAULT NULL COMMENT 'IP地址', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8 COMMENT='系統(tǒng)訪問(wèn)日志 記錄訪問(wèn)日志(只記錄管理相關(guān)日志,其它日志僅記錄到日志文件)';
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 如何使用JDBC連接數(shù)據(jù)庫(kù)
這篇文章主要介紹了Java 如何使用JDBC連接數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-02-02spring4.3 實(shí)現(xiàn)跨域CORS的方法
下面小編就為大家分享一篇spring4.3 實(shí)現(xiàn)跨域CORS的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01解決SpringBoot中使用@Async注解失效的問(wèn)題
這篇文章主要介紹了解決SpringBoot中使用@Async注解失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09JAVA將中文轉(zhuǎn)換為拼音簡(jiǎn)單實(shí)現(xiàn)方法
拼音轉(zhuǎn)換是中文處理的常見(jiàn)需求,TinyPinyin、HanLP、pinyin4j是常用的本地拼音轉(zhuǎn)換庫(kù),各有特點(diǎn),開(kāi)發(fā)者可根據(jù)具體需求選擇合適的拼音轉(zhuǎn)換工具,需要的朋友可以參考下2024-10-10Java獲取e.printStackTrace()打印的信息方式
這篇文章主要介紹了Java獲取e.printStackTrace()打印的信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08