sql中limit的语法
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
先在接口mapper中定义方法。
List<User> getUserByLimit(Map<String,Integer> map);
然后到mapper.xml中进行注册
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
select * from mybatis.user limit #{value1},#{value2}
</select>
这里面的value1,value2可以随便写,因为只是map中的value值,只要跟测试的时候对得上就行。
@Test
public void testLimit(){
Map<String, Integer> map = new HashMap<>();
map.put("value1",0);
map.put("value2",3);
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
sqlSession.close();
}
本文介绍了如何在MyBatis中利用LIMIT子句进行分页查询,通过在Mapper接口和Mapper XML文件中设置方法和SQL语句,实现对数据表的指定偏移量和数量的记录进行选取。测试代码展示了如何传入参数并获取分页后的用户列表。

2596

被折叠的 条评论
为什么被折叠?



