From 11dc911d2bb0e74ee429f8ab778cb48009d9ffb3 Mon Sep 17 00:00:00 2001 From: WangHao <43278047@qq.com> Date: Thu, 8 Oct 2020 20:20:40 +0800 Subject: [PATCH] =?UTF-8?q?SpringBoot=E9=9B=86=E6=88=90MongoDB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/note/NmNoteController.java | 17 ++++- .../controller/MongdbApplicationTests.java | 58 ++++++++++++++++ .../web/test/controller/SqBookmarkTest.java | 7 -- .../src/main/resources/application-druid.yml | 11 +++ .../src/main/resources/application.yml | 3 + ruoyi-common/pom.xml | 8 +++ .../ruoyi/note/domain/NoteContentMgDb.java | 36 ++++++++++ .../note/service/INoteRepositoryService.java | 21 ++++++ .../impl/NoteRepositoryServiceImpl.java | 69 +++++++++++++++++++ 9 files changed, 222 insertions(+), 8 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/MongdbApplicationTests.java create mode 100644 ruoyi-note/src/main/java/com/ruoyi/note/domain/NoteContentMgDb.java create mode 100644 ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java create mode 100644 ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NoteRepositoryServiceImpl.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/note/NmNoteController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/note/NmNoteController.java index 189f79359..3d2585d06 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/note/NmNoteController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/note/NmNoteController.java @@ -3,7 +3,8 @@ package com.ruoyi.web.controller.note; import java.util.List; import com.ruoyi.common.core.domain.entity.SysUser; -import com.sun.org.apache.bcel.internal.generic.NEW; +import com.ruoyi.note.domain.NoteContentMgDb; +import com.ruoyi.note.service.INoteRepositoryService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -36,6 +37,20 @@ public class NmNoteController extends BaseController @Autowired private INmNoteService nmNoteService; + @Autowired + private INoteRepositoryService noteRepositoryService; + + + //测试 + @GetMapping("/selectBymenuNote2") + public void save() { + NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); + noteContentMgDb.setId(7L); + noteContentMgDb.setNoteContent("宋人头2"); + noteRepositoryService.save(noteContentMgDb); + } + + /** * 用户查看栏目下的所有便签 diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/MongdbApplicationTests.java b/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/MongdbApplicationTests.java new file mode 100644 index 000000000..a1f80b4de --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/MongdbApplicationTests.java @@ -0,0 +1,58 @@ +package com.ruoyi.web.test.controller; + +import com.ruoyi.note.domain.NoteContentMgDb; +import com.ruoyi.note.service.INoteRepositoryService; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +/** + * @Auther: Wang + * @Date: 2020/10/08 19:11 + * 功能描述: + */ +public class MongdbApplicationTests extends BaseSpringBootTest{ + @Autowired + private INoteRepositoryService noteRepositoryService; + + /** + * 查询所有信息 + */ + @Test + public void findAll() { + List all = noteRepositoryService.findAll(); + System.out.println(all.size()); + } + + /** + * 新增信息 + */ + @Test + public void save() { + NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); + noteContentMgDb.setId(19L); + noteContentMgDb.setNoteContent("宋人头"); + noteRepositoryService.save(noteContentMgDb); + } + + + /** + * 修改信息 + */ + @Test + public void update() { + NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); + noteContentMgDb.setId(2L); + noteContentMgDb.setNoteContent("吴很帅"); + noteRepositoryService.update(noteContentMgDb); + } + + /** + * 删除信息 + */ + @Test + public void delete() { + noteRepositoryService.delete(3); + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/SqBookmarkTest.java b/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/SqBookmarkTest.java index 2cebe23cc..cefb315da 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/SqBookmarkTest.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/test/controller/SqBookmarkTest.java @@ -15,12 +15,8 @@ import com.ruoyi.web.controller.system.SysLoginController; import com.ruoyi.web.controller.yunbookmark.SqBookmarkController; import org.junit.Before; import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.web.servlet.MockMvc; @@ -31,10 +27,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import javax.annotation.Resource; import javax.servlet.http.HttpSession; -import java.util.List; -import java.util.Map; import static org.junit.Assert.assertEquals; diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-druid.yml index 064682bfd..4a444ea15 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-druid.yml @@ -55,3 +55,14 @@ spring: wall: config: multi-statement-allow: true + + + #mongodb + data: + mongodb: + uri: mongodb://localhost:27017/ChangQuYun +#设置了密码 +# spring.data.mongodb.uri=mongodb://admin:123456@192.168.56.128:27017/admin +#格式: mongodb://账号:密码@ip:端口/数据库?认证数据库 +# 配置MongoTemplate的执行日志 +# logging.level.org.springframework.data.mongodb.core=debug diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 032b12c23..bfb405412 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -146,3 +146,6 @@ uid: boostPower: 3 # RingBuffer size扩容参数, 可提高UID生成的吞吐量, 默认:3 paddingFactor: 50 # 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50 #scheduleInterval: 60 # 默认:不配置此项, 即不实用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒 + + + diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 812879e12..4547b25ee 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -161,6 +161,14 @@ lombok 1.18.12 + + + org.springframework.boot + spring-boot-starter-data-mongodb + 2.3.4.RELEASE + + + diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/domain/NoteContentMgDb.java b/ruoyi-note/src/main/java/com/ruoyi/note/domain/NoteContentMgDb.java new file mode 100644 index 000000000..d99beaf34 --- /dev/null +++ b/ruoyi-note/src/main/java/com/ruoyi/note/domain/NoteContentMgDb.java @@ -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; + } +} diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java b/ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java new file mode 100644 index 000000000..45f354940 --- /dev/null +++ b/ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java @@ -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 findAll(); + + void delete(Integer id); +} diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NoteRepositoryServiceImpl.java b/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NoteRepositoryServiceImpl.java new file mode 100644 index 000000000..bcb9a6ef2 --- /dev/null +++ b/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NoteRepositoryServiceImpl.java @@ -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 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); + } +}