mybatis不仅支持分页,它还内置了一个专门处理分页的类-RowBounds。
下面详细讲一个根据姓名模糊查询,并且将结果分页显示
实例:
1.UserMapper.java(方法接口)
public List<User> findByRowBounds(@Param("name") String name, RowBounds rowBounds);
2.UserMapper.xml(接口映射文件)
<select id="findByRowBounds" resultType="user">
select id, name, age from user where name like concat('%',#{name},'%')
</select>
3.Test(测试类)
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
RowBounds rowBounds = new RowBounds(0, 4);
List<User> list = mapper.findByRowBounds("张", rowBounds);
for (User user : list) {
System.out.println(user);
}
运行结果:
User [id=1, name=张三, age=23]
User [id=3, name=张无忌, age=30]
User [id=6, name=张良, age=30]
User [id=7, name=张秀文, age=30]