mybatis学习之分页

分页一般分为物理分页:先查询所有值再分页输出,逻辑分页:直接分页查询输出,mybatis支持物理分页,如下:

1、物理分页:

mapper映射:

<select id="findStudents" resultMap="StudentResult">
    select * from t_student order by id asc
</select>

Dao接口:

/**
     * mybatis分页查询-逻辑分页
     * @param rowBounds
     * @return
     */
    public List<Student> findStudents(RowBounds rowBounds);

测试:

/**
     * 逻辑分页----查询所有数据再分页输出
     * @throws Exception
     */
    @Test
    public void testRowBounds() throws Exception{
        logger.info("学生查询-逻辑分页");
        int offset = 3;        //start
        int limit = 3;        //size
        RowBounds rowBounds = new RowBounds(offset,limit);
        List<Student> studentList = studentDao.findStudents(rowBounds);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }

2、逻辑分页:

mapper映射:

<select id="findStudents2" parameterType="Map" resultMap="StudentResult">
    select * from t_student
    <if test="start != null and size != null">
        limit #{start},#{size}
    </if>
</select>

Dao接口:

/**
     * mybatis分页查询-物理分页
     * @param rowBounds
     * @return
     */
    public List<Student> findStudents2(Map<String, Object> map);

测试:

/**
     * 物理分页----查询分页数据再输出
     * @throws Exception
     */
    @Test
    public void testFind() throws Exception{
        logger.info("学生查询-物理分页");
        Map<String, Object> map = new HashMap<>();
        map.put("start", 0);
        map.put("size", 3);
        List<Student> studentList = studentDao.findStudents2(map);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }

 

转载于:https://my.oschina.net/caiya928/blog/784043

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值