mysql数据库中使用mybatis的xml文件limit分页使用遇到的坑

30 篇文章 5 订阅
6 篇文章 0 订阅

数据库:MySQL数据库

因为初次使用mybatis-plus的分页查询,有点不太不习惯,于是决定自己写个分页查询。

mysql中的分页查询语句,是使用limit来分页的:

select * from tablename  limit a,b;//这句话的意思是从表tablename中查询,从第a+1条开始,共查询b条数据。

那么在这里,a和b的含义就要了解明白了,要不然就容易犯我的错(我刚开始理解的意思是,从a页开始,查询b条数据)。

概念清晰的话,涉及到分页,就需要了解几个计算公式了:

//总记录数=查询符合条件的总数 SELECT COUNT(1) FROM tablename WHERE 条件;
Integer count ;
//每页记录数=每页查询的条数,一般都是前端传过来的
Integer pageSize ;
//当前页数=当前页数,一般都是前端传过来的想要查询的哪页
Integer currPage ;
//总页数= (总记录数 + 每页记录数 - 1)/每页记录数
Integer totalPage = (count + pageSize - 1) / pageSize;

明白上面的几个概念之后,可以进行操作了。

请注意,sql语句不能写成:select * from tablename  limit currPage ,pageSize ;//这是错误的

应该计算一下select * from tablename  limit a,b;语句中的a(一定要注意,a不代表第几页,而是代表从a+1条开始),

a的计算:(当前页-1)* 每页记录数      ==>  (currPage - 1) * pageSize

这样的话你的分页就可以啦。

我的分页数据也是自己封装回去的,可以参考下,我是用的是fastjson:

JSONObject jsonObject = new JSONObject();
jsonObject.put("totalCount", count);
jsonObject.put("pageSize", pageSize);
jsonObject.put("totalPage", totalPage);
jsonObject.put("currPage", currPage);
jsonObject.put("list", list);

样例代码:

//列表数据
......
String page = params.get("page").toString();
String limit = params.get("limit").toString();
int i = Integer.parseInt(page);
int b = Integer.parseInt(limit);
params.put("start",(i-1)*b);
List<Student> list = dao.queryByList(params)

//mybatis中的sql:

 <select id="queryByList" resultMap="aMap">
        select
        <include refid="Base_Column_List"/>
        from student where
        <if test="ids != null">
            teacher_id in
            <foreach collection="ids" item="item" open="(" separator="," close=")">
                #{item , jdbcType=INTEGER}
            </foreach>
        </if>
        and enabled = 1
        limit ${start},${limit};
</select>


//总记录数
Integer count = aService.coutByIdList(idList);

<select id="coutByhospitalIdList" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM student WHERE hospital_id in
        <if test="list != null">
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item , jdbcType=INTEGER}
            </foreach>
        </if>
        <if test="idcard != null and idcard != ''">
            and idcard = #{idcard}
        </if>
        and enabled = 1
</select>





//每页记录数
Integer pageSize = Integer.parseInt(params.get("limit").toString());
//当前页数
Integer currPage = Integer.parseInt(params.get("page").toString());
//总页数
Integer totalPage = (count + pageSize - 1) / pageSize;

JSONObject jsonObject = new JSONObject();
jsonObject.put("totalCount", count);
jsonObject.put("pageSize", pageSize);
jsonObject.put("totalPage", totalPage);
jsonObject.put("currPage", currPage);
jsonObject.put("list", list);
return R.ok().put("data", jsonObject);

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis集成Predicate使用XML方式实现分页,我们可以使用MyBatis提供的分页插件PageHelper,它可以帮助我们自动进行分页处理,简化了分页的实现过程。 在使用PageHelper之前,我们需要在MyBatis的配置文件配置分页插件,示例代码如下: ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> </plugin> </plugins> ``` 在上述代码,我们使用PageInterceptor作为分页插件,并指定数据库MySQL。 然后,在Mapper接口,我们定义一个方法来接收Predicate对象和分页参数,并将其作为参数传递给XML定义的SQL语句。示例代码如下: ``` public interface UserMapper { List<User> findUserByPredicate( @Param("predicate") Predicate<User> predicate, @Param("page") PageRequest pageRequest); } ``` 在上述代码,我们使用PageRequest类来封装分页参数,包括页码和每页记录数。然后,在XML,我们可以使用PageHelper提供的语法来进行分页查询,示例代码如下: ``` <select id="findUserByPredicate" resultMap="userResultMap"> SELECT * FROM user <where> <if test="predicate != null"> AND <foreach collection="predicate.conditions" item="condition" separator=" AND "> ${condition.property} ${condition.operator} #{condition.value} </foreach> </if> </where> <if test="page != null"> ORDER BY id LIMIT #{page.offset}, #{page.pageSize} </if> </select> ``` 在上述代码,我们使用if标签来判断传入的分页参数是否为null,如果不为null,则使用LIMIT子句来进行分页查询。其,#{page.offset}表示查询偏移量,#{page.pageSize}表示每页记录数。 最后,在调用Mapper接口方法时,我们可以传入Predicate对象和分页参数,示例代码如下: ``` Predicate<User> predicate = ...; PageRequest pageRequest = new PageRequest(1, 10); List<User> userList = userMapper.findUserByPredicate(predicate, pageRequest); ``` 在上述代码,我们使用PageRequest类来构造分页参数,并将其传递给Mapper接口方法。然后,PageHelper会自动进行分页处理,返回符合条件的记录列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值