springboot +mybatis 使用PageHelper實現分頁,並帶條件模糊查詢


  • 調用接口Controller類

 

    @ApiOperation("查詢列表")
    @PostMapping("/selectList")
    public Result selectList(@RequestBody User_InfoListRequest userInfo ) {
        Page page = PageHelper.startPage(userInfo.pageNum,userInfo.pageSize); //這行是重點(放在方法第一行)表示從pageNum頁開始,每頁pageSize條數據
        List<User_info> _list= userService.selectList(userInfo);//返回集合和分頁之前不要有其它代碼,這兩行上下要緊靠近
        PageInfo pageInfo = new PageInfo<>(_list);
        return  Result.SUCCESS( pageInfo);
    }

 

package com.xj.demo.model.Request;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;

public class User_InfoListRequest extends  PageRequest  {

    @ApiModelProperty(value = "學生姓名")
    public String uname;

    @ApiModelProperty(value = "學生年齡")
    public int uage;
}

package com.xj.demo.model.Request;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 分頁請求request基類
 */
@Data
public class PageRequest  {

    @ApiModelProperty(value = "當前頁碼")
    public Integer pageNum;

    @ApiModelProperty(value = "分頁大小")
    public Integer pageSize;

}
  • 定義接口,注意紅色顯示部分
package com.xj.demo.mapper;

import com.github.pagehelper.Page;
import com.xj.demo.model.Request.UserEditRequest;
import com.xj.demo.model.Request.User_InfoListRequest;
import com.xj.demo.model.Response.UserInfoResponse;
import com.xj.demo.model.entity.User_info;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface User_infoMapper {

    // 查詢列表
     List<User_info> selectList(@Param("userInfo")  User_InfoListRequest userInfo);

    
}
  • 實現接口service
package com.xj.demo.service;

import com.github.pagehelper.Page;
import com.xj.demo.mapper.User_infoMapper;
import com.xj.demo.model.Request.UserEditRequest;
import com.xj.demo.model.Request.User_InfoListRequest;
import com.xj.demo.model.Response.UserInfoResponse;
import com.xj.demo.model.entity.User_info;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User_infoService implements User_infoMapper {

    @Autowired
    private  User_infoMapper uMapper;

    @Override
    public List<User_info> selectList(User_InfoListRequest userInfo) {
        return uMapper.selectList(userInfo);
    }

 

   
}

 

  • mapper中sql語句uname進行模糊查詢和年齡完全匹配
    <!---查詢列表-->
    <select id="selectList" resultType="User_info">
    select * from User_info where 1=1
     <if test="userInfo!=null">
        <if test="userInfo.uname!=null and userInfo.uname!='' ">
             and uname like '%' #{userInfo.uname} '%'
        </if>

         <if test="userInfo.uage!=0">
             and uage = #{userInfo.uage}
         </if>
     </if>
     order by create_time desc
    </select>
  • 使用postman進行接口測試

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM