zoukankan      html  css  js  c++  java
  • 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_02-自定义查询页面-服务端-接口开发

    在Service中实现自定义查询


    StringUtils.isNotEmpty()是这个包下的org.apache.commons.lang3.StringUtils;


    再设置其他的条件

    定义Example对象

    把example作为第一个参数

    controller

    代码不用改

    测试



    这里加一个断点测试

    可以看到传入的条件

    最终查询到的数据

    最终代码

    package com.xuecheng.manage_cms.service;
    
    import com.xuecheng.framework.domain.cms.CmsPage;
    import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
    import com.xuecheng.framework.model.response.CommonCode;
    import com.xuecheng.framework.model.response.QueryResponseResult;
    import com.xuecheng.framework.model.response.QueryResult;
    import com.xuecheng.manage_cms.dao.CmsPageRepository;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.*;
    import org.springframework.stereotype.Service;
    
    @Service
    public class PageService {
        @Autowired
        CmsPageRepository cmsPageRepository;
        public QueryResponseResult findList(int page,int size, QueryPageRequest queryPageRequest) {
    
            if(queryPageRequest==null){
                queryPageRequest=new QueryPageRequest();
            }
            //自定义查询条件
            ExampleMatcher exampleMatcher=ExampleMatcher.matching()
                    .withMatcher("pageAliase",ExampleMatcher.GenericPropertyMatchers.contains());
            //条件之对象
            CmsPage cmsPage=new CmsPage();
            //设置条件值 (站点ID)
            if(StringUtils.isNotEmpty(queryPageRequest.getSiteId())){
                cmsPage.setSiteId(queryPageRequest.getSiteId());
            }
            //设置模板id 作为查询条件
            if(StringUtils.isNotEmpty(queryPageRequest.getTemplateId())){
                cmsPage.setTemplateId(queryPageRequest.getTemplateId());
            }
            //设置页面别名为查询条件
            if(StringUtils.isNotEmpty(queryPageRequest.getPageAliase())){
                cmsPage.setPageAliase(queryPageRequest.getPageAliase());
            }
            //定义Exmaple对象
            Example<CmsPage> example=Example.of(cmsPage,exampleMatcher);
    
    
            if(page<0){
                page=1;
            }
            page = page -1;
            if(size<=0){
                size = 10;
            }
            Pageable pageable = PageRequest.of(page, size);
            Page<CmsPage> all = cmsPageRepository.findAll(example,pageable);
            QueryResult queryResult=new QueryResult();
            queryResult.setList(all.getContent());//设置返回的列表数据
            queryResult.setTotal(all.getTotalElements());//设置总记录数
            QueryResponseResult queryResponseResult=new QueryResponseResult(CommonCode.SUCCESS,queryResult);
            return queryResponseResult;
        }
    }
    PageService
  • 相关阅读:
    Unix系统编程()进程和程序
    java保存动态代理生成的类的class文件
    Hibernate 查询sql结果行数、查询列表的几种方法
    JVM学习--内存分配策略(持续更新)
    JVM学习--开启应用的gc日志功能
    JVM垃圾收集器组合--各种组合对应的虚拟机参数实践
    持续集成环境--Tomcat热部署导致线程泄漏
    监控redis服务器执行的命令--类似于tomcat的local-access.log
    centos7搭建nexus maven私服(二)
    centos7搭建nexus maven私服
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11565539.html
Copyright © 2011-2022 走看看