MybatisPlus中的多表条件排序查询
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.自定义返回对象
@Data
@apiModel
public class SupLogUserVO
{
@ApiModelProperty(value="日志ID")
private Long id;
@ApiModelProperty(value="用户ID")
private Long userId;
@ApiModelProperty(value="姓名")
private String name;
@ApiModelProperty(value="操作类型")
private String operation;
@ApiModelProperty(value="方法")
private String method;
@ApiModelProperty(value="参数")
private String param;
@ApiModelProperty(value="客户端类型")
private String clientType;
@ApiModelProperty(value="业务系统")
private String sysId;
@ApiModelProperty(value="ip")
private String ip;
@ApiModelProperty(value="创建时间")
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自定义的查询工具
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;
public class QueryUtil
{
public static String filterSql(String sql)
{
return BclSqlUtil.filterSql(sql, true);
}
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);
}
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) ) //无指定值
{
if( AppConst.DESC.equalsIgnoreCase(defOrderType) ) //判断默认值
isAsc = false;
}
else //有指定值
{
if( AppConst.DESC.equalsIgnoreCase(orderType) )
isAsc = false;
}
//再判断排序字段
if( StringUtils.isBlank(orderField) ) //无指定值
{
if( !StringUtils.isBlank(defOrderField) ) //有默认值
wrapper.orderBy(true, isAsc, QueryUtil.filterSql(defOrderField));
}
else //有指定值
wrapper.orderBy(true, isAsc, QueryUtil.filterSql(orderField));
return wrapper;
}
public static <T> QueryWrapper<T> addOrderBy(QueryWrapper<T> wrapper, String orderField, String orderType)
{
return addOrderBy(wrapper, orderField, null, orderType, null);
}
//待删除
public static int[] getPageParam(Integer pageNo, Integer pageSize)
{
int[] result = new int[3];
//条数
if(pageSize==null || pageSize<1)
result[1] = 20;
else if(pageSize > 200)
result[1] = 200;
else
result[1] = pageSize;
//开始索引
if(pageNo==null || pageNo<1)
{
result[0] = 0;
result[2] = 1;
}
else
{
result[0] = (pageNo-1)*result[1];
result[2] = pageNo;
}
return result;
}
}
7.常量配置
public class AppConst
{
//==== 查询相关的参数
public static final int Q_MAX_RESULT = 1000;
public static final int Q_MAX_PER_PAGE = 100;
public static final int Q_DEFAULT_PER_PAGE = 20;
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"; //返回给前端的分页数据参数名
}
8.controller方法
@GetMapping("/getList")
@ApiOperation(value="查询用户列表", notes="")
public CommonResponse<Object> getList(
@ApiParam(name="name", value="名称(模糊匹配),空表示不限。",defaultValue="超级") @RequestParam(required=false) String name,
@ApiParam(name="curPage", value="开始页数",defaultValue="1") @RequestParam(required=false) Integer curPage,
@ApiParam(name="limit", value="每页条数",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>();
//分页参数
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
入参:
出参:
10.sql表
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户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 '账户类别 999999=超级用户',
`status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '账户状态 ≤0=禁用(0=冻结),>0=正常',
`check_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '审核状态 ≤0=待审核,>0=审核通过',
`delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除标志 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 '身份证号',
`mobile_phone` varchar(50) DEFAULT NULL COMMENT '手机号',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`dept_id` int(11) NOT NULL DEFAULT '1' COMMENT '所属部门',
`position` varchar(10) DEFAULT NULL COMMENT '职务',
`memo` varchar(50) DEFAULT NULL COMMENT '备注',
`create_user` int(11) DEFAULT NULL COMMENT '创建者 此记录的创建用户',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`user_id`),
UNIQUE KEY `login_name_UNI` (`login_name`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='用户';
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 '用户ID',
`operation` varchar(50) NOT NULL COMMENT '操作',
`method` varchar(100) NOT NULL COMMENT '调用的方法',
`param` varchar(500) DEFAULT NULL COMMENT '参数',
`client_type` varchar(10) DEFAULT NULL COMMENT '客户端类型 见枚举定义',
`sys_id` varchar(50) DEFAULT NULL COMMENT '访问的业务系统 见枚举定义',
`ip` varchar(50) DEFAULT NULL COMMENT 'IP地址',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8 COMMENT='系统访问日志 记录访问日志(只记录管理相关日志,其它日志仅记录到日志文件)';
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章