插件PageHelper实现分页查询
一,需求:
CommonQuery--PyQueryBean
PyQueryBean:鹏飞历史记录查询,以往哪些人对征信进行了查询。
CommonQuery:查询条件:根据查询人(umName)、被查询人姓名(name)、身份证号(documentNo)、手机号(phone)、查询日期(queryDate)
二,配置相关:
1,mybatis-config.xml
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
复制代码
2,pom依赖
<!-- mybatis分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
三,代码
Controller
/** * @param request, pyQueryBean * @return com.pingan.credit.model.ResponseResult<java.util.List<com.pingan.credit.model.py.PyQueryBean>> * @Description: 历史记录查询 * @date 2017/11/23 17:00 */ @RequestMapping(path = "/queryPyHistoryRecord", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public ResponseResult<BasePageInfo<PyQueryBean>> queryPyHistoryRecord(HttpServletRequest request, CommonQuery commonQuery) { ResponseResult<BasePageInfo<PyQueryBean>> responseResult = new ResponseResult<>(); try { BasePageInfo<PyQueryBean> pageInfo = pyQueryService.queryHistoryRecord(commonQuery); responseResult.setData(pageInfo); responseResult.setRet(SUCCESS); responseResult.setRefID(commonQuery.getRefID()); responseResult.setTimestamp(DateUtil.getTimeStamp().toString()); } catch (CreditException e) { responseResult.setErrorCode(e.getErrorCode()); responseResult.setRet(SUCCESS); responseResult.setErrorMsg(e.getMessage()); } catch (Exception e) { responseResult.setErrorCode(PyCreditServiceErrorEnum.SYSTEM_ERROR.getCode()); responseResult.setRet(FAILED); responseResult.setErrorMsg(PyCreditServiceErrorEnum.SYSTEM_ERROR.getMsg()); } return responseResult; }
接口响应类
public class ResponseResult<T> extends BasePojo { /**成功标识符 **/ private boolean ret; /** 异常信息**/ private String errorMsg; /** 返回数据类型 **/ private T data; /** 异常码 **/ private String errorCode; /** 请求ID**/ private String refID; /** 时间戳 **/ private String timestamp;
--响应结果类
public class PyQueryBean extends BaseQuery implements Serializable { private transient int id; /**查询者姓名,不允许为空**/ private String name; //被查询者证件号码,不允许为空 private String documentNo; //被查询者手机号码,反欺诈的报告不允许为空 深圳个人征信可以为空 private String phone; //查询原因ID,只能传本单位允许的查询原因 101 private String queryReasonID; //96040 反欺诈 90008 深圳个人信用 private String subreportIDs; //引用ID,查询者提交的用于识别本次查询的流水号,系统返回结果中会原样包含该流水号。可以为空。 private String refID; private String umName; //证件类型 private String documentType; //报告 private String docId; //查询日期 private String queryDate; //查询类型 @JSONField(serialize = false ) private Integer queryType; private String imageId; //是否直连 1:直连 private Integer queryFromPy;
** * 分页查询类 ** @date 2017/11/29 15:54 **/ public class BasePageInfo<T>{ /** * 总记录数 **/ private long total; /** * 结果集 **/ private List<T> list; /** * 第几页 **/ private int pageNum; /** * 每页记录数 */ private int pageSize; /** * 总页数 **/ private int pages; /** * 当前页的数量 <= pageSize,该属性来自ArrayList的size属性 **/ private int size; /** * 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理, * 而出现一些问题。 * * @param list page结果 */ public BasePageInfo(List<T> list) { if (list instanceof Page) { Page<T> page = (Page<T>) list; this.pageNum = page.getPageNum(); this.pageSize = page.getPageSize(); this.total = page.getTotal(); this.pages = page.getPages(); this.list = page; this.size = page.size(); } }
** * 通用查询类,封装查询条件* @date 2017/11/29 9:27 **/ public class CommonQuery extends BaseQuery { /** * 姓名 **/ private String name; /** * 身份证号 **/ private String documentNo; /** * 手机号 **/ private String phone; /** * 查询起始日期 **/ private String startDate; /** * 查询终止日期 **/ private String endDate; /** * 查询方式 **/ private String queryWay; /** * um帐号 **/ private String umName; /** * 引用ID **/ private String refID; /** * 查询页码 **/ private int page; /** * 页面展示数量 **/ private int pageSize;
Service
/** * @param commonQuery, type, pageNum, pageSize * @return com.github.pagehelper.PageInfo<com.pingan.credit.model.py.PyQueryBean> * @Description: 根据查询人(umName)、被查询人姓名(name)、身份证号(documentNo)、手机号(phone)、查询日期(queryDate) * @date 2017/11/29 9:24 */ @Override public BasePageInfo<PyQueryBean> queryHistoryRecord(CommonQuery commonQuery) throws Exception { try { logger.info("queryHistoryRecord ... "); Map<String, Object> map = new HashMap<>(); map.put("obj", commonQuery); PageHelper.startPage(commonQuery.getPage(), commonQuery.getPageSize()); List<PyQueryBean> list = queryRecordMapper.selectQueryRecordByCondition(map); BasePageInfo<PyQueryBean> result = new BasePageInfo<>(list); result.setList(list); logger.info(JSON.toJSONString(list)); return result; } catch (Exception e) { logger.error("level0_queryHistoryRecord@PyQueryServiceImpl_Exception", e); throw e; } }
sql
<!-- 根据条件查询出历史记录 --> <select id="selectQueryRecordByCondition" resultMap="BaseResultMap" parameterType="Map"> SELECT name,documentNo,umName,to_char(queryDate,'yyyy-MM-dd hh24:mi:ss') as queryDate,queryWay,queryReasonCode,queryReason FROM ICS_PY_QUERYRECORD <trim prefix="WHERE" prefixOverrides="AND | OR "> <if test="obj.startDate !=null and obj.startDate !='' "> <![CDATA[ AND QUERYDATE> to_date(#{obj.startDate,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss') ]]> </if> <if test="obj.endDate !=null and obj.endDate !='' "> <![CDATA[ AND QUERYDATE<=to_date(#{obj.endDate,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss' )]]> </if> <if test="obj.umName != null and obj.umName !=''"> AND UMNAME = #{obj.umName,jdbcType=VARCHAR} </if> <if test="obj.name != null and obj.name !=''"> AND NAME = #{obj.name,jdbcType=VARCHAR} </if> <if test="obj.queryWay !=null and obj.queryWay !='' "> AND QUERYWAY = #{obj.queryWay,jdbcType=VARCHAR} </if> <if test="obj.queryReasonCode !=null and obj.queryReasonCode !='' "> AND QUERYREASONCODE = #{obj.queryReasonCode,jdbcType=VARCHAR} </if> </trim> ORDER BY QUERYDATE DESC </select>