码迷,mamicode.com
首页 > 编程语言 > 详细

Spring data Jpa 分页从1开始,查询方法兼容 Mybatis,分页参数兼容Jqgrid

时间:2018-06-09 13:25:19      阅读:1826      评论:0      收藏:0      [点我收藏+]

标签:nat   des   main   over   efi   real   color   util   long   

废话少说

有参数可以设置

在org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties 中

        /**
         * Whether to expose and assume 1-based page number indexes. Defaults to "false",
         * meaning a page number of 0 in the request equals the first page.
         */
        private boolean oneIndexedParameters = false;

所以在application.yml中

spring:
  data:
    web:
      pageable:
        default-page-size: 20
        size-parameter: rows
        one-indexed-parameters: true

 

兼容Mybatis 分页查询

    /**
    * laizhenwei
    * @param OrderPage
    * @return org.springframework.data.domain.Page<Order>
    */     
    @Override
    public Page<Order> page(OrderPage OrderPage){
        return this.readPage( OrderPage.getPageable(), OrderPage);
    }

    /**
    * laizhenwei
    * @param pageable
    * @param OrderPage
    * @return org.springframework.data.domain.Page<Order>
    */ 
    @Override
    public Page<Order> readPage(Pageable pageable, @Nullable OrderPage OrderPage) {
        return  PageableExecutionUtils.getPage(getMapper().page(OrderPage), pageable, () -> getMapper().pageCount(OrderPage));
    }

 

mybatis pageCount方法

      <!-- 条件分页查询,数据 -->
    <select id="pageCount" resultType="long" parameterType="OrderPage">
        SELECT COUNT(o.id) FROM `order` o LEFT JOIN `user` u ON o.`user_id` = u.`id`
        <trim prefix="where" prefixOverrides="and|or">
            <if test="status!=null">
                AND o.status = #{status}
            </if>
            <if test="billStatus!=null">
                AND o.bill_status=#{billStatus}
            </if>
            <if test="type!=null">
                AND o.type =#{OrderPage.type}
            </if>

            <if test="realName!=null">
                AND u.real_name LIKE CONCAT(‘%‘,#{realName},‘%‘)
            </if>
            <if test="origin!=null and origin!=‘‘">
                AND o.origin LIKE  CONCAT(‘%‘,#{origin},‘%‘)
            </if>
            <if test="destination!=null and destination!=‘‘">
                AND o.destination LIKE CONCAT(‘%‘,#{destination},‘%‘)
            </if>
        </trim>
    </select>

controller

     @RequestMapping(path = "/page",method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo<Page<Order>> page(@PageableDefault Pageable pageable, @ModelAttribute OrderPage orderPage){
        ResponseVo<Page<Order>> responseVo = ResponseVo.success();
        orderPage.setPageable(pageable);
        return responseVo.setData(orderService.page(orderPage));
    }

传入参数

技术分享图片

结果

技术分享图片

 

Spring data Jpa 分页从1开始,查询方法兼容 Mybatis,分页参数兼容Jqgrid

标签:nat   des   main   over   efi   real   color   util   long   

原文地址:https://www.cnblogs.com/sweetchildomine/p/9158806.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!