分页
limit分页:
SELECT * from user limit startIndex,pageSize;
Mybatis分页,基于SQL
1.接口
List<User> getUserByLimit(Map<String, Object> map);
2.mapper
<select id="getUserByLimit" parameterType="map" resultType="user">
select * from user limit #{startIndex},#{pageSize}
</select>
3.测试
@Test
public void getUserLimit() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("startIndex", 2);
map.put("pageSize", 2);
List<User> userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
sqlSession.close();
}
RowBounds分页
不再基于SQL,基于JAVA对象实现 1.接口
List<User> getUserByRowBounce(Map<String, Object> map);
2.mapper
<select id="getUserByRowBounce" resultType="user">
select * from user
</select>
3.测试使用:
@Test
public void getUserByRowBounceTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RowBounds rowBounds = new RowBounds(1, 2);
List<User> objects = sqlSession.selectList("com.lms.dao.UserMapper.getUserByRowBounce", null, rowBounds);
for (User object : objects) {
System.out.println(object);
}
sqlSession.close();
}
注解开发
核心使用反射进行底层实现。 1.注解在接口上实现
@Select("select * from user")
List<User> getUsers();
2.核心配置文件绑定
<mappers>
<mapper class="com.lms.dao.UserMapper"/>
</mappers>
3.测试
本质:反射机制 底层:动态代理
|