定义一个 PageBean 工具类。的工具类需要至少6个私有属性,数据的totalRecord总数; totalPage 数据页总数;pageSize:每页几个数据;pageNow 当前页码;列出当前页码的列表数据集合;startIndex sql 语句 查询的起点。
package com . pagehelper . test ;
import java . util . List ;
public class PageBean < T > {
/*total number of records*/
private int totalRecord ;
/*total number of pages*/
private int totalPage ;
/*data per page Number of
items */private int pageSize ;
/*Current page number*/
private int pageNow ;
/*Data collection obtained<T>Generic in order to make the tool class universal*/
private List < T >list ;
/*Database query starts with that record*/
private int startIndex ;
public PageBean ( int pageNow , int pageSize , int totalRecord ) {
this . totalRecord = totalRecord ;
/*Trinocular operation, totalRecord can be divisible by pageSize totalPage=totalRecord/pageSize
* = Not divisible totalRecord TotalPage/* the pageSize +. 1/
TotalPage = totalRecord % the pageSize == 0 ? TotalRecord / the pageSize : totalRecord / the pageSize + . 1 ;
the this . The pageSize = the pageSize ;
the this . PageNow = pageNow ;
startIndex = ( pageNow - . 1 ) * pageSize ;
}
/*Constructor for fixed PageSize*/
public PageBean( int pageNow , int totalRecord ) {
this . totalRecord = totalRecord ;
pageSize = 5 ;
totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1 ;
this . pageNow = pageNow ;
startIndex = ( pageNow - 1) * pageSize ;
}
/*Set,Get method*/
public int getTotalRecord ( ) {
return totalRecord ;
}
public void setTotalRecord ( int totalRecord ) {
this . totalRecord = totalRecord ;
}
public int getTotalPage ( ) {
return totalPage ;
}
public void setTotalPage ( int totalPage ) {
this . totalPage = totalPage ;
}
public int getPageSize ( ) {
return pageSize ;
}
public void setPageSize ( int pageSize ) {
this . pageSize = pageSize ;
}
public int getPageNow ( ) {
return pageNow ;
}
public void setPageNow ( int pageNow ) {
this . pageNow = pageNow ;
}
public List < T > getList ( ) {
return list ;
}
public void setList ( List < T > list ) {
this . list = list ;
}
public int getStartIndex ( ) {
return startIndex ;
}
public void setStartIndex ( int startIndex ) {
this . startIndex = startIndex ;
}
}
Mapper 接口至少需要两种方法,一种方法查询总记录数,另一种方法查询数据。Worker 是我的数据库的 pojo 类。
int getTotalRecord ( ) ;
List < Worker > selectAll ( @Param ( value = "startIndex" ) int startIndex , @Param ( value = "pageSize" ) int pageSize ) ;
以下代码是Worker对应的resultMap和对应接口方法的实现
< mapper namespace = " com.pagehelper.mapper.WorkerMapper " >
< resultMap id = " BaseResultMap " type = " com.pagehelper.pojo.Worker " >
< id column = " wid " property = " wid " jdbcType = " INTEGER " />
< result column = " password " property = " password" jdbcType = " VARCHAR " />
< result column = " company " property = " company " jdbcType = " VARCHAR " />
< result column = " department " property = " department " jdbcType = " VARCHAR " />
< result column = " job " property =" job " jdbcType = " VARCHAR " />
< result column = " name " property = " name " jdbcType = " VARCHAR " />
< result column = " sex " property = " sex " jdbcType = " VARCHAR " />
< result column = "age " property = " age " jdbcType = " INTEGER " />
< result column = " tel " property = " tel " jdbcType = " VARCHAR " />
< result column = " email " property = " email " jdbcType = " VARCHAR " />
< result column = "regist_time " Property = " registTime " the jdbcType = " TIMESTAMP " />
< Result column = " IMG " Property = " IMG " the jdbcType = " VARCHAR " />
</ The resultMap >
< SELECT ID = " getTotalRecord " the resultType = " int " >
SELECT COUNT(wid) FROM worker
</ select >
< select id = " selectAll " resultMap = " BaseResultMap " >
SELECT wid, password, company, department,job, name, sex, age, tel, email, regist_time,img FROM worker limit #{startIndex},#{pageSize}
</ select >
</ mapper >
服务层调用代码:
@Override
public PageBean < the Worker > the getAll ( int pageNow ) {
/* Get the total number of calls to method of recording getTotalRecord () */
int totalRecord = workerMapper . GetTotalRecord ( ) ;
/* instantiate a class PageBean tool parameters passed pageNow, totalRecord, the PageSize fixed constructor I used here */
PageBean < Worker > pageBean = new PageBean < > ( pageNow , totalRecord ) ;
/* Call data query method selectAll (StartIndex, PageSize), parameters PageBean properties, the results of the query package to PageBean of the List */
pageBean . SetList ( workerMapper . A selectAll ( pageBean . GetStartIndex ( ) , pageBean . GetPageSize ( ) ) ) ;
/*Return PageBean object*/
return pageBean ;
}
因为构建的java项目没有写Controller和page,所以使用test方法显示查询结果
package com . pagehelper . test ;
import com . pagehelper . mapper . WorkerMapper ;
import com . pagehelper . pojo . Worker ;
import com . pagehelper . service . impl . WorkerServiceImpl ;
import com . pagehelper . util . PageBean ;
import org . apache . ibatis . io . Resources ;
importorg . apache . ibatis . session . SqlSession ;
import org . apache . ibatis . session . SqlSessionFactory ;
import org . apache . ibatis . session . SqlSessionFactoryBuilder ;
import java . io . IOException ;
import java . io . InputStream ;
public class Test1 {
public static void main ( String [ ] args ) throws IOException {
/*Get and load configuration file*/
String resource = "mybatis-config.xml" ;
InputStream inputStream = Resources . getResourceAsStream ( resource ) ;
/*Create a SqlSessionFactory session factory*/
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder ( ) . Build ( inputStream ) ;
/*Production SQLSession*/
The SqlSession the session = the sessionFactory . The openSession ( ) ;
/* realized obtained WorkerMapper interface */
WorkerMapper Mapper = the session . GetMapper ( WorkerMapper . Class ) ;
/* the Controller layer calls Service layer methods, examples of WorkerService */
WorkerServiceImpl workerService = new new WorkerServiceImpl ( mapper ) ;
/*Call WorkerService method, return PageBean, encapsulate paging related data
* Controller or after get data request is transmitted to the front end module to PageBean page, the page can obtain data directly via EL database, the current page number, total number of pages, and other related parameters */
PageBean < the Worker > pageBean = workerService . The getAll ( . 1 ) ;
/* print the total number of records TotalRecord */
the System . OUT . the println ( "total number of records:" + pageBean . getTotalRecord ( ) ) ( ) ) ; /* print from the database acquired data */for ( the worker worker : pageBean . getList ( ) ;
/* * print pages TotalPage/
the System . OUT . the println ( "pages: " + pageBean . getTotalPage
) {
System . OUT . The println ( worker ) ;
}
}
}
操作结果
log4j:ERROR Could not find value for key log4j.appender.LogFIle
log4j:ERROR Could not instantiate appender named "LogFIle".
2019-07-24 09:26:41 [main:1]-[DEBUG] ==> Preparing: SELECT COUNT(wid) FROM worker
2019-07-24 09:26:41 [main:56]-[DEBUG] ==> Parameters:
2019-07-24 09:26:41 [main:123]-[DEBUG] <== Total: 1
2019-07-24 09:26:41 [main:131]-[DEBUG] ==> Preparing: SELECT wid, password, company, department,job, name, sex, age, tel, email, regist_time,img FROM worker limit ?,?
2019-07-24 09:26:41 [main:132]-[DEBUG] ==> Parameters: 0(Integer), 5(Integer)
2019-07-24 09:26:41 [main:148]-[DEBUG] <== Total: 5
Total records: 29
Total pages: 6
Worker{wid=100001, password='admin', company='Soft Emperor', department='Development Department', job='Super Admin', name='Super Admin', sex='Male', age= 18, tel='123', email='[email protected]', registTime=Sat Jul 20 11:33:18 CST 2019, img='default.jpg'}
Worker{wid=100002, password='123', company='Soft Emperor Group', department='Development Department', job='Employee', name='Administrator', sex='Male', age=122, tel='123', email='[email protected]', registTime=Sat Jul 20 16:00:59 CST 2019, img='default.jpg'}
Worker{wid=100003, password='admin', company='Soft Emperor', department='Development Department', job='Administrator', name='Administrator', sex='Male', age=18, tel='123', email='[email protected]', registTime=Sat Jul 20 11:33:20 CST 2019, img='default.jpg'}
Worker{wid=100004, password='123', company='Soft Emperor', department='Development Department', job='BOSS', name='123', sex='男', age=18, tel= '123', email='123', registTime=Sat Jul 20 15:10:33 CST 2019, img='default.jpg'}
Worker{wid=100005, password='123', company='Soft Emperor Group', department='Personnel Department', job='Manager', name='Sada', sex='Male', age=18, tel='123', email='123', registTime=Sat Jul 20 15:10:50 CST 2019, img='default.jpg'}
Process finished with exit code 0
Java开发工具还有很多,大家可要多多了解,在以后的学习中还会用到。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习