<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java手寫PageBean實(shí)現(xiàn)分頁

          共 3240字,需瀏覽 7分鐘

           ·

          2021-11-16 22:03

          手寫PageBean分頁對象


          提示:

          本次測試使用環(huán)境為:SpringBoot +Mybatis Plus Mysql,如使用純servlet可自行修改代邏輯。


          思路:

          1. 需要?jiǎng)?chuàng)建個(gè)PageBean泛型對象進(jìn)行存儲分頁數(shù)據(jù)。

          2. 查詢總頁數(shù)

          3. 查詢分頁數(shù)據(jù),并封裝分頁對象數(shù)據(jù)

          4. 返回分頁數(shù)據(jù)


          步驟:

          1. Controller接收才當(dāng)前頁參數(shù)和取多少條參數(shù)

          2. 調(diào)用Service處理數(shù)據(jù)

          3. Dao獲取并返回PageBean分頁數(shù)據(jù)對象


          1. PageBean泛型對象

          把需要用到的屬性進(jìn)行封裝,T泛型為可傳入任意對象。

          /**
          ?*?PageBean?分頁工具對象
          ?*
          ?*?@param??數(shù)據(jù)類型
          ?*/

          public?class?PageBean<T>?{

          ????private?Integer?totalPage;//總頁碼
          ????private?Integer?totalCount;//總記錄數(shù)
          ????private?Integer?currentPage;?//當(dāng)前頁碼
          ????private?Integer?pageSize;?//每頁顯示的記錄數(shù)
          ????private?List?list;?//每頁的數(shù)據(jù)List集合
          ????
          }

          返回個(gè)給頁面的參數(shù)對象就是該P(yáng)ageBean對象,包含分頁數(shù)據(jù)。


          2. Controlle層

          此處直接調(diào)用Dao層,不寫Service,有需要可自己寫。

          /**
          ?*?controller層
          ?*?返回json數(shù)據(jù)
          ?*
          ?*?@Author?黃柏茗
          ?*?@Date?2021-11-14
          ?*/

          @RestController
          @RequestMapping("/test")
          public?class?TestController?{

          ????//注入Dao層
          ????@Autowired
          ????private?UserDao?userDao;

          ????/**
          ?????*?獲取分頁數(shù)據(jù)
          ?????*
          ?????*?@param?currentPage?當(dāng)前頁
          ?????*?@param?pageSize????獲取多少條
          ?????*?@return
          ?????*/

          ????@GetMapping("/getPagingData")
          ????public?PageBean?getPagingData(Integer?currentPage,?Integer?pageSize)?{
          ????????System.err.println("當(dāng)前頁:"?+?currentPage?+?",獲取"?+?pageSize?+?"條數(shù)據(jù)");
          ????????return?userDao.getPaging(currentPage,?pageSize);
          ????}

          }


          3. Service層

          本文不寫service層,按需自寫。


          4. Dao數(shù)據(jù)訪問層

          獲取分頁數(shù)據(jù)關(guān)鍵部分在此處,注意看步驟就行。

          /**
          ?*?Dao層
          ?*
          ?*?@Author?黃柏茗
          ?*?@Date?2021-11-14
          ?*/

          @Repository
          public?class?UserDao?{

          ????//注入mapper
          ????@Autowired
          ????private?UserMapper?userMapper;

          ????/**
          ?????*?獲取總記錄條數(shù)
          ?????*
          ?????*?@return 總記錄條數(shù)
          ?????*/

          ????public?Integer?getTotalCount()?{
          ????????QueryWrapper?wrapper?=?new?QueryWrapper<>();
          ????????//獲取總記錄條數(shù)
          ????????Integer?count?=?userMapper.selectCount(wrapper);
          ????????return?count;
          ????}

          ????/**
          ?????*?獲取分頁數(shù)據(jù)
          ?????*
          ?????*?@param?currentPage?當(dāng)前頁
          ?????*?@param?pageSize????顯示條數(shù)
          ?????*?@return
          ?????*/

          ????public?PageBean?getPaging(Integer?currentPage,?Integer?pageSize)?{
          ????????//1.創(chuàng)建空PageBean對象
          ????????PageBean?pageBean?=?new?PageBean<>();

          ????????//2.查詢總記錄數(shù)
          ????????Integer?totalCount?=?getTotalCount();

          ????????//3.計(jì)算總頁數(shù)
          ????????//總記錄數(shù)量/每頁數(shù)量=總頁數(shù)
          ????????double?total?=?(double)totalCount?/?pageSize;
          ????????//向上取整??如23/10=2.3??取整為3頁
          ????????total?=?Math.ceil(total);

          ????????//4.設(shè)置從哪開始獲取數(shù)據(jù)
          ????????Integer?start?=?(currentPage?-?1)?*?pageSize;

          ????????//5.獲取分頁數(shù)據(jù)列表
          ????????List?list?=?userMapper.getPagingMapper(start,?pageSize);
          ????????//要返回的數(shù)據(jù)
          ????????pageBean.setTotalCount(totalCount);//總記錄數(shù)
          ????????pageBean.setCurrentPage(currentPage);//當(dāng)前頁
          ????????pageBean.setPageSize(pageSize);//取多少條數(shù)據(jù)
          ????????pageBean.setTotalPage((int)?total);//總頁數(shù)
          ????????pageBean.setList(list);//分頁數(shù)據(jù)

          ????????//6.返回PageBean分頁對象
          ????????return?pageBean;
          ????}
          }


          5. Mapper層

          操作數(shù)據(jù)庫,使用的是Mybats Plus,如不是Mybatis可忽略mapper,在Dao層直接獲取數(shù)據(jù),在此為了方便獲取數(shù)據(jù),使用MP進(jìn)行測試。


          /**
          ?*?Mapper層
          ?*?使用的?Mybatis?Plus
          ?*?@Author?黃柏茗
          ?*?@Date?2021-11-14
          ?*/

          @Mapper
          public?interface?UserMapper?extends?BaseMapper<User>?{

          ????/**
          ?????*?獲取分頁數(shù)據(jù)列表
          ?????*
          ?????*?@param?start?開始位置
          ?????*?@param?pageSize?獲取條數(shù)
          ?????*?@return
          ?????*/

          ????@Select("select?*?from?tb_User?limit?${start},${pageSize}")
          ????public?List?getPagingMapper(@Param("start")?Integer?start,?@Param("pageSize")?Integer?pageSize);
          }

          注意:此處傳入的?start? 和 pageSize 參數(shù)在表字段中是沒有的,在mybatis中需要使用?@Param("start")? 標(biāo)注,才可正常傳入數(shù)據(jù),如果不是mybatis,則可以不使用。


          測試:


          頁面數(shù)據(jù)展示:

          頁面數(shù)據(jù)渲染在此不做測試,可自行獲取到數(shù)據(jù)進(jìn)行渲染。



          總結(jié):

          核心部分在Dao,注意看嗷,分頁功能已實(shí)現(xiàn),開熏。


          瀏覽 144
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  东京热黄色录像大全 | 啊v手机在线小视频 | 88av在线播放 | 蜜桃导航-精品导航 | 影音先锋av资源在线 |