需求背景,写了个联合接口,需要自定义body请求参数,page分页和params
看代码
@PostMapping("/xxx")
public AjaxResult xxx(HttpServletRequest request){
AjaxResult ajax = AjaxResult.success();
OrderGift orderGift_top = new OrderGift();
OrderGift orderGift_normal = new OrderGift();
String shopId = "11"
ConfigGlobal sysConfigGlobal = configGlobalService.selectConfigGlobalByShopId(shopId);
if(sysConfigGlobal != null && sysConfigGlobal.getGiftShowTopStatus().equals(UserConstants.YES)){
PageHelper.startPage(1, 3).setReasonable(true);
// desc为降序,asc为升序
PageHelper.orderBy("init_amount desc");
Map<String, Object> params_top = new HashMap<>();
String endTime = MyUtils.dateToStrLong(new Date());
int giftRecentDay = 7;
String beginTime = MyUtils.getBeforeDay(endTime, giftRecentDay);
params_top.put("beginTime", beginTime);
params_top.put("endTime", endTime);
orderGift_top.setParams(params_top);
List<OrderGift> topList = orderGiftService.selectOrderGiftList(orderGift_top);
ajax.put("topList", topList);
}
PageHelper.clearPage();
int pageNum = 10;
BigDecimal giftShowMin = new BigDecimal(0);
Map<String, Object> params_normal = new HashMap<>();
if(sysConfigGlobal != null){
pageNum = sysConfigGlobal.getGiftShowCount();
giftShowMin = sysConfigGlobal.getGiftShowMin();
}
params_normal.put("minAmount", giftShowMin);
orderGift_normal.setParams(params_normal);
PageHelper.startPage(1, pageNum).setReasonable(true);
List<OrderGift> giftList = orderGiftService.selectOrderGiftList(orderGift_normal);
ajax.put("giftList", giftList);
return ajax;
}
mybatis中xml的代码
<select id="selectOrderGiftList" parameterType="OrderGift" resultMap="OrderGiftResult">
<include refid="selectOrderGiftVo"/>
where 1 = 1
<if test="orderNo != null and orderNo != ''">
AND o.order_no like concat('%', #{orderNo}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
AND date_format(o.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
AND date_format(o.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="params.minAmount != null"><!-- 最小金额检索 -->
AND #{params.minAmount} <= o.init_amount
</if>
<!-- 数据范围过滤 -->
${params.dataScope}
</select>
自定义分页:
PageHelper.startPage(1, 5).setReasonable(true);
// desc为降序,asc为升序 自定义排序:
PageHelper.orderBy("init_amount desc");
清除当前线程的分页参数
PageHelper.clearPage();
/**
* 封装分页对象 会读取客户端传来的pageSize,客户端没传,会默认10条
*/
public static PageDomain getPageDomain()
{
PageDomain pageDomain = new PageDomain();
pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1));
pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10));
pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
return pageDomain;
}
/**
* 封装分页对象 会读取客户端传来的pageSize,客户端没传,会默认10条
*/
public static PageDomain getPageDomainByPost()
{
PageDomain pageDomain = new PageDomain();
pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameterByPost(PAGE_NUM), 1));
pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameterByPost(PAGE_SIZE), 10));
pageDomain.setOrderByColumn(ServletUtils.getParameterByPost(ORDER_BY_COLUMN));
pageDomain.setIsAsc(ServletUtils.getParameterByPost(IS_ASC));
pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
return pageDomain;
}
问题:
由于我自定义的方法startPageByPost(),看代码中的initAmount,因为startPageByPost()方法会将前端传来的驼峰写法改成下划线,init_amount
我一开始这么写
PageHelper.orderBy("initAmount desc");
startPageByPost()
结果:就会导致pageSize不生效
如果去掉startPageByPost(),变成这么写
PageHelper.orderBy("initAmount desc");
就会提示 initAmount 未定义
结论:
不需要startPageByPost()方法,手动修改成下划线写法,对应数据库的字段名
PageHelper.orderBy("init_amount desc");