Linit分页

1
2
语法:SELECT * from user limit startIndex, pageSze;
SELECT * from user limit 0, 3;

mapper.java

1
2
3
4
public interface UserMapper {
//分页
List<User> getUserByLimit(Map<String, Object> map);
}

mapper.xml

1
2
3
4
<!-- 分页-->
<select id="getUserByLimit" parameterType="map" resultType="com.xxx.pojo.User">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);

HashMap<String, Object> map = new HashMap<>();
map.put("startIndex", 0);
map.put("pageSize", 2);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(user);
}

sqlSession.close();
}