欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

用java實現(xiàn)分頁查詢的方法

不懂用java實現(xiàn)分頁查詢的方法?其實想解決這個問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。

創(chuàng)新互聯(lián)建站專注于連江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供連江營銷型網(wǎng)站建設(shè),連江網(wǎng)站制作、連江網(wǎng)頁設(shè)計、連江網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務(wù),打造連江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供連江網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

1.基本思路

我現(xiàn)階段的分頁查詢的實現(xiàn)是基于sql語句的。

select * from user where id limit a, b

構(gòu)造出相應(yīng)的a和b就可以查詢出想要的數(shù)據(jù),在顯示在頁面上。重點是要構(gòu)造出當(dāng)前的頁數(shù),就要封裝一個javaBean,存儲有關(guān)分頁的基本屬性。

這樣只需在service層計算想要的頁數(shù),并封裝基本的信息,在查詢出來顯示在前端就可以了。

2.具體實現(xiàn)

1.定義JavaBean

public @Data
class PageBean<T> implements Serializable {
  private Integer page;//當(dāng)前頁數(shù)
  private Integer limit;//每頁顯示數(shù)
  private Integer totalPage;//總頁數(shù)
  private Integer total;//總記錄數(shù)
  private List<T> pageRecode;//當(dāng)前頁面的數(shù)據(jù)集合
  private List<Integer> pages;//返回頁數(shù)的集合,用于顯示index頁面的上一頁、下一頁
}

2.controller:

PageBean<QuestionDTO> pageBean = questionService.questionList(page);

返回一個QuestionDTO類型的JavaBean,其中包含了分頁的一些信息和當(dāng)前頁面所要顯示的數(shù)據(jù)集合。有關(guān)QuestionDTO:

public @Data
class QuestionDTO {
  private Integer id;
  private String title;
  private String description;
  private Long gmtCreate;
  private Long GmtModified;
  private Integer creator;
  private Integer attentionCount;
  private Integer viewCount;
  private Integer likeCount;
  private String tag;
  private User user;
  
}

3.調(diào)用的Service:

   //查詢所有的問題回顯到index頁面
  public PageBean<QuestionDTO> questionList(Integer page) {
    List<QuestionDTO> list = new ArrayList<>();
    PageBean<QuestionDTO> pagesinfo = new PageBean<>();
    //1.設(shè)置limit
    Integer limit = 5;
    pagesinfo.setLimit(limit);
    //2.設(shè)置總記錄數(shù)
    Integer total = questionMapper.fingCount();
    pagesinfo.setTotal(total);
    //3.設(shè)置總的頁數(shù)
    Integer totalPage;
    if(total % limit == 0){
      totalPage = total / limit;
    }else{
      totalPage = total / limit + 1;
    }
    pagesinfo.setTotalPage(totalPage);
    //4.設(shè)置頁數(shù)的集合
    List<Integer> pages = new ArrayList<>();
    for(int i=1;i<totalPage+1;i++){
      pages.add(i);
    }
    pagesinfo.setPages(pages);
    //5.設(shè)置每頁的數(shù)據(jù)集合
    List<Question> questions = questionMapper.questionList(page,limit);
    for(Question question : questions){
      User user = userMapper.findById(question.getCreatar());
      QuestionDTO questionDTO = new QuestionDTO();
      BeanUtils.copyProperties(question,questionDTO);
      questionDTO.setUser(user);
      list.add(questionDTO);
    }
    pagesinfo.setPageRecode(list);
    return pagesinfo;
  }

在service層為PageBean的屬性賦值,并且查詢出相關(guān)的數(shù)據(jù)。上述代碼中第5步如果有疑惑請參照多表聯(lián)合查詢的簡單另類的實現(xiàn)方式。

4.mapper

  //查詢所有的問題并回顯到index頁面
  @Select("select * from question where id limit #{page},#{limit}")
  List<Question> questionList(Integer page, Integer limit);

  //查詢總的問題數(shù)
  @Select("select count(id) from question")
  Integer fingCount();

做完這些,controller中的PageBean中就會封裝有查詢的數(shù)據(jù)。在返回前端顯示就完成了。

