springboot 某个表有50000条数据mybatis 每次查询200条for循环查完
可以使用分页查询的方式来优化查询,分页查询可以通过limit和offset参数来实现。
在MyBatis中,可以使用RowBounds对象来设置分页参数,示例如下:
int pageSize = 200;
int total = 50000;
for (int offset = 0; offset < total; offset += pageSize) {
RowBounds rowBounds = new RowBounds(offset, pageSize);
List<YourEntity> result = yourMapper.selectByPage(rowBounds);
// 处理查询结果
for (YourEntity entity : result) {
// TODO: 处理每条数据
}
}
在yourMapper中定义一个selectByPage方法,该方法接收一个RowBounds参数,示例如下:
List<YourEntity> selectByPage(RowBounds rowBounds);
在Mapper XML中,可以编写对应的SQL语句来实现分页查询,示例如下:
<select id="selectByPage" parameterType="org.apache.ibatis.session.RowBounds" resultMap="YourEntityResultMap">
SELECT * FROM your_table
LIMIT #{offset}, #{limit}
</select>
在上述示例中,offset表示当前查询的起始位置,limit表示每次查询的条数,这两个参数会由RowBounds对象传递给SQL语句。
通过以上方式,可以实现每次查询指定数量的数据,然后在for循环中处理查询结果,直到查询完所有数据
原文地址: https://www.cveoy.top/t/topic/i0b9 著作权归作者所有。请勿转载和采集!