03 springdata mongodb 基本操作(增删改查,条件分页,基于MongoRepository)

需求分析(条件分页查询)

根据站点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<CmsPage> example = Example.of(cmsPage, exampleMatcher);

        Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);

        QueryResult<CmsPage> queryResult = new 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<CmsPage> all = cmsPageRepository.findAll();

        System.out.println(all);

    }

    //测试分页查询

    @Test

    public void testfindPage() {

        int page = 1;

        int size = 3;

        Pageable pageable = PageRequest.of(page, size);

        Page<CmsPage> all = cmsPageRepository.findAll(pageable);

        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<CmsPage> example = Example.of(cmsPage, exampleMatcher);

        Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);

        List<CmsPage> content = all.getContent();

        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> cmsPageParams = new ArrayList<>();

        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<CmsPage> optional = cmsPageRepository.findById("5c3c7f3c49b2ef2880d25b8d");

        if (optional.isPresent()) {

            CmsPage cmsPage = optional.get();

            cmsPage.setPageName("测试页面wtg1111");

            cmsPageRepository.save(cmsPage);

        }

    }

}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值