Mybatis-07-分頁
阿新 • • 發佈:2020-09-19
-
-
方式一:使用limit實現分頁
-
介面類:UserMapper
public interface UserMapper {
List<User> selectLimit(Map<String,Integer> map);
} -
介面的實現類:UserMapper.xml
-
測試
public static void selectLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",1);
map.put("pageSize",3);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
-
-
方式二:使用RowBounds實現分頁
-
介面類:UserMapper
public interface UserMapper {
List<User> selectRowBounds();
} -
介面的實現類:UserMapper.xml
-
測試
public static void selectRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
/*
RowBounds(int offset, int limit)
RowBounds(開始位置, 查詢個數)
*/
RowBounds rowBounds = new RowBounds(1, 2);
/*
selectList(String var1, Object var2, RowBounds var3)
selectList(介面類的全限定名.方法名,一般寫null即可, RowBounds物件)
*/
List<User> userList = sqlSession.selectList("com.hmx.mapper.UserMapper.selectRowBounds", "null", rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
-
-
方式三:使用分頁外掛PageHelper進行分頁
如需使用,可參考以下連結: