Java神鬼莫测之MyBatis实现分页全过程(三)

举报
baidaguo 发表于 2022/05/26 00:28:54 2022/05/26
【摘要】 基本过程 1.Controller @RequestMapping("") @ResponseBody public Response userData(User user, Page...

基本过程

1.Controller

@RequestMapping("")
    @ResponseBody
    public Response userData(User user, Pager<User> pager, UserSearch<User> search) {
        //将用户账号查询设定为全模糊查询
        search.like(User::getUserCode, Search.LIKE_BOTH);
        //将用户名查询设定为全模糊查询
        search.like(User::getUserName, Search.LIKE_BOTH);
        //将查询设定为全模糊查询
        PageHelper.startPage(pager.getPageNum(), pager.getPageSize(), pager.get());
        List<UserDto> userDtoList = userService.findAll(user, search);
        PageResponse<UserDto> pageResponse = new PageResponse<>(userDtoList, pager);
        return Response.success(pageResponse);
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.Service

List<UserDto> findAll(User user, Search<User> search);

  
 
  • 1

3.ServiceImpl

@Override
    public List<UserDto> findAll(User user, Search<User> search) {
        return userDao.findAll(user,search);
    }

  
 
  • 1
  • 2
  • 3
  • 4

4.Dao

List<UserDto> findAll(@Param("user") User user, @Param("search") Search<User> search);

  
 
  • 1

5.Mapper

<select id="findAll" resultMap="userDtoMap">
        select
        <include refid="allColumn"/>
        from user
        where 1=1
            <if test="user.id != null">
                and id = #{user.id}
            </if>
            <if test="user.userCode != null and ''!= user.userCode">
                and user_code = #{user.userCode}
            </if>
            <if test="user.userName != null and ''!= user.userName">
                and user_name = #{user.userName}
            </if>
            <if test="user.password != null and ''!= user.password">
                and password = #{user.password}
            </if>
            <if test="user.typeCode != null and ''!= user.typeCode">
                and type_code = #{user.typeCode}
            </if>
            <if test="user.groupCode != null and ''!= user.groupCode">
                and group_code = #{user.groupCode}
            </if>
            <if test="user.address != null and ''!= user.address">
                and address = #{user.address}
            </if>
            <if test="user.mobile != null and ''!= user.mobile">
                and mobile = #{user.mobile}
            </if>
            <if test="user.phoneNum != null and ''!= user.phoneNum">
                and phone_num = #{user.phoneNum}
            </if>
            <if test="user.eMail != null and ''!= user.eMail">
                and e_mail = #{user.eMail}
            </if>
            <if test="user.roleId != null">
                and role_id = #{user.roleId}
            </if>
            <if test="user.userLock != null">
                and user_lock = #{user.userLock}
            </if>
                and is_del != 2
    </select>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

分页使用到工具类

1.Pager

public class Pager<T> {
    /**
     * 升序
     */
    public static final String SORT_ASC = "asc";
    /**
     * 降序
     */
    public static final String SORT_DESC = "desc";
    /**
     * 存储排序的集合
     */
    private List<String> sortInfo = new ArrayList<>();
    /**
     * url地址
     */
    private String pageUrl;
    /**
     * 当前页
     */
    private Integer pageNum = 1;
    /**
     * 每页分页数
     */
    private Integer pageSize = 20;

    /**
     * 添加排序信息
     *
     * @author fengzx
     * @date 2020/3/26 19:55
     */
    public Pager<T> add(IGetter<T> fn, String sort) {
        //属性名
        String propertyName = BeanUtils.convertToFieldName(fn);
        String columnName = BeanUtils.HumpToUnderline(propertyName);
        sortInfo.add(columnName + " " + sort);
        return this;
    }

    /**
     * 获取排序信息
     *
     * @author fengzx
     * @date 2020/3/26 19:54
     */
    public String get() {
        return StringUtils.join(sortInfo.toArray(), ",");
    }


    public String getPageUrl() {
        return pageUrl;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

2.来自com.github.pagehelper.PageHelper的PageHelper类

3.返回结果类

public class PageResponse<T> extends PageInfo<T> {
    /**
     * ajax请求地址
     */
    private String pageUrl;

    public PageResponse(List<T> list, Pager pager) {
        super(list);
        this.pageUrl = pager.getPageUrl();
    }

    public String getPageUrl() {
        return pageUrl;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

返回结果

{
  "type": "success",
  "data": {
    "navigatepageNums": [
      1
    ],
    "startRow": 1,
    "hasNextPage": false,
    "prePage": 0,
    "nextPage": 0,
    "endRow": 3,
    "pageSize": 20,
    "list": [
      {
        "gmtModified": 1624949479000,
        "address": "www",
        "gmtUserLock": 1614046814000,
        "gmtCreated": 1614046814000,
        "roleId": 100,
        "mobile": "13958352935",
        "phoneNum": "13958352935",
        "userLock": 1,
        "userName": "管理员",
        "userCode": "admin",
        "eMail": "2222",
        "typeCode": "100-100100",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "id": 1,
        "isDel": 1,
        "groupCode": "100-100100",
        "hospitalDeptName": "西京医院-眼科"
      },
      {
        "gmtModified": 1625120041000,
        "address": "123123",
        "gmtCreated": 1625120041000,
        "roleId": 100,
        "mobile": "213123",
        "phoneNum": "123123",
        "userLock": 1,
        "userName": "312",
        "userCode": "123123",
        "eMail": "123123",
        "typeCode": "100-100100",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "id": 13,
        "isDel": 1,
        "groupCode": "100-100100",
        "hospitalDeptName": "西京医院-眼科"
      },
      {
        "gmtModified": 1625191309000,
        "address": "杭州市滨江区",
        "gmtCreated": 1625191218000,
        "roleId": 100,
        "mobile": "13839448710",
        "phoneNum": "13839448710",
        "userLock": 1,
        "userName": "test1",
        "userCode": "test",
        "eMail": "13839448710@163.com",
        "typeCode": "100-100100",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "id": 14,
        "isDel": 1,
        "groupCode": "100-100100",
        "hospitalDeptName": "西京医院-眼科"
      }
    ],
    "pageNum": 1,
    "navigatePages": 8,
    "navigateFirstPage": 1,
    "total": 3,
    "pages": 1,
    "size": 3,
    "isLastPage": true,
    "hasPreviousPage": false,
    "navigateLastPage": 1,
    "isFirstPage": true
  },
  "code": null,
  "msg": null
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

文章来源: baidaguo.blog.csdn.net,作者:白大锅,版权归原作者所有,如需转载,请联系作者。

原文链接:baidaguo.blog.csdn.net/article/details/118419055

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。