大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 從入門到精通的Java分頁工具

從入門到精通的Java分頁工具

更新時間:2021-11-09 11:00:50 來源:動力節點 瀏覽904次

從數據庫層面分頁(數據庫為MySQL,數據訪問層框架為MyBatis)

定義一個 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='123@163.com', 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='123@163.com', 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='123@163.com', 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開發工具還有很多,大家可要多多了解,在以后的學習中還會用到。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日日摸夜夜摸狠狠摸日日碰夜夜做 | 日本特级爽毛片叫声 | 狠狠骑| 亚洲欧美日韩在线不卡中文 | 亚洲欧洲尹人香蕉综合 | 成 人 黄 色 免费网 | 国内精品七七久久影院 | 国产综合视频 | 亚洲精品一区二区三区四区 | 奇米影视奇米色777欧美 | 久久99精品这里精品动漫6 | 欧美 亚洲 中文字幕 | 色综合中文字幕在线亚洲 | 中文字幕不卡一区 | 国产99青草全福视在线 | 亚洲另类视频在线观看 | 日韩欧美一区在线观看 | 一级黄色片免费 | 香蕉成人国产精品免费看网站 | 日夜夜操| 国产福利免费视频 | 国产色在线视频 | 国产成人在线视频免费观看 | 久草性视频 | 一级片免 | 在线观看精品视频一区二区三区 | 成人在色线视频在线观看免费大全 | 亚洲国产精品久久久久婷婷老年 | 乱人伦中文视频在线 | 男人的天堂在线视频 | 公主恋人ova | 色综合一区二区三区 | 亚洲经典在线中文字幕 | 婷婷色网 | 五月婷婷综合网 | 99久久做夜夜爱天天做精品 | 久操中文在线 | 老司机午夜影院 | 2019最新四虎免费8848 | a色毛片免费视频 | 精品国产免费观看 |