1.1Spring Data Mongodb基础
Spring Data Mongodb不需要像Mybatis那样专注于写SQL语句,直接用来调用它的内置查询方法的。
导入的包pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置文件application.yml
spring:
data:
mongodb:
uri: mongodb://127.0.0.1:27017
database: xc_cms
定义接口
public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
CmsPage findByPageNameAndSiteIdAndPageWebPath(String pageName,String siteId,String pageWebPath);
}
接口只需要继承MongoRepository(里面含有默认的增删改查方法),里面的泛型指的是Mongodb集合中的文档转换的对象,string指的是在接口中的方法的参数类型。
然后我用的是springboot集成测试编写测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class CmsPageRepositoryTest {
@Autowired
CmsPageRepository cmsPageRepository;
@Test
public void testFindAll(){
List<CmsPage> all = cmsPageRepository.findAll();
System.out.println(all);
}
@Test
public void testFindPage(){
int page=1;//第一页
int size=10;//页的大小
Pageable pageable= PageRequest.of(page, size);
Page<CmsPage> all = cmsPageRepository.findAll(pageable);//查询所有并且分页显示返回列表
System.out.println(all);
}
@Test
public void testFindAllByExample(){
int page=0;
int size=10;
Pageable pageable= PageRequest.of(page, size);
CmsPage cmsPage=new CmsPage();
cmsPage.setPageAliase("轮播");
// cmsPage.setSiteId("5a751fab6abb5044e0d19ea1");
ExampleMatcher exampleMatcher=ExampleMatcher.matching();
exampleMatcher=exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());
// ExampleMatcher.GenericPropertyMatchers.contains();
// ExampleMatcher.GenericPropertyMatchers.startsWith();
Example<CmsPage>example=Example.of(cmsPage,exampleMatcher);
Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);//查询在pageAliase字段的含有轮播的内容的全部文档,并且进行分页,列表返回。
List<CmsPage> content=all.getContent();
System.out.println(content);
}
}
2.1 Spring Data Mongodb增删改查
2.1.1增加数据一般先去查是否数据库里面含有数据
public CmsPageResult add(CmsPage cmsPage){
if (cmsPage==null){
}
CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());//按索引进行查找
if (cmsPage1!=null){
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
}
cmsPage.setPageId(null);//mongodb会自动给id赋值uuid
CmsPage save = cmsPageRepository.save(cmsPage);把信息保存到mongodb
return new CmsPageResult(CommonCode.SUCCESS, save);
// return new CmsPageResult(CommonCode.FAIL,null);
}
2.1.2改数据也需要查询数据
//根据id查询页面
public CmsPage getById(String id){
Optional<CmsPage> optional = cmsPageRepository.findById(id);
if (optional.isPresent()){//判断是否为空
CmsPage cmsPage=optional.get();
return cmsPage;
}
return null;
}
//更新页面信息
public CmsPageResult update(String id,CmsPage cmsPage){
CmsPage cmsPage1 = this.getById(id);
if (cmsPage1!=null){
cmsPage1.setTemplateId(cmsPage.getTemplateId());
cmsPage1.setSiteId(cmsPage.getSiteId());
cmsPage1.setPageAliase(cmsPage.getPageAliase());
cmsPage1.setPageName(cmsPage.getPageName());
cmsPage1.setPageWebPath(cmsPage.getPageWebPath());
cmsPage1.setPagePhysicalPath(cmsPage.getPagePhysicalPath());
cmsPage1.setDataUrl(cmsPage.getDataUrl());
cmsPageRepository.save(cmsPage1);//
return new CmsPageResult(CommonCode.SUCCESS,cmsPage1);
}
return new CmsPageResult(CommonCode.FAIL,null);
}
关于Optional:
Optional是jdk1.8引入的类型,Optional是一个容器对象,它包括了我们需要的对象,使用isPresent方法判断所包
含对象是否为空,isPresent方法返回false则表示Optional包含对象为空,否则可以使用get()取出对象进行操作。
Optional的优点是:
1、提醒你非空判断。
2、将对象非空检测标准化。
2.1.3删除数据
public ResponseResult delete(String id){
Optional<CmsPage> optional = cmsPageRepository.findById(id);
if (optional.isPresent()){
cmsPageRepository.deleteById(id);
return new ResponseResult(CommonCode.SUCCESS);
}
return new ResponseResult(CommonCode.FAIL);
}