mybatis的分页实现

五、分页

1.使用limit分页

1.方法:利用集合去接受结果,把分页的数值存在map中

//分页
List<User> getLimit(Map<String,Integer> map);

2.SQL语句,就像前面提到的那样,我们的结果如果是list。那么把泛型数据填在里面;

<typeAliases>
        <typeAlias type="com.saxon.pojo.User" alias="user"/>
    </typeAliases>
 <select id="getLimit" parameterType="map" resultType="user">
           select * from mybatis.saxon limit ${startIndex},${pageSize};
    </select>

3.测试

@Test
    public  void  limit(){
        SqlSession sqlSession = MybatisUnit.getSqlSession ();
        UserMapper mapper = sqlSession.getMapper (UserMapper.class);
        Map<String, Integer> Map = new HashMap<> ();
        Map.put ("startIndex",0);
        Map.put ("pageSize",2);
        List<User> limit =  mapper.getLimit (Map);
        for (User user : limit) {
            System.out.println (user);
        }
        sqlSession.close ();
    }

2.使用rowbounds

  //分页2
    List<User> getLimitby();

分页操作由rowbounds做

    <select id="getLimitby" resultType="user">
        select * from mybatis.saxon
    </select>
@Test
    public  void testRowbounds () {
        SqlSession sqlSession = MybatisUnit.getSqlSession ();
        RowBounds rowBounds = new RowBounds (1, 2);//其实下标和也页面大小
        List<User> list = sqlSession.selectList ("com.saxon.Dao.UserMapper.getLimitby", null,rowBounds );
        for (User o : list) {
            System.out.println (o);
        }

        sqlSession.close ();
    }

3.使用插件

例:pageHelper

自学总结
学习地址:狂神说Java

posted @ 2020-08-15 15:23  SaxonMo  阅读(98)  评论(0编辑  收藏  举报