首页  >  文章  >  Java  >  Java中使用Mybatis实现分页查询的方法

Java中使用Mybatis实现分页查询的方法

王林
王林转载
2023-05-07 23:19:06976浏览

1.map集合

我们的分页是需要多个参数的,并不是只有一个参数。当需要接收多个参数的时候,我们使用Map集合来装载。

public List<Student>  pagination(int start ,int end) throws Exception {
        //得到连接对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try{
            //映射文件的命名空间.SQL片段的ID,就可以调用对应的映射文件中的SQL
 
 
            /**
             * 由于我们的参数超过了两个,而方法中只有一个Object参数收集
             * 因此我们使用Map集合来装载我们的参数
             */
            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关键字实现分页

    <select id="selectByPageInfo" resultMap="BaseResult">
        select * from tb_user limit #{pageNo}, #{pageSize}
    </select>

(2)业务层直接调用

    public List<User> findByPageInfo(PageInfo info) {
        return userMapper.selectByPageInfo(info);
    }

(3)控制层直接调用

我们都知道mybatis框架,对于数据方面的应用更为出色。就数据的找寻方面,我们有时会涉及到分页搜索的操作,相信这点也是很多人迫切需要学习的知识点。

以上是Java中使用Mybatis实现分页查询的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:yisu.com。如有侵权,请联系admin@php.cn删除