1. 程式人生 > 其它 >Mybatis知識小匯(6)——分頁

Mybatis知識小匯(6)——分頁

技術標籤:mybatis

文章目錄

分頁

limit實現分頁
  1. 介面
//分頁查詢
List<User> getUserListByLimit(Map<String, Integer> map);
  1. UserMapper.xml
<!--    分頁查詢-->
<select id="getUserListByLimit" resultMap="userMap" parameterType="map">
    select * from user limit #{startIndex}, #{pageSize};
</
select
>
  1. 測試
@Test
public void getUserListByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
    hashMap.put("startIndex", 0
); hashMap.put("pageSize", 3); List<User> userList = mapper.getUserListByLimit(hashMap); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
RowBounds
  1. 介面
List<User> getUserListByRowBounds();
  1. UserMapper.xml
<select id="
getUserListByRowBounds"
resultType="user">
select * from user; </select>
  1. 測試
@Test
public void getUserListByRowBounds(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    RowBounds rowBounds = new RowBounds(0, 3);
    List<User> users = sqlSession.selectList("com.wjq.dao.UserMapper.getUserListByRowBounds", null, rowBounds);
    for (User user : users) {
        System.out.println(user);
    }
    sqlSession.close();
}

上一節—>日誌

如有不對的地方歡迎指出,共同進步!