MyBatis中分页

MyBatis中分页

1.使用Limit分页

MyBatis实现分页,核心为SQL。

1.sql语句:

//分页语法:
select * from user limit startIndex,pageSize

2.接口:

 //分页
    List<User> queryByLimitUser(Map<String,Integer> map);

3.Mapper.xml :

 <select id="queryByLimitUser" resultType="zkx.lianxi.pojo.User">
        select * from user limit #{startIndex} , #{pageSize}
    </select>

4.测试:

@Test
    public void getUserByLimit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ApMapper mapper = sqlSession.getMapper(ApMapper.class);
        HashMap<String, Integer> ma = new HashMap<String, Integer>();
        ma.put("startIndex",2);
        ma.put("pageSize",3);
        List<User> users = mapper.queryByLimitUser(ma);
        for (User us : users) {
            System.out.println(us);
        }
        sqlSession.close();
    }

2.RowBounds 分页:

RowBounds 将完全脱离了sql语句去实现分页!

1.接口

//分页
    List<User> queryByRowBounds();

2.Mapper.xml

<select id="queryByRowBounds" resultType="zkx.lianxi.pojo.User">
        select * from user
    </select>

3.测试

 @Test
    public void queryByRowBounds(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //RowBounds实现
        RowBounds rowBounds = new RowBounds(2, 3);
        List<User> users = sqlSession.selectList("zkx.lianxi.dao.ApMapper.queryByRowBounds",null,rowBounds);
        for (User us : users) {
            System.out.println(us);
        }
        //关闭sqlsession
        sqlSession.close();
    }
上一篇:趋势转折的5个重要步骤


下一篇:BZOJ2429: [HAOI2006]聪明的猴子