自定义分页查询使用过两种方法,当然总的绝对不止有两种方法能够实现layui自定义查询
一,第一种,自定义dto,自己编写sql语句,再xml文件中将数据库字段别名为对应的dto属性
1.对应的dto
import lombok.Getter;
import lombok.Setter;
import org.springframework.scheduling.support.SimpleTriggerContext;
import java.io.Serializable;
public class DictDto implements Serializable {
/**
* 字典值对应的ID
*/
@Getter
@Setter
private String id;
/**
* 字典类型ID
*/
@Getter
@Setter
private String typeId;
/**
* 字典类型编码
*/
@Getter
@Setter
private String typeCode;
/**
* 字典类型名称
*/
@Getter
@Setter
private String typeName;
/**
* 字典code
*/
@Getter
@Setter
private String dictCode;
/**
* 字典中文显示值
*/
@Getter
@Setter
private String dictName;
/**
* 字典排序
*/
@Getter
@Setter
private Integer sort;
}
2.对应的xml查询语句
其中param为搜索条件参数集合
<select id="selectDictList" resultType="com.rtsm.zhjs.background.modules.system.dto.dict.DictDto">
select v.id,
v.bm as dictCode,
mrxs as dictName,
lxbm as typeCode,
t.mc as typeName
from xt_zdz v
left join xt_zdlx t on (v.lxbm = t.id)
where v.sc = 0
<if test="null!=param.keyword and ''!=param.keyword">
and
(
v.bm like CONCAT('%',#{param.keyword},'%')
or mrxs like CONCAT('%',#{param.keyword},'%')
or zwxs like CONCAT('%',#{param.keyword},'%')
or lxbm like CONCAT('%',#{param.keyword},'%')
or t.mc like CONCAT('%',#{param.keyword},'%')
)
</if>
<if test="null!=param.code and ''!=param.code">
and v.lxbm = #{param.code}
</if>
</select>
二,使用entity实体类进行查询,不用自己写sql但是需要自己做封装。
1.PrisonInfoListResp resp dto类
package com.rtsm.zhjs.background.modules.prison.dto;
import lombok.Getter;
import lombok.Setter;
import org.joda.time.LocalDate;
/**
* @program: zhjs-background
* @description: 罪犯信息集合resp
* @author: hu-hu
* @create: 2019-04-26 14:58
**/
@Setter
@Getter
public class PrisonInfoListResp {
/**
* id
*/
private String id;
/**
* 囚犯姓名
*/
private String name;
/**
* 囚犯编号
*/
private String code;
/**
* 囚犯性别
*/
private String sex;
/**
* 囚犯出生日期
*/
private String birthDate;
/**
* 囚犯名族
*/
private String nationality;
/**
* 囚犯政治面貌
*/
private String politicalLandscape;
/**
* 囚犯证件号码
*/
private String idNumber;
/**
* 囚犯国家/地区
*/
private String countriesRegions;
/**
* 囚犯籍贯区号
*/
private String nativePlace;
/**
* 囚犯出生地
*/
private String brithPlace;
/**
* 囚犯家庭住址
*/
private String homeAddress;
/**
* 囚犯身高
*/
private String height;
/**
* 囚犯体重
*/
private String weight;
/**
* 囚犯等级
*/
private String prisonerLevel;
/**
* 囚犯监仓编号
*/
private String prisonCode;
}
2.分页查询查询实现类
/**
* @description: 分页查询罪犯信息集合
* @author: huhu
* @date: 2019/4/26
* [req]
* @return: com.baomidou.mybatisplus.core.metadata.IPage<com.rtsm.zhjs.background.modules.prison.dto.PrisonInfoListResp>
*/
@Override
public IPage<PrisonInfoListResp> findPrisonInfoListPage(PrisonInfoListReq req) {
Page<JcglZfxx> page = new Page<>(req.getPageNum(),req.getPageSize());
QueryWrapper qw = new QueryWrapper();
if(!StringUtils.isBlank(req.getCountriesRegions())) {
qw.like("gjdq",req.getCountriesRegions());
}
if(!StringUtils.isBlank(req.getName())) {
qw.like("xm",req.getName());
}
if(!StringUtils.isBlank(req.getIdNumber())) {
qw.like("zjhm",req.getIdNumber());
}
if(!StringUtils.isBlank(req.getPrisonCode())) {
qw.like("jcbh",req.getPrisonCode());
}
qw.eq("sc",SysConstance.isDel.NO);
//因为之后需要替换ipage中的records,所以ipage不要用泛型,不然之后的list塞不进去
IPage ipage = jcglZfxxMapper.selectPage(page,qw);
//遍历ipge,将属性set到resp中
List<PrisonInfoListResp> list = new ArrayList<>();
for(JcglZfxx jcglZfxx : (List<JcglZfxx>)ipage.getRecords()) {
PrisonInfoListResp pr = new PrisonInfoListResp();
pr.setId(jcglZfxx.getId());
pr.setBirthDate(String.valueOf(jcglZfxx.getCsrq()));
pr.setBrithPlace(jcglZfxx.getCsqh());
pr.setCode(jcglZfxx.getZfBh());
pr.setCountriesRegions(jcglZfxx.getGjdq());
pr.setHeight(String.valueOf(jcglZfxx.getSg()));
pr.setHomeAddress(jcglZfxx.getJtmx());
pr.setIdNumber(jcglZfxx.getZjhm());
pr.setName(jcglZfxx.getXm());
pr.setNationality(jcglZfxx.getMz());
pr.setPoliticalLandscape(jcglZfxx.getBqmm());
pr.setNativePlace(jcglZfxx.getJgqh());
pr.setPrisonCode(jcglZfxx.getJqbh());
pr.setPrisonerLevel(jcglZfxx.getZfdj());
pr.setSex(jcglZfxx.getXb());
pr.setWeight(String.valueOf(jcglZfxx.getTz()));
list.add(pr);
}
ipage.setRecords(list);
return ipage;
}
本人推荐使用第一种,第一种代码可读性更强一些。而且从新能方面来分析第一种肯定也会更好一点。