ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mybatis分页查询(limit分页、RowBounds分页)

2020-04-26 14:07:02  阅读:389  来源: 互联网

标签:removed 分页 forcefully connections DEBUG limit closed mybatis main


1、limit分页

(1)mapper接口:

 List<Student> getStudentByLim(HashMap<String, Integer> map);

(2)配置文件:

    <select id="getStudentByLim" resultType="pers.zhb.pojo.Student" parameterType="map">
        select * from student limit #{startIndex},#{pageSize}
    </select>

(3)测试:

    @Test
    public void getStudentByLimTest(){
        SqlSession sqlSession= MybatisUtils.getSqlSession();
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        HashMap<String,Integer> map=new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",3);
        List<Student> students= studentMapper.getStudentByLim(map);
        for(Student student:students){
            System.out.println(student);
        }
    }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1728790703.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@670b40af]
DEBUG [main] - ==>  Preparing: select * from student limit ?,? 
DEBUG [main] - ==> Parameters: 0(Integer), 3(Integer)
DEBUG [main] - <==      Total: 3
Student{studentno='201811', sname='zhai', sex='男', birthday='1998-11-11', classno='80501', point='890', phone='1234567890', email='null', clas=null}
Student{studentno='201812', sname='zhai2', sex='男', birthday='1998-11-11', classno='80601', point='893', phone='19837372533', email='null', clas=null}
Student{studentno='201813', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}

 

2、RowBounds分页(了解,不建议使用)

(1)接口:

List<Student> getStudentByRowBounds();

(2)配置文件:

    <select id="getStudentByRowBounds" resultType="pers.zhb.pojo.Student">
        select * from student
    </select>

(3)测试:

    @Test
    public void getStudentByRowBoundsTest(){
        SqlSession sqlSession= MybatisUtils.getSqlSession();
        RowBounds rowBounds=new RowBounds(1,2);
        List<Student> students=sqlSession.selectList
                ("pers.zhb.mapper.StudentMapper.getStudentByRowBounds",null,rowBounds);
        for (Student student : students) {
            System.out.println(student);
        }
        sqlSession.close();
    }
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 243745864.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@e874448]
DEBUG [main] - ==>  Preparing: select * from student 
DEBUG [main] - ==> Parameters: 
Student{studentno='201812', sname='zhai2', sex='男', birthday='1998-11-11', classno='80601', point='893', phone='19837372533', email='null', clas=null}
Student{studentno='201813', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@e874448]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@e874448]
DEBUG [main] - Returned connection 243745864 to pool.

使用RowBounds实现的分页是基于代码的,二limit实现的分页是基于sql的

 

3、分页插件

官网含有该插件的文档,里面有详细的步骤

 

 摘自:https://pagehelper.github.io/docs/

 

标签:removed,分页,forcefully,connections,DEBUG,limit,closed,mybatis,main
来源: https://www.cnblogs.com/zhai1997/p/12779074.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有