- 分页查询的原理在于两个参数:
- 页码:pageno
- 页尺寸:pagesize
- 在接口StudentDao中添加方法:
public List<Student> findStudentByPage(Map map); //分页查询
分页要用到两个参数,由于sutdentList只能传递一个参数,所以采用Map类型封装多个参数的方法
2. 在映射文件StudentDaoImpl中添加语句:
<select id="findStudentByPage" resultType="com.hbfu.entity.Student" parameterType="java.util.Map">
select * from student limit #{startRow},#{pagesize}
</select>
- startRow:开始查询行
- pagesize:查询的数据量
- startRow=(pageno-1)*pagesize
- 在接口StudentDao实现类StudentDaoImpl中添加方法:
@Override
public List<Student> findStudentByPage(Map map) {
SqlSession session = null;
List<Student> studentList = new ArrayList<Student>();
try {
session = MyBatisUtil.getSession();
studentList = session.selectList("com.hbfu.dao.StudentDao.findStudentByPage",map);
}catch (Exception e){
e.printStackTrace();
}finally {
MyBatisUtil.closeSession();
}
return studentList;
}
- 在测试类StudentTest中添加代码:
Scanner scanner = new Scanner(System.in);
System.out.println("你想设置每页显示几条数据:");
int pagesize = scanner.nextInt();
System.out.println("你想查询第几页:");
int pageno = scanner.nextInt();
Map map = new HashMap();
map.put("startRow",(pageno-1)*pagesize);
map.put("pagesize",pagesize);
System.out.println(studentDao.findStudentByPage(map));
通过map.put(“startRow”,(pageno-1)*pagesize); 这条语句封装参数startRow的实参的值,这样传的是计算好的(pageno-1)*pagesize)的值
版权声明:本文为weixin_54139442原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。