zoukankan      html  css  js  c++  java
  • mybatis学习之分页

    分页一般分为物理分页:先查询所有值再分页输出,逻辑分页:直接分页查询输出,mybatis支持物理分页,如下:

    1、物理分页:

    mapper映射:

    <select id="findStudents" resultMap="StudentResult">
        select * from t_student order by id asc
    </select>

    Dao接口:

    /**
         * mybatis分页查询-逻辑分页
         * @param rowBounds
         * @return
         */
        public List<Student> findStudents(RowBounds rowBounds);

    测试:

    /**
         * 逻辑分页----查询所有数据再分页输出
         * @throws Exception
         */
        @Test
        public void testRowBounds() throws Exception{
            logger.info("学生查询-逻辑分页");
            int offset = 3;        //start
            int limit = 3;        //size
            RowBounds rowBounds = new RowBounds(offset,limit);
            List<Student> studentList = studentDao.findStudents(rowBounds);
            for (Student student : studentList) {
                System.out.println(student);
            }
        }

    2、逻辑分页:

    mapper映射:

    <select id="findStudents2" parameterType="Map" resultMap="StudentResult">
        select * from t_student
        <if test="start != null and size != null">
            limit #{start},#{size}
        </if>
    </select>

    Dao接口:

    /**
         * mybatis分页查询-物理分页
         * @param rowBounds
         * @return
         */
        public List<Student> findStudents2(Map<String, Object> map);

    测试:

    /**
         * 物理分页----查询分页数据再输出
         * @throws Exception
         */
        @Test
        public void testFind() throws Exception{
            logger.info("学生查询-物理分页");
            Map<String, Object> map = new HashMap<>();
            map.put("start", 0);
            map.put("size", 3);
            List<Student> studentList = studentDao.findStudents2(map);
            for (Student student : studentList) {
                System.out.println(student);
            }
        }
  • 相关阅读:
    python基础
    用纯css改变下拉列表select框的默认样式兼容ie9需要用js
    如何阻止360浏览器backspace键的默认快捷键
    js判断undefined类型,undefined,null, 的区别详细解析
    jquery事件日历插件e-calendar 的修改
    each和for一起使用
    2019.9.24 网络ip小知识
    数据库SQL开始学习_5
    2019.9.22 java小知识
    数据库SQL开始学习_4
  • 原文地址:https://www.cnblogs.com/vipzhou/p/5640859.html
Copyright © 2011-2022 走看看