共计 1178 个字符,预计需要花费 3 分钟才能阅读完成。
思考:为什么要分页?
缩小数据的处理量
1、应用 Limit 分页
语法:
select * from user limit startIndex,pageSize;
select * from user limit 3;#[0,n]
应用 Mybatis 实现分页,外围 SQL
接口
/**
* 分页查问
* @return
*/
List<User> getUserByLimt(Map<String,Integer> map);
Mapper.xml
<!– 分页实现查问 –>
<select id=”getUserByLimt” parameterType=”map” resultMap=”UserMap”>
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
// 测试
public void getUserByLimt(){SqlSession sqlsession = MybatisUtil.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",3);
List<User> userByLimt = mapper.getUserByLimt(map);
for (User user : userByLimt) {System.out.println(user);
}
}
2、RowBounds 分页
不在应用 SQL 实现分页
接口
/**
*
* @return
*/
List<User> getUserByRowBounds();
Mapper.xml
<!– 分页实现查问 –>
<select id="getUserByRowBounds" resultMap="UserMap">
select * from mybatis.user
</select>
// 测试
public void getUserByRowBounds(){SqlSession sqlsession = MybatisUtil.getSqlsession();
//RowBounds 实现
RowBounds rowBounds = new RowBounds(1, 2);
// 通过 java 层面实现分页
List<User> list = sqlsession.selectList("com.aostarit.dao.UserMapper.getUserByRowBounds",null,rowBounds);
for (User user : list) {System.out.println(user);
}
// 敞开 sqlSession
sqlsession.close();}
正文完