java中mybatis的分页要借助map的原理,在下文中会展开详细的叙述。同时也可以了解LIMIT关键字的分页方法。
1.map集合
我们的分页是需要多个参数的,并不是只有一个参数。当需要接收多个参数的时候,我们使用Map集合来装载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public List<Student> pagination(int start ,int end) throws Exception {
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
Map<String, Object> map = new HashMap();
map.put( "start" , start);
map.put( "end" , end);
return sqlSession.selectList( "StudentID.pagination" , map);
} catch (Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception {
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.pagination(0, 3);
for (Student student : students) {
System.out.println(student.getId());
}
}
|
2.LIMIT关键字
(1)mapper代码:利用limit关键字实现分页
1 2 3 | <select id= "selectByPageInfo" resultMap= "BaseResult" >
select * from tb_user limit #{pageNo}, #{pageSize}
</select>
|
(2)业务层直接调用
1 2 3 | public List<User> findByPageInfo(PageInfo info) {
return userMapper.selectByPageInfo(info);
}
|
(3)控制层直接调用
我们都知道mybatis框架,对于数据方面的应用更为出色。就数据的找寻方面,我们有时会涉及到分页搜索的操作,相信这点也是很多人迫切需要学习的知识点。
以上就是mybatis在java中分页查询的方法,因为参数的不固定,所以我们要借助map进行处理。对于另一种关键字的分页方法,大家也可以做一个知识的了解。更多基础知识指路:头条
推荐操作环境:windows7系统、java10版,DELL G3电脑。