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);

    }

 

posted @ 2019-07-24 13:02  德鲁大叔817  阅读(245)  评论(0编辑  收藏  举报