5.前端代碼

 <!-- 分頁 -->
      <nav aria-label="Page navigation" th:align="right">
        <ul class="pagination">
          <li th:if="${pageBean.totalPage>5 || pageBean.totalPage==1}">
            <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous">
              <span aria-hidden="true">&laquo;</span>
            </a>
          </li>
          <li th:each="page:${pageBean.pages}"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:text="${page}"></a></li>
          <li>
            <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next" th:if="${pageBean.totalPage>5}">
              <span aria-hidden="true">&raquo;</span>
            </a>
          </li>
        </ul>
      </nav>

循環(huán)取出page。

另一種方式,使用mybatis-generator生成的分頁查詢方法

1.新建generatorConfig.xml配置文件‘

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

  <context id="DB2Tables" targetRuntime="MyBatis3">

    <!-- 生成帶有分頁方法的插件-->
    <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>

    <!-- 連接數(shù)據(jù)庫的基本信息 -->
    <jdbcConnection driverClass="com.MySQL.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/community&#63;characterEncoding=utf-8&amp;serverTimezone=UTC"
            userId="root"
            password="root">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成的實體類
      targetPackage:實體類存放的包名
      targetProject:項目地址(到j(luò)ava)
    -->
    <javaModelGenerator targetPackage="cn.fzkj.community.domain" targetProject="src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!--生成的xml映射文件 -->
    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 生成的mapper接口 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="cn.fzkj.community.mapper" targetProject="src/main/java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 表名,想生成的實體類的名稱 -->
    <table tableName="question" domainObjectName="Question" ></table>
    <table tableName="user" domainObjectName="User" ></table>
    <table tableName="comment" domainObjectName="Comment" ></table>
    <table tableName="notification" domainObjectName="Notification" ></table>

  </context>
</generatorConfiguration>

注:

1.配置文件的名稱固定是generatorConfig.xml

2.在控制臺運行命令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate生成帶有分頁查詢方法的代碼

注:overwrite=true會覆蓋掉前一次生成的代碼,可根據(jù)實際需求進(jìn)行調(diào)整。

3.舉例:

    QuestionExample example = new QuestionExample();
    example.createCriteria().
        andCreatorEqualTo(id);
    List<Question> questions = questionMapper.selectByExampleWithRowbounds(example, new RowBounds(page,limit));

設(shè)置好頁數(shù)和每一頁顯示的個數(shù),就可以返回對應(yīng)的結(jié)果集。也同樣可以做到分頁。

持續(xù)更新~~~

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享用java實現(xiàn)分頁查詢的方法內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!

當(dāng)前題目:用java實現(xiàn)分頁查詢的方法
瀏覽路徑:http://aaarwkj.com/article48/jjgjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站建設(shè)、ChatGPT營銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
欧美精品一区二区三区在线| 国产精品精品久久久久久| 日本不卡一区二区在线播放| 最新在线中文字幕av不卡| 国精品午夜福利视频不卡| 日本一区不卡二区高清| 中文字幕一区免费视频| 成人黄色动漫在线播放| 国产在线精品不卡一区| 九九有点热以前的视频| 精品国产亚洲av未满十八| 粉嫩护士国产在线观看| av天堂在线观看网站| 亚洲精品国产自在现线| 超碰香蕉在线在线观看| 日本91大神在线观看| 亚洲综合日韩精品国产av| 国产高清av免费在线观看| 国产在线拍揄自揄视频不卡99| 日本不卡不二三区在线看| 香蕉视频欧美日韩国产| 国产精品1区2区久久久| 国产一区二区高清在线| 国产一级二级三级在线电影| 色在色在线播放亚洲中文| 精品少妇人妻av蜜桃| 天堂av影片在线观看| 亚洲精品国产自在现线| 亚洲国产精品综合色在线| 性生活视性生活大片日本| 日韩三级一区二区三区| 97视频在线视频免费| 国产av不卡二区三区| 91麻豆精品在线观看| 日韩精品电影一二三| 国产精品久久高清免费| 亚洲精品国产中文字幕| 国产国产乱老熟视频网站| 亚洲综合精品久久久一区| 日本韩国视频一区二区| 97国产一区二区精品久久呦|