Use Mybatis for paging operations

table of Contents

Two paging operations

limit query

Use PageHelper plugin


Two paging operations

 

One is through limit query, the other is through PageHelper plugin

 

 

limit query

 

    //分页
    List<User> getUserByLimit(Map<String,Integer> map);
    <select id="getUserByLimit" parameterType="map" resultType="com.lt.pojo.User">
        select * from user limit #{startIndex},#{pageSize}
    </select>
    //测试分页方法(手写SQL)
    @Test
    public void getUserByLimit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);

        HashMap<String,Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",2);

        List<User> users = dao.getUserByLimit(map);
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

 

 

Use PageHelper plugin

 

 Guide package

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.3</version>
        </dependency>

Add the plugins tag in the main configuration file, pay attention to be written behind the settings tag

The interface does not need to change, ordinary query

mapper.xml doesn't need to change

Test method, add a line to set a few pages, how many lines are displayed on each page

View the results, according to the custom paging requirements, find the first two lines of data

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105662109