Mybatis知識小匯(6)——分頁
阿新 • • 發佈:2021-02-10
技術標籤:mybatis
文章目錄
分頁
limit實現分頁
- 介面
//分頁查詢
List<User> getUserListByLimit(Map<String, Integer> map);
- UserMapper.xml
<!-- 分頁查詢-->
<select id="getUserListByLimit" resultMap="userMap" parameterType="map">
select * from user limit #{startIndex}, #{pageSize};
</ select>
- 測試
@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
- 介面
List<User> getUserListByRowBounds();
- UserMapper.xml
<select id=" getUserListByRowBounds" resultType="user">
select * from user;
</select>
- 測試
@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();
}
如有不對的地方歡迎指出,共同進步!