亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

如何用java實現分頁查詢

瀏覽:10日期:2022-08-31 10:33:41

1.基本思路

我現階段的分頁查詢的實現是基于sql語句的。

select * from user where id limit a, b

構造出相應的a和b就可以查詢出想要的數據,在顯示在頁面上。重點是要構造出當前的頁數,就要封裝一個javaBean,存儲有關分頁的基本屬性。

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

2.具體實現

1.定義JavaBean

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

2.controller:

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

返回一個QuestionDTO類型的JavaBean,其中包含了分頁的一些信息和當前頁面所要顯示的數據集合。有關QuestionDTO:

public @Dataclass 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.調用的Service:

//查詢所有的問題回顯到index頁面 public PageBean<QuestionDTO> questionList(Integer page) { List<QuestionDTO> list = new ArrayList<>(); PageBean<QuestionDTO> pagesinfo = new PageBean<>(); //1.設置limit Integer limit = 5; pagesinfo.setLimit(limit); //2.設置總記錄數 Integer total = questionMapper.fingCount(); pagesinfo.setTotal(total); //3.設置總的頁數 Integer totalPage; if(total % limit == 0){ totalPage = total / limit; }else{ totalPage = total / limit + 1; } pagesinfo.setTotalPage(totalPage); //4.設置頁數的集合 List<Integer> pages = new ArrayList<>(); for(int i=1;i<totalPage+1;i++){ pages.add(i); } pagesinfo.setPages(pages); //5.設置每頁的數據集合 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的屬性賦值,并且查詢出相關的數據。上述代碼中第5步如果有疑惑請參照多表聯合查詢的簡單另類的實現方式。

4.mapper

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

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

5.前端代碼

<!-- 分頁 --> <nav aria-label='Page navigation' th:align='right'><ul class='pagination'> <li th:if='${pageBean.totalPage>5 || pageBean.totalPage==1}'> <a href='http://www.aoyou183.cn/bcjs/6684.html#' 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='http://www.aoyou183.cn/bcjs/6684.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' th:text='${page}'></a></li> <li> <a href='http://www.aoyou183.cn/bcjs/6684.html#' 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>

循環取出page。

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

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

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE generatorConfiguration PUBLIC '-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN' 'http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd'><generatorConfiguration> <context targetRuntime='MyBatis3'> <!-- 生成帶有分頁方法的插件--> <plugin type='org.mybatis.generator.plugins.RowBoundsPlugin'></plugin> <!-- 連接數據庫的基本信息 --> <jdbcConnection driverClass='com.mysql.jdbc.Driver' connectionURL='jdbc:mysql://localhost:3306/community?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:項目地址(到java) --> <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會覆蓋掉前一次生成的代碼,可根據實際需求進行調整。

3.舉例:

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

設置好頁數和每一頁顯示的個數,就可以返回對應的結果集。也同樣可以做到分頁。

持續更新~~~

以上就是如何用java實現分頁查詢的詳細內容,更多關于java實現分頁查詢的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 91久久九九精品国产综合 | 91人人爱| 在线爽| 精品国产1000部91麻豆 | 日本护士做xxxxxx视频 | 日高千晶在线观看 | 一级毛片免费一级直接观看 | 免费看国产精品久久久久 | 欧美日韩在线视频不卡一区二区三区 | 欧美一做特黄毛片 | 真人一级毛片免费完整视 | 国产综合久久久久影院 | 成人亲子乱子伦视频 | 精品特级一级毛片免费观看 | 久久久久久久久久免免费精品 | 亚洲国产二区三区久久 | 国产亚洲片| 久久婷婷久久一区二区三区 | 黄色网址中文字幕 | 国产美女在线播放 | 久久久国产精品网站 | jizz成熟丰满中文字幕 | 99国产精品九九视频免费看 | 免费视频一区二区性色 | 国产小视频免费看 | 最新看片网址 | 精品一区二区在线观看 | 毛片a区| 99久久久国产精品免费牛牛四川 | 国产丰满主播丝袜勾搭秀 | 免费一级片网站 | 在线免费观看小视频 | 免费中国女人69xxxxx视频 | 成人国产在线观看 | 国产亚洲精品xxx | 亚洲日本中文字幕一本 | 国产一区二区三区丶四区 | 亚洲欧美综合日韩字幕v在线 | 国产乱码精品一区二区三上 | 在线黄色.com | 美女被拍拍拍拍拍拍拍拍 |