• 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进行接口测试

     

  • 相关阅读:
    Web负载均衡的几种实现方式
    Apache和Nginx的区别
    Nginx和Apache区别
    Git 使用中显示“Another git process seems to be running in this repository...”问题解决
    上传本地代码到gitHub过程详解
    MySQL数据库中varchar与char类型的区别
    正则表达式中/i,/g,/ig,/gi,/m的区别和含义
    内行看门道:看似“佛系”的《QQ炫舞手游》,背后的音频技术一点都不简单
    惧怕羊毛党?腾讯云为你保驾护航
    教你1天搭建自己的“微视”
  • 原文地址:https://www.cnblogs.com/personblog/p/13949061.html
Copyright © 2020-2023  润新知