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的优点是:
- 提醒你非空判断。
- 将对象非空检测标准化。
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}字段
匹配方式
- withMatcher(“pageAliase”,ExampleMatcher.GenericPropertyMatchers.contains())
在Spring boot的yml文件中配置MongoDB
spring:
data:
mongodb:
uri: mongodb://127.0.0.1:27017
database: mongodb