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

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

基本过程


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);
    }

2.Service

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

3.ServiceImpl

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

4.Dao

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

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.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;
    }
}

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;
    }
}

返回结果

{
  "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
}


目录
相关文章
|
3月前
|
SQL Java 数据库连接
MyBatis分页
MyBatis作为Java持久层框架,需结合数据库特性或插件实现分页。分页分为物理分页(如MySQL的LIMIT)和逻辑分页(内存截取),推荐使用PageHelper插件自动注入分页语句,提升开发效率与性能。需注意索引优化、深分页问题及多表关联时的兼容性,结合业务场景选择合适方案。
144 4
|
9月前
|
SQL Java 数据库连接
微服务——MyBatis分页
本文介绍了分页的多种实现方式,包括自带RowBounds分页、第三方插件PageHelper分页、SQL分页、数组分页及拦截器分页。其中,RowBounds是先查询全部结果再内存分页;PageHelper通过修改SQL动态添加分页关键字;SQL分页依赖数据库自身的分页功能如`LIMIT`;数组分页则是查询全量数据后用`subList`方法截取;拦截器分页则统一在SQL后添加分页语句。最后总结逻辑分页适合小数据量,但大数据量易内存溢出;物理分页虽小数据量效率较低,但更适合大数据场景,优先推荐使用。
121 0
|
9月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】Mybatis-Plus调用YashanDB怎么设置分页
【YashanDB知识库】Mybatis-Plus调用YashanDB怎么设置分页
|
7月前
|
SQL Java 数据安全/隐私保护
发现问题:Mybatis-plus的分页总数为0,分页功能失效,以及多租户插件的使用。
总的来说,使用 Mybatis-plus 确实可以极大地方便我们的开发,但也需要我们理解其工作原理,掌握如何合适地使用各种插件。分页插件和多租户插件是其中典型,它们的运用可以让我们的代码更为简洁、高效,理解和掌握好它们的用法对我们的开发过程有着极其重要的意义。
695 15
|
6月前
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
246 1
|
7月前
|
SQL Java 数据库连接
Java中实现SQL分页的方法
无论何种情况,选择适合自己的,理解了背后的工作原理,并能根据实际需求灵活变通的方式才是最重要的。
199 9
|
10月前
|
XML SQL Java
十二、MyBatis分页插件
十二、MyBatis分页插件
295 17
|
10月前
|
监控 安全 数据库
【YashanDB 知识库】Mybatis-Plus 调用 YashanDB 怎么设置分页
数据库状态分为正常与异常两种情况。当出现异常时,首先查看告警列表确认问题(如实例无法连接),并尝试用数据库用户名和密码登录。若能登录,说明主实例故障已切换至备库;若无法登录或为单节点,则需进一步排查。接着检查监控项,若有数据表明主实例故障,无数据则可能是通信中断。随后检查主机上的服务是否存在,若存在但通信受限,需排查安全设置或网络;若服务不存在,可能因重启或断电导致,需手动启动相关服务。最终在YashanDB列表中确认状态恢复。
|
9月前
|
SQL Java 关系型数据库
MyBatis篇-分页
本文介绍了多种分页方式,包括自带rowbound内存分页、第三方插件pagehelper(通过修改SQL实现分页)、SQL分页(依赖limit或rownum等关键字)、数组分页(先查询全部数据再用subList分页)、拦截器分页(自定义拦截器为SQL添加分页语句)。最后总结了逻辑分页(内存分页,适合小数据量)和物理分页(直接在数据库层面分页,适合大数据量)的优缺点,强调物理分页优先于逻辑分页。
|
9月前
|
SQL Java 数据库连接
MyBatis 实现分页的机制
MyBatis 的分页机制主要依赖于 `RowBounds` 对象和分页插件。`RowBounds` 实现内存分页,适合小数据量场景,通过设定偏移量和限制条数对结果集进行筛选。而针对大数据量,则推荐使用分页插件(如 PageHelper),实现物理分页。插件通过拦截 SQL 执行,动态修改语句添加分页逻辑,支持多种数据库方言。配置插件后,无需手动调整查询方法即可完成分页操作,提升性能与灵活性。
201 0