Java教程

mybatis配置分页

本文主要是介绍mybatis配置分页,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

7、分页

为什么分页

  • 减少数据的处理量

7.1、limit分页

SELECT *FROM USER LIMIT 2,2

接口UserMapper

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

配置文件Mapper

    <select id="getUserByLimit" resultType="User" parameterType="map">
        select * from user limit #{begin},#{end};
    </select>

方法测试

    @Test
    public void getUserByLimitTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("begin",1);
        map.put("end",2);

        List<User> userByLimit = mapper.getUserByLimit(map);

        for (User user : userByLimit) {
            System.out.println(user);
        }

        sqlSession.close();
    }

7.2、RowBounds分页

不使用SQL实现分页

1.接口

    public List<User> getUserByRowBounds();

2.Mapper.xml

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

3.测试

    @Test
    public void getUserByRowBounds(){

        //RowBounds实现
        RowBounds rowBounds = new RowBounds(1,2);

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        List<User> userList = sqlSession.selectList("com.lin.mapper.UserMapper.getUserByRowBounds", null, rowBounds);

        for (User user : userList) {
            System.out.println(user);
        }

        sqlSession.close();
    }

这篇关于mybatis配置分页的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!