mysql 分页row_mybatis分页查询(limit分页、RowBounds分页)

本文介绍了使用MyBatis实现分页查询的两种方法:基于SQL的limit分页和基于代码的RowBounds分页,并提供了具体的接口定义、XML配置及测试代码示例。

1、limit分页

(1)mapper接口:

List getStudentByLim(HashMap map);

(2)配置文件:

select * from student limit #{startIndex},#{pageSize}

(3)测试:

@Testpublic voidgetStudentByLimTest(){

SqlSession sqlSession=MybatisUtils.getSqlSession();

StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);

HashMap map=new HashMap();

map.put("startIndex",0);

map.put("pageSize",3);

List 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 falseon JDBC Connection [com.mysql.jdbc.JDBC4Connection@670b40af]

DEBUG [main]- ==> Preparing: select * from student limit ?,?DEBUG [main]- ==> Parameters: 0(Integer), 3(Integer)

DEBUG [main]- <== Total: 3Student{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 getStudentByRowBounds();

(2)配置文件:

select*from student

(3)测试:

@Testpublic voidgetStudentByRowBoundsTest(){

SqlSession sqlSession=MybatisUtils.getSqlSession();

RowBounds rowBounds=new RowBounds(1,2);

List 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 falseon 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 trueon 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、分页插件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值