Spring Data Mongodb的基本JPA操作

Spring Data Mongodb基础操作

cmsPageRepository是继承了MongoRepository<T, ID>的接口

1 添加

//添加
@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);
    cmsPageRepository.save(cmsPage);
    System.out.println(cmsPage);
}

2 删除

//删除
@Test
public void testDelete() {
    cmsPageRepository.deleteById("5b17a2c511fe5e0c409e5eb3");
}

关于Optional:
Optional是jdk1.8引入的类型,Optional是一个容器对象,它包括了我们需要的对象,使用isPresent方法判断所包
含对象是否为空,isPresent方法返回false则表示Optional包含对象为空,否则可以使用get()取出对象进行操作。
Optional的优点是:

  1. 提醒你非空判断。
  2. 将对象非空检测标准化。

3 修改

//修改
@Test
public void testUpdate() {
Optional<CmsPage> optional = cmsPageRepository.findOne("5b17a34211fe5e2ee8c116c9");
if(optional.isPresent()){
CmsPage cmsPage = optional.get();
cmsPage.setPageName("测试页面01");
cmsPageRepository.save(cmsPage);
}
}

4.自定义查询方法

基础自定义查询

同Spring Data JPA一样Spring Data mongodb也提供自定义方法的规则,如下:
按照findByXXX,findByXXXAndYYY、countByXXXAndYYY等规则定义方法,实现查询操作。

public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
    //根据页面名称查询
    CmsPage findByPageName(String pageName);
    //根据页面名称和类型查询
    CmsPage findByPageNameAndPageType(String pageName,String pageType);
    //根据站点和页面类型查询记录数
    int countBySiteIdAndPageType(String siteId,String pageType);
    //根据站点和页面类型分页查询
    Page<CmsPage> findBySiteIdAndPageType(String siteId,String pageType, Pageable pageable);
}
	

基础分页查询

//从0开始
int page = 0;
int size = 10;
//定义分页对象
Pageable pageable = PageRequest.of(page, size).first();
//查询所有进行分页,可以使用自定义方法代替,但是自定义形参中必须有Pageable
Page<CmsPage> all = cmsPageRepositoty.findAll(pageable);
System.out.println(all);

带有条件的分页查询

//从0开始
int page = 0;
int size = 10;
//定义分页参数
Pageable pageable = PageRequest.of(page, size).first();
//条件值对象,一般为集合的实体对象
CmsPage cmspage = new CmsPage();
//设置要查询的站点id(精确查询),直接设置值的情况下是精确匹配
//cmspage.setSiteId("5a751fab6abb5044e0d19ea1");
//设置模板id条件(精确查询)
//cmspage.setTemplateId("5a962b52b00ffc514038faf7");
//设置页码别名
cmspage.setPageAliase("轮播");
//定义条件匹配器
/*
ExampleMatcher exampleMatcher = ExampleMatcher.matching();
//针对pageAliase进行修改匹配为模糊匹配
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 = cmsPageRepositoty.findAll(example, pageable);
//转换为List并打印
List<CmsPage> content = all.getContent();
System.out.println(content);
  • Pageable 分页参数对象
  • CmsPage 查询条件实体(自定义)
  • ExampleMatcher 条件匹配器
    • withMatcher(“pageAliase”,ExampleMatcher.GenericPropertyMatchers.contains())
      指定{pageAliase}字段匹配方式

在Spring boot的yml文件中配置MongoDB

spring:
  data:
    mongodb:
      uri: mongodb://127.0.0.1:27017
      database: mongodb
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘿嘿嘿1212

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值