SpringBoot集成MongoDB

This commit is contained in:
WangHao
2020-10-08 20:20:40 +08:00
parent 41146e7c6d
commit 11dc911d2b
9 changed files with 222 additions and 8 deletions

View File

@ -0,0 +1,36 @@
package com.ruoyi.note.domain;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @Auther: Wang
* @Date: 2020/10/08 19:07
* 功能描述:
*/
@Document(collection = "NoteContent")
public class NoteContentMgDb implements Serializable {
@Id
private Long id;
private String NoteContent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNoteContent() {
return NoteContent;
}
public void setNoteContent(String noteContent) {
NoteContent = noteContent;
}
}

View File

@ -0,0 +1,21 @@
package com.ruoyi.note.service;
import com.ruoyi.note.domain.NoteContentMgDb;
import java.util.List;
/**
* @Auther: Wang
* @Date: 2020/10/08 19:01
* 功能描述:
*/
public interface INoteRepositoryService {
void save(NoteContentMgDb noteContentMgDb);
void update(NoteContentMgDb noteContentMgDb);
List<NoteContentMgDb> findAll();
void delete(Integer id);
}

View File

@ -0,0 +1,69 @@
package com.ruoyi.note.service.impl;
import com.ruoyi.note.domain.NoteContentMgDb;
import com.ruoyi.note.service.INoteRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: wanghao
* @Date: 2020/10/08 19:02
* @Description:
*/
@Service
public class NoteRepositoryServiceImpl implements INoteRepositoryService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 新增信息
* @param student
*/
@Override
public void save(NoteContentMgDb student) {
mongoTemplate.save(student);
}
/**
* 修改信息
* @param noteContentMgDb
*/
@Override
public void update(NoteContentMgDb noteContentMgDb) {
//修改的条件
Query query = new Query(Criteria.where("id").is(noteContentMgDb.getId()));
//修改的内容
Update update = new Update();
update.set("name", noteContentMgDb.getNoteContent());
mongoTemplate.updateFirst(query,update, NoteContentMgDb.class);
}
/**
* 查询所有信息
* @return
*/
@Override
public List<NoteContentMgDb> findAll() {
return mongoTemplate.findAll(NoteContentMgDb.class);
}
/**
* 根据id查询所有信息
* @param id
*/
@Override
public void delete(Integer id) {
NoteContentMgDb byId = mongoTemplate.findById(1, NoteContentMgDb.class);
mongoTemplate.remove(byId);
}
}