在实现了分页查询后,我原本的项目之前的模糊查询失效,之前是传一个封装好的实体对象给后端,后端通过这个对象查询后返回结果给到前端。由于实现了分页,所以查询得到的结果也需要分页。

有个很关键的问题是分页需要传pageNumpageSize这两个参数,后端@RequestBody一次只能接收一个对象(@RequestBody是读取的流的方式, 在取 body参数时第一个参数取到后把request.getInputStream()关闭,一个请求中只包含一个request body)。而我要进行的查询又需要传一个对象,在走了一些弯路后想到:为什么不把pageNum和pageSize以及需要的实体整合成一个新的对象,传给后端后再分开获取值呢?这样虽然比较麻烦,但是没有其他思路我就这么试了,确实可以实现想要的多条件分页模糊查询。

后端实现

**注意:**要先导入PageHelper和Lombok插件,具体操作可以查看我的上一篇博客。

  1. 新建一个实体类

    @Data
    @NoArgsConstructor
    public class FundPage {private String fundCode;private String fundFullName;private String custodianCode;private String fundCategory;private String needReview;private Integer pageNum;private Integer pageSize;
    }
    
  2. dao层和mapper文件不需要更改,修改service层和实现类的业务函数

    //原本的service层函数
    List<FundInfo> getLots(FundInfo fundInfo)
    //改写后
    PageInfo<FundInfo> getLots(FundInfo fundInfo,Integer pageNum,Integer pageSize);
    //service的实现类
    @Overridepublic PageInfo<FundInfo> getLots(FundInfo fundInfo,Integer pageNum,Integer pageSize) {FundInfoExample example = new FundInfoExample();FundInfoExample.Criteria criteria = example.createCriteria();if (fundInfo.getFundCode() != null) {criteria.andFundCodeLike("%" + fundInfo.getFundCode() + "%");}if (fundInfo.getCustodianCode() != null) {criteria.andCustodianCodeLike("%" + fundInfo.getCustodianCode() + "%");}if (fundInfo.getFundFullName() != null) {criteria.andFundFullNameLike("%" + fundInfo.getFundFullName() + "%");}if (fundInfo.getNeedReview() != null) {criteria.andNeedReviewLike("%" + fundInfo.getNeedReview() + "%");}if (fundInfo.getFundCategory() != null) {criteria.andFundCategoryLike("%" + fundInfo.getFundCategory() + "%");}PageHelper.startPage(pageNum,pageSize);List<FundInfo> fundInfoList=fundInfoMapper.selectByExample(example);PageInfo<FundInfo> pageInfo=new PageInfo<>(fundInfoList);return pageInfo;}
    

    这里使用了PageHelper中的PageInfo类,这里主要是用到了其中的几个方法。

    注意:PageHelper.startPage(pageNum,pageSize);的作用开启分页,通过将查询后的结果集用PageInfo型的对象接收即可实现分页。此外,这里的模糊查询使用的是逆向工程生成的方法,具体可以查看我的其他博客。

  3. controller层

    @Autowiredprivate FundInfoService fundInfoService;
    ...
    @PostMapping("/test")public Result<List<FundInfo>> getTest(@RequestBody FundPage fundPage){FundInfo fundInfo=new FundInfo();fundInfo.setNeedReview(fundPage.getNeedReview());fundInfo.setFundCategory(fundPage.getFundCategory());fundInfo.setFundCode(fundPage.getFundCode());fundInfo.setFundFullName(fundPage.getFundFullName());fundInfo.setCustodianCode(fundPage.getCustodianCode());int pageNum=fundPage.getPageNum();int pageSize=fundPage.getPageSize();PageInfo<FundInfo> pageInfo=fundInfoService.getLots(fundInfo,pageNum,pageSize);List<FundInfo> fundInfoList=pageInfo.getList();int total= Math.toIntExact(pageInfo.getTotal());return Result.buildResult(Result.Status.OK,fundInfoList,total);}
    

    controller层通过对获取到的FundPage的属性进行提取,提取到的fundInfo,pageNum,pageSize作为调用模糊查询方法的参数。查询结果用PageInfo类型的对象接收,再定义一个FundInfo类型的List集合,调用PageInfogetList()方法,最后通过工具类返回给前端。

前端实现

由于我是使用angular+primeng的来做前端,所以这里贴上我的页面布局,上面的部分是一个组件,下面的部分是一个组件(angular是组件化开发,可以理解为一个页面/部分是一个组件)

由于模糊查询提交的表单是在一个组件,显示结果在另一个组件,所以当中涉及到组件间传值的问题

  1. 我解决组件间传值的方法是在fundInfoTable组件中导入fundInfo组件,然后再ngDoCheck()函数中监听表单是否提交

    ngDoCheck()是组件的生命周期的其中一部分,用于监测数据是否变更,但是每个变化检测周期都会大频率调用这个钩子,所以要轻量级实现,以下是官方文档的部分介绍

  2. 新建一个实体类,这实体类与后端是一致的。

    export class FundPage {fundCode: string;fundFullName: string;fundCategory: string;custodianCode: string;needReview: string;pageNum:number;pageSize:number;
    }
    
  3. 将提交后的表单数据赋值给fundPage,并把pageNum和pageSize给个初始化值(null也行)

    sub(data) {if (data.custodianCode == '') {data.custodianCode = null;}if (data.fundCategory == '') {data.fundCategory = null;}if (data.needReview == '') {data.needReview = null;}this.fundPage=data;this.fundPage.pageNum=1;this.fundPage.pageSize=10;}
    

    这里省略了表单还有其他细节,主要也是说一个思路

  4. fundInfoTable组件定义监听变量

    //前两个用于控制懒加载的函数在ngDoCheck()中只被调用一次,没有控制的话会一直调用
    bindOne: boolean = false;
    bindTwo: boolean = true;
    //用于区别对全部数据的分页查询和对模糊条件的分页查询
    isSearch: boolean = false;
    
  5. 编写ngDoCheck()变更检测钩子

    ngDoCheck() {//当监听到查询值改变时,将监听值bindTwo赋值为true,使页面能够再次重新调用懒加载函数if(this.fundPage!=this.fundInfoComponent.fundPage){this.bindTwo=true;}// 检查是否提交了模糊查询服务//通过两个变量来监听,使得this.loadFundInfos()只能调用一次if (this.fundInfoComponent.fundPage&&(this.bindOne||this.bindTwo)) {this.fundPage = this.fundInfoComponent.fundPage;this.isSearch = true;this.bindTwo=false;this.loadFundInfos();//查询后回到第一页this.first=0}}
    
  6. 懒加载函数

    //懒加载分页的数据loadFundInfos() {this.loading = true;setTimeout(() => {if (this.isSearch) {this.fundPage.pageNum = (this.first + 10) / 10;this.fundPage.pageSize = this.rows;this.fundService.getLots(this.fundPage).then(response => { this.totalRecords = response.total; this.infos = response.data; })} else {this.fundService.getPage(this.first + 10, this.rows).then(response => { this.totalRecords = response.total; this.infos = response.data; })}this.loading = false;}, 500)}
    

总结

后端的模糊查询分页其实难度不大,主要是前端卡了很久,主要问题在于如何把查询到的分页数据实时渲染到table上,试了几种钩子都没有效果,最后是突然想到之前做过的一道算法题,给我提供了用两个监听变量来控制函数的调用。我的实现方法可能不规范,但总归是实现了,希望能给大家一些思路,我是个新手,如果有错误的地方,欢迎大家指正,我们可以一起讨论!

多条件模糊分页查询(angular+primeng+springboot)相关推荐

  1. 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法

    [Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...

  2. Springboot模糊分页查询思路

    最近做项目用到了模糊分页查询,做一些必要的笔记,一来是对自己学习的知识的巩固,二来对有同样问题的人有参考作用 我们几乎做的每一个项目都会用到分页查询,多条件动态查询或者这两者结合即多条件动态分页查询 ...

  3. SpringBoot+Mybatis+Elasticsearch 实现模糊分页查询并标记关键字

    SpringBoot 整合 Elasticsearch 实现模糊分页查询并标记关键字 一.概述 & 介绍 Elasticsearch 是基于 Lucense 技术的搜索引擎(服务器),将数据进 ...

  4. Spring Data JPA 复杂/多条件组合分页查询

    推荐视频: http://www.icoolxue.com/album/show/358 public Map<String, Object> getWeeklyBySearch(fina ...

  5. spring data jpa实现有条件的分页查询功能

    spring data jpa实现有条件的分页查询功能 前端部分代码.发送请求: $('#grid').datagrid({iconCls: 'icon-forward',fit: true,bord ...

  6. 使用servlet来实现对数据库的模糊分页查询

    首先要注意一个问题,通过servlet来获取到你jsp页面输入要要查询的关键字,将这些关键字赋值给一对象,通过对象从数据库查出数据之后,当你查询出来的对象用好多页显示,当你点击下一页的时候就会发现会返 ...

  7. 后端分页+前端分页显示(Angular+Primeng+SpringBoot)

    后端实现 引入PageHelper插件 <dependency><groupId>com.github.pagehelper</groupId><artifa ...

  8. 新闻发布系统-用户登录.模糊分页查询

    通过上几次的博客,我们大致完成了管理员的功能,现在是用户的功能,用户点击后到用户信息页面 给本页面绑定数据库,查询到数据库中新闻表的数据 用户信息页面数据代码 <div class=" ...

  9. springDataJpa @Query注解多条件动态模糊分页查询,传入形参对象

    1.代码: @Query(value = "SELECT * FROM s_order_baseinfo WHERE 1=1 " +"AND IF(''!=:#{#ord ...

最新文章

  1. 请投上您的一票,助力 2019 开源基础设施峰会
  2. kustomize+argo
  3. VTK:图片之ImageIslandRemoval2D
  4. [你必须知道的异步编程]——异步编程模型(APM)
  5. VM异常关闭后导致虚拟机无法打开问题解决办法【已解决】
  6. .net mysql 类库_(精华)2020年6月27日 C#类库 MySqlHelper(Ado.net数据库封装)
  7. 【领域综述】NLP领域,你推荐哪些综述性的文章?
  8. mfc 服务器文件拷贝到本地,mfc服务器客户端间传输文件
  9. 利用EXP/IMP进行数据迁移,如何转换表空间操作(完整版)
  10. 如何使用MASM 5.0汇编语言编译器
  11. Eclipse安装Thymeleaf插件
  12. oracle 扩充语句,Oracle扩充表空间语句
  13. 2021年暑期训练阶段三Day3
  14. Java POI PPT 转 PNG 图片设置背景色失效
  15. eregi php 5.2,PHP5.3x不再支持ereg和eregi
  16. php进行Markdown解析
  17. 按“window+E”键出现【找不到应用程序】或【explore.exe找不到】的解决方法
  18. 基于微信小程序的食堂窗口自助点餐系统设计与实现-计算机毕业设计源码和lw文档
  19. 西铁城CL-S631无法打印,打印测试空白
  20. html5做密码形式的游戏,html5仿支付宝密码框的实现代码

热门文章

  1. 放弃VMware改投VirtualBox的五个理由
  2. 8.8.6. Polygons
  3. 邮件发送时间怎么修改 python_使用python通过电子邮件发送日期时间
  4. html word表格边框变成表格,word表格边框显示不全跨页面显示的解决方法
  5. android手势动画
  6. 5款考试学习的高效率APP,让你轻松学习一整天!
  7. python毕业设计项目源码选题(5)校园网站系统毕业设计毕设作品开题报告开题答辩PPT
  8. 微云虚拟化VOS操作系统
  9. 想了解直播系统开发用什么语言?
  10. css3中的属性选择器以及新增伪类