MyBatis学习笔记(八)分页查询

来源:互联网 发布:艺考生软件怎么样 编辑:程序博客网 时间:2024/06/05 15:39

1.逻辑分页

这种方式是将全部数据放到内存中再分页的,与jsp类似


Mapper接口方法:

public List<Student> getAllStudents1(RowBounds rowBounds);
映射:

<select id="getAllStudents1" resultMap="studentMap">select * from mybatis_Student</select>
和全查询一样,并没有使用rowBounds参数

测试:

@Testpublic void testLogicPaging(){logger.info("测试逻辑分布");StudentMapper mapper = session.getMapper(StudentMapper.class);/** * RowBounds参数:offset:数据偏移 * limit:每页大小 */int pageIndex = 1; //第几页int limit = 2;     //每页大小List<Student> list = mapper.getAllStudents1(new RowBounds(limit* (pageIndex-1) ,limit));for(Student stu : list){logger.info(stu.toString());}}

2,物理分页

查询表里的部分数据.

Mapper接口方法:

public List<Student> getAllStudents2(Map<String,Object> map);
映射:

<select id="getAllStudents2" resultMap="studentMap">select * from mybatis_Student<if test="start!=null and limit!=null">limit #{start},#{limit}</if></select>


测试:

@Testpublic void testPhysicPaging(){logger.info("测试物理分布");StudentMapper mapper = session.getMapper(StudentMapper.class);/** * RowBounds参数:offset:数据偏移 * limit:每页大小 */int pageIndex = 2; //第几页int limit = 2;     //每页大小Map<String,Object> map = new HashMap<String,Object>();map.put("start",limit* (pageIndex-1) );map.put("limit", limit);List<Student> list = mapper.getAllStudents2(map);for(Student stu : list){logger.info(stu.toString());}}}





0 0
原创粉丝点击