• mybatis的学习4______分页的实现


    分页的实现此处有两种思路:

    1. 使用SQL的 limit ?,?  

    2.使用RowBounds

    方式一 (步骤:):

    1.dao层中UserMapper接口的编写:

       //分页的sql实现
        List<User> getUserLimit(Map<String,Integer> map);

    2.dao层中UserMapper接口的userMapper.xml编写

     <select id="getUserLimit" parameterType="Map" resultType="User">
            select * from user limit #{indexStart},#{pageSize}
        </select>

    3.项目测试类的编写:

       @Test
        public void testSqlLimit(){
    
            SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
    
            int currentPage=1;
            int pageSize=2;
            Map<String, Integer> map = new HashMap<String,Integer>();
            map.put("indexStart",(currentPage-1)*pageSize);
            map.put("pageSize",pageSize);
    
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userLimit = mapper.getUserLimit(map);
            System.out.println(userLimit);
        }

    方式二(步骤):

    1.dao层中UserMapper接口的编写:

        //分页的代码实现
        List<User> getUserByRowBounds();

    2.dao层中UserMapper接口的userMapper.xml编写

      <select id="getUserByRowBounds" resultType="User">
            select * from user
        </select>

    3.测试类:

    @Test
        public void testRowBounds(){
            SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            int currentPage=2;
            int pageSize=2;
            RowBounds rowBounds = new RowBounds((currentPage - 1) * pageSize, pageSize);
            //传进2参数
            //不能用getMapper
            List<Object> user = sqlSession.selectList("com.xbf.dao.UserMapper.getUserByRowBounds", null, rowBounds);
            System.out.println(user);
    
        }
  • 相关阅读:
    java中VO、PO、DTO 、DO、POJO、BO、TO
    java可变参数
    排序
    快速排序
    单元测试概述
    Spring 对事务管理的支持
    Spring的事务管理基础知识
    混合切面类型
    基于Schema配置切面
    Spring AOP @AspectJ进阶
  • 原文地址:https://www.cnblogs.com/xbfchder/p/11237325.html
Copyright © 2020-2023  润新知