需求分析(条件分页查询)
根据站点Id、精确匹配
模板Id、精确匹配
页面别名、模糊匹配
页面名称、模糊匹配
条件分页查询页面信息
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; /** * @description: * @author: wtg * @createDate: 2019-08-10 9:31 */ @Service public class PageService { @Autowired CmsPageRepository cmsPageRepository; public QueryResponseResult pageFindCondition(QueryPageRequest queryPageRequest) { Integer page = queryPageRequest.getPage();//当前页 Integer size = queryPageRequest.getSize();//每页显示的条数 if (page <= 0) { page = 1; } if (size <= 0) { size = 1; } //分页条件 Pageable pageable = PageRequest.of(page - 1, size); //条件值对象 CmsPage cmsPage = new CmsPage(); if (StringUtils.isNotEmpty(queryPageRequest.getSiteId())) { cmsPage.setSiteId(queryPageRequest.getSiteId());//精准匹配站点id } if (StringUtils.isNotEmpty(queryPageRequest.getTemplateId())) { cmsPage.setTemplateId(queryPageRequest.getTemplateId());//精准匹配模板id } //模糊匹配页面别名 cmsPage.setPageAliase(queryPageRequest.getPageAliase()); //模糊匹配页面名称 cmsPage.setPageName(queryPageRequest.getPageName()); /** * 条件匹配器 * ExampleMatcher exampleMatcher = ExampleMatcher.matching(); * exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains()); * ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字 * ExampleMatcher.GenericPropertyMatchers.startsWith()//前缀匹配 */ ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains()) .withMatcher("pageName",ExampleMatcher.GenericPropertyMatchers.contains()); //定义Example Example Page QueryResult queryResult.setList(all.getContent());//数据列表 queryResult.setTotal(all.getTotalElements());//数据总记录数 QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS, queryResult); return queryResponseResult; } } |
|
springdata mongodb增删改查
package com.xuecheng.manage_cms; import com.xuecheng.framework.domain.cms.CmsPage; import com.xuecheng.framework.domain.cms.CmsPageParam; import com.xuecheng.manage_cms.dao.CmsPageRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.*; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; /** * Created by wutegang on 2019/1/14 ${time} */ @SpringBootTest @RunWith(SpringRunner.class) public class CmsPageRepositoryTest { @Autowired private CmsPageRepository cmsPageRepository; //测试查询所有 @Test public void testfindAll() { List System.out.println(all); } //测试分页查询 @Test public void testfindPage() { int page = 1; int size = 3; Pageable pageable = PageRequest.of(page, size); Page System.out.println(all); } /** * 自定义条件分页查询测试 */ @Test public void testFindAllByExample() { //分页参数 int page = 0;//从0开始 int size = 10; Pageable pageable = PageRequest.of(page, size); //条件值对象 CmsPage cmsPage = new CmsPage(); //要查询5a751fab6abb5044e0d19ea1站点的页面 // cmsPage.setSiteId("5b30b052f58b4411fc6cb1cf"); //设置模板id条件 // cmsPage.setTemplateId("5ad9a24d68db5239b8fef199"); //设置页面别名 cmsPage.setPageAliase("页面"); /** * 条件匹配器 */ // ExampleMatcher exampleMatcher = ExampleMatcher.matching(); // exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains()); ExampleMatcher exampleMatcher = ExampleMatcher.matching() .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains()); //ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字 // ExampleMatcher.GenericPropertyMatchers.startsWith()//前缀匹配 /** * 定义Example */ Example Page List System.out.println(content); } //测试添加 @Test public void testInsert() { CmsPage cmsPage = new CmsPage(); cmsPage.setSiteId("s01"); cmsPage.setTemplateId("t01"); cmsPage.setPageName("测试页面"); cmsPage.setPageCreateTime(new Date()); List CmsPageParam cmsPageParam = new CmsPageParam(); cmsPageParam.setPageParamName("param1"); cmsPageParam.setPageParamValue("value1"); cmsPageParams.add(cmsPageParam); cmsPage.setPageParams(cmsPageParams); CmsPage insert = cmsPageRepository.insert(cmsPage); } //测试删除 @Test public void testDelete() { cmsPageRepository.deleteById("5c3c7e7949b2ef124411a13e"); } //测试修改 @Test public void testUpdate() { Optional if (optional.isPresent()) { CmsPage cmsPage = optional.get(); cmsPage.setPageName("测试页面wtg1111"); cmsPageRepository.save(cmsPage); } } }
|