Guns实现服务端分页并解决表格字段不能排序的问题

山涧抚琴曲意浓,马蹄轻骑过客心。叹探贪弹啊!

1.在前端分页的时候client我们只要sortable=true就会出现升序和降序的图标并实现排序,但当我们变成server端分页时,该排序方法就行不通了,如果我们也想在服务端动态的排序,那么接下来就是开挂的时候啦,小板凳拉好,瓜子西瓜已到位,灯光开启,走你。

我们再点击上下排序的剪头的时候我们可以发现Headers里面,已经帮我们传递了sortOrder,sortName排序字段,我们只需要获取他传给后台处理

Guns实现服务端分页并解决表格字段不能排序的问题_第1张图片

controller层

@RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String order_id, String mer_name, String merchant_bn, String beginTimestamp, String endTimestamp, String sourceId, String paymentId, String payStatusId, String contactMobile, String contactPerson, String sourceName, String pageNumber, String pageSize,@RequestParam(value="sortOrder",required = false)String sortOrder,@RequestParam(value="sortName",required = false)String sortName) {
        //排序参数处理驼峰转下划线
        if(StringUtils.isNotBlank(sortName) && sortName != null){
            sortName = UnderlineToCamelUtils.camelToUnderline(sortName);
        }
        logger.info("-----------开始查询订单表格接收参数:【pageNumber:"+pageNumber+"】【pageSize:"+pageSize+"】【sortOrder:"+sortOrder+"】【sortName:"+sortName+"】-----------");
        long startTime = System.currentTimeMillis();
        //查询表格数据
        int page_number = Integer.valueOf(pageNumber);
        int page_size = Integer.valueOf(pageSize);

        Page  sdbB2cOrdersPage = sdbB2cOrdersService.querySdbB2cOrderList2(order_id,mer_name,merchant_bn,beginTimestamp,endTimestamp,sourceId,paymentId,payStatusId,contactMobile,contactPerson,sourceName,page_number,page_size,sortOrder,sortName);
        if(sdbB2cOrdersPage != null){
            List records = sdbB2cOrdersPage.getRecords();
            for (SdbB2cOrders record : records) {
                String createtime = record.getCreatetime();
                record.setCreatetime(TimeUtil.toDate(createtime));
            }
            HashMap map = new HashMap<>();
            map.put("total",sdbB2cOrdersPage.getTotal());
            map.put("rows",records);
            long endTime = System.currentTimeMillis();
            logger.info("-------------查询订单表格总计用时:【"+(endTime-startTime)+"】--------------");
            return map;
        }
        return null;

    }

2.service层

    Page querySdbB2cOrderList2(String order_id, String mer_name, String merchant_bn, String beginTimestamp, String endTimestamp, String sourceId, String paymentId, String payStatusId, String contactMobile, String contactPerson, String sourceName, int pageNumber, int pageSize,String sortOrder,String sortName);

3.serviceImpl

 @Override
    public Page querySdbB2cOrderList2(String order_id, String mer_name, String merchant_bn, String beginTimestamp, String endTimestamp, String sourceId, String paymentId, String payStatusId, String contactMobile, String contactPerson, String sourceName, int pageNumber, int pageSize,String sortOrder,String sortName) {
        Page sdbB2cOrdersPage = new Page<>(pageNumber, pageSize);
        Page sdbB2cOrdersPage1 = sdbB2cOrdersPage.setRecords(baseMapper.querySdbB2cOrderList2(order_id, mer_name, merchant_bn, beginTimestamp, endTimestamp, sourceId, paymentId, payStatusId, contactMobile, contactPerson, sourceName, sdbB2cOrdersPage,sortOrder,sortName));
        return sdbB2cOrdersPage1;
    }

4.mapper层

List querySdbB2cOrderList2(@Param("order_id") String order_id, @Param("mer_name") String mer_name, @Param("merchant_bn") String merchant_bn, @Param("beginTimestamp") String beginTimestamp, @Param("endTimestamp") String endTimestamp, @Param("sourceId") String sourceId,@Param("paymentId")String paymentId,@Param("payStatusId")String payStatusId,@Param("contactMobile") String contactMobile,@Param("contactPerson") String contactPerson,@Param("sourceName")String sourceName, Page sdbB2cOrdersPage,@Param("sortOrder")String sortOrder,@Param("sortName")String sortName);

5.mapper.xml

 

5.因为在order by 后是表格里的列名,而再传参时,浏览器已经帮我们处理成了驼峰形式,所以记得转为列名

        //排序参数处理驼峰转下划线
        if(StringUtils.isNotBlank(sortName) && sortName != null){
            sortName = UnderlineToCamelUtils.camelToUnderline(sortName);
        }

 下划线和驼峰转换工具

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 驼峰法-下划线互转
 * @author cshaper
 * @since 2015.07.04
 * @version 1.0.0
 */
public class UnderlineToCamelUtils {
  
 /**
  * 下划线转驼峰法
  * @param line 源字符串
  * @param smallCamel 大小驼峰,是否为小驼峰
  * @return 转换后的字符串
  */
 public static String underlineToCamel(String line,boolean smallCamel){
  if(line==null||"".equals(line)){
   return "";
  }
  StringBuffer sb=new StringBuffer();
  Pattern pattern=Pattern.compile("([A-Za-z\\d]+)(_)?");
  Matcher matcher=pattern.matcher(line);
  while(matcher.find()){
   String word=matcher.group();
   sb.append(smallCamel&&matcher.start()==0?Character.toLowerCase(word.charAt(0)):Character.toUpperCase(word.charAt(0)));
   int index=word.lastIndexOf('_');
   if(index>0){
    sb.append(word.substring(1, index).toLowerCase());
   }else{
    sb.append(word.substring(1).toLowerCase());
   }
  }
  return sb.toString();
 }
 /**
  * 驼峰法转下划线
  * @param line 源字符串
  * @return 转换后的字符串
  */
 public static String camelToUnderline(String line){
  if(line==null||"".equals(line)){
   return "";
  }
  line=String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
  StringBuffer sb=new StringBuffer();
  Pattern pattern=Pattern.compile("[A-Z]([a-z\\d]+)?");
  Matcher matcher=pattern.matcher(line);
  while(matcher.find()){
   String word=matcher.group();
   sb.append(word.toUpperCase());
   sb.append(matcher.end()==line.length()?"":"_");
  }
  return sb.toString();
 }
}

OK当你在点击排序的时候就可以正常排序了,而且数据是服务端分页

 

你可能感兴趣的:(java)