Mybatis-PageHelper分页/动态排序/对List遍历/where语句动态拼装

一、Mybatis-PageHelper实现分页

    public ServerResponse<PageInfo> manageGetProductList(int pageNum, int pageSize){
        //startPage--start
        //填充自己的sql查询逻辑
        //pageHelper-收尾
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.selectProductList();

        List<ProductListVo> productListVoList = Lists.newArrayList();
        for (Product product : productList){
            ProductListVo productListVo = assembleProductListVo(product);
            productListVoList.add(productListVo);
        }
        //给前端的是productListVO,但是需要productList进行分页
        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

主要代码:

//startPage--start
//填充自己的sql查询逻辑
//pageHelper-收尾

//传入参数
PageHelper.startPage(pageNum, pageSize);
//对结果进行封装
PageInfo pageInfo = new PageInfo(productList);
//返回
return pageInfo ;

二、Mybatis-PageHelper实现动态排序

//排序处理 PageHelper的排序参数格式:price asc; price desc;
if (StringUtils.isNotBlank(orderBy)){
   if (Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
     String[] orderByArray = orderBy.split("_");
      PageHelper.orderBy(orderByArray[0] + " " + orderByArray[1]);
    }
}

传入的参数orderBy形式:price_asc或price_desc,传入到PageHelper中为price asc。

可以进行封装成枚举类或内部接口

    public interface ProductListOrderBy{
        //Set查询效率为O(1), List为O(n)
        Set<String> PRICE_ASC_DESC = Sets.newHashSet("price_desc","price_asc");
    }

三、Mybatis中对List遍历

当Mybatis中参数为List的时候,需要遍历List中参数。

如:

List<Product> selectByNameAndCategoryIds(@Param("keyword") String productName, @Param("categoryList") List<Integer> categoryList);

底层实现:

使用

<foreach></foreach>进行遍历。
  <select id="selectByNameAndCategoryIds" parameterType="map" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM
    mmall_product
    WHERE status = 1
    <if test="productName != null">
      AND  name LIKE #{productName}
    </if>
    <if test="categoryList != null">
      and category_id in
      <foreach collection="categoryList" item="item" index="index" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>
  </select>

 

四、Mybatis中where语句动态拼装

方法:

List<Product> selectByNameAndId(@Param("productName") String productName,
                                    @Param("productId") Integer productId);

当传入的参数productName和productId都有可能为null的时候,需要在底层实现进行判断。

where
<if test="productName != null">
        name LIKE #{productName}
 </if>
<if test="productId != null">
        AND id = #{productId}
 </if>

这样当productName为null的时候,整个语句变为

where and id = #{productId}

而使用<where>标签可以避免这个问题。

    <where>
      <if test="productName != null">
        AND name LIKE #{productName}
      </if>
      <if test="productId != null">
        AND id = #{productId}
      </if>
    </where>

当productName为null 的时候,会自动忽略productId前的AND。

完整实现:

  <select id="selectByNameAndId" parameterType="map" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM  mmall_product
    <where>
      <if test="productName != null">
        AND name LIKE #{productName}
      </if>
      <if test="productId != null">
        AND id = #{productId}
      </if>
    </where>
  </select>

 

转载于:https://www.cnblogs.com/noaman/p/8811326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值