Mybatis分页查询同时返回总数和数据

功能要求:

我们在使用Mybatis分页查询数据列表时,在用户的一个请求中常常需要同时返回当前页的列表数据以及满足条件的数据总条数用于分页。

实现方案

1)执行两次SQL,一次查列表,一次查总数

      这种方法最简单,也最容易实现。

2)分页插件PageHelper

      另一种常用的方式就是使用Mybatis提供的PageHelper插件。实际上PageHelper插件的原理同1)一样,就是执行两次SQL查询。

3)通过特殊的Mybatis语法,只执行一次SQL查询。这个功能要求connectionUrl参数包含allowMultiQueries=true,对于如zebra等集成工具,就算配了allowMultiQueries=true,也不一定起作用。

代码如下:

<select id="queryListAppendTotal"
        parameterType="com.domain.OrderExample"
        resultMap="BaseResultMap, recordCounts">
    select
    SQL_CALC_FOUND_ROWS
    <include refid="Base_Column_List"/>
    from order_example
    <if test="_parameter != null">
        <include refid="Example_Where_Clause"/>
    </if>
    <if test="orderByClause != null">
        order by ${orderByClause}
    </if>
    <if test="limit != null">
        <if test="offSet != null">
            limit ${offSet}, ${limit}
        </if>
        <if test="offSet == null">
            limit ${limit}
        </if>
    </if>
    ;SELECT found_rows() AS recordCounts;
</select>
 
 
 
<resultMap id="BaseResultMap" type="com.domain.OrderExample">
    <id column="id" jdbcType="BIGINT" property="id"/>
    <result column="order_id" jdbcType="VARCHAR" property="orderId"/>   
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
</resultMap>
 
 
<resultMap id="recordCounts" type="java.lang.Long">
  <result column="recordCounts" jdbcType="BIGINT"/>
</resultMap>

注意:在使用时须要在配置文件中,设置容许sql进行多语句执行:allowMultiQueries=true,在sql的url上加上这个配置就能够了

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用MyBatis进行分页查询,可以通过PageHelper插件来实现。首先,确保你已经在项目中引入了PageHelper依赖。可以在`pom.xml`文件中添加以下代码: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 接下来,在MyBatis的配置文件中,配置PageHelper插件。可以在`application.properties`文件中添加以下配置: ```properties # 设置分页插件 pagehelper.helper-dialect=mysql pagehelper.params=count=countSql ``` 然后,在你的Mapper接口中定义查询方法。例如,假设你要查询名为`User`的表,并进行分页查询,可以创建一个类似如下的方法: ```java import com.github.pagehelper.Page; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { Page<User> findByPage(); } ``` 最后,在你的Service或Controller层中调用该方法,并传入页码参数来实现分页查询。例如,假设你要查询第2页的数据,可以这样调用: ```java import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Service public class UserService { @Autowired private UserMapper userMapper; public PageInfo<User> getUsersByPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); Page<User> page = userMapper.findByPage(); return new PageInfo<>(page); } } ``` 这样,`getUsersByPage`方法将返回一个包含分页数据的`PageInfo`对象,其中包括了总页数等信息。 请注意,以上示例是基于MySQL数据库的配置和使用方法。如果你使用的是其他数据库,需要相应地修改`pagehelper.helper-dialect`配置项。 希望以上信息对你有帮助!如有更多问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值