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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

如何用java實(shí)現(xiàn)分頁(yè)查詢

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

1.基本思路

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

select * from user where id limit a, b

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

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

2.具體實(shí)現(xiàn)

1.定義JavaBean

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

2.controller:

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

返回一個(gè)QuestionDTO類型的JavaBean,其中包含了分頁(yè)的一些信息和當(dāng)前頁(yè)面所要顯示的數(shù)據(jù)集合。有關(guān)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.調(diào)用的Service:

//查詢所有的問(wèn)題回顯到index頁(yè)面 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è)置總的頁(yè)數(shù) Integer totalPage; if(total % limit == 0){ totalPage = total / limit; }else{ totalPage = total / limit + 1; } pagesinfo.setTotalPage(totalPage); //4.設(shè)置頁(yè)數(shù)的集合 List<Integer> pages = new ArrayList<>(); for(int i=1;i<totalPage+1;i++){ pages.add(i); } pagesinfo.setPages(pages); //5.設(shè)置每頁(yè)的數(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步如果有疑惑請(qǐng)參照多表聯(lián)合查詢的簡(jiǎn)單另類的實(shí)現(xiàn)方式。

4.mapper

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

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

5.前端代碼

<!-- 分頁(yè) --> <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>

循環(huán)取出page。

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

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'> <!-- 生成帶有分頁(yè)方法的插件--> <plugin type='org.mybatis.generator.plugins.RowBoundsPlugin'></plugin> <!-- 連接數(shù)據(jù)庫(kù)的基本信息 --> <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> <!-- 生成的實(shí)體類 targetPackage:實(shí)體類存放的包名 targetProject:項(xiàng)目地址(到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> <!-- 表名,想生成的實(shí)體類的名稱 --> <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.在控制臺(tái)運(yùn)行命令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate生成帶有分頁(yè)查詢方法的代碼

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

3.舉例:

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

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

持續(xù)更新~~~

以上就是如何用java實(shí)現(xiàn)分頁(yè)查詢的詳細(xì)內(nèi)容,更多關(guān)于java實(shí)現(xiàn)分頁(yè)查詢的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲一区 在线播放 | 国产啪精品 | 国产精品99精品久久免费 | 日韩精品视频在线播放 | 欧美成综合网网站 | 图片综合区 | 国产在线视频www色 国产在线视频一区 | 在线日本三级 | 美日韩精品 | 免费一级特黄a | 1000部18未成人禁止国产 | 91在线品视觉盛宴免费 | 国产亚洲精品成人a在线 | 2020久久精品国产免费 | 99爱视频精品免视看 | 亚洲精品综合 | 亚洲一级毛片免费在线观看 | 一级黄色大片免费观看 | 欧美成人免费午夜全 | 久久亚洲国产成人影院 | 黄色毛片电影 | 中文国产成人精品久久久 | 在线观看中文字幕国产 | 国产高清精品在线 | a毛片视频 | 一区二区三区福利视频 | 99久久国语露脸精品对白 | 91视频苹果版 | 免费一级国产大片 | 精品一区二区三区免费站 | 亚洲人成一区二区三区 | 亚洲欧美韩日 | 2020国产免费久久精品99 | 爱爱动态视频免费 | 国产免费高清无需播放器 | 亚洲热热久久九九精品 | 国产91在线精品 | 国产一级片观看 | 在线视频中文字幕乱人伦 | 亚洲一区在线视频 | 牛牛在线 |