SpringBoot集成MongoDB
This commit is contained in:
parent
41146e7c6d
commit
11dc911d2b
@ -3,7 +3,8 @@ package com.ruoyi.web.controller.note;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
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.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@ -36,6 +37,20 @@ public class NmNoteController extends BaseController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private INmNoteService nmNoteService;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户查看栏目下的所有便签
|
* 用户查看栏目下的所有便签
|
||||||
|
@ -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<NoteContentMgDb> 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);
|
||||||
|
}
|
||||||
|
}
|
@ -15,12 +15,8 @@ import com.ruoyi.web.controller.system.SysLoginController;
|
|||||||
import com.ruoyi.web.controller.yunbookmark.SqBookmarkController;
|
import com.ruoyi.web.controller.yunbookmark.SqBookmarkController;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.http.MediaType;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
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.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@ -55,3 +55,14 @@ spring:
|
|||||||
wall:
|
wall:
|
||||||
config:
|
config:
|
||||||
multi-statement-allow: true
|
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
|
||||||
|
@ -146,3 +146,6 @@ uid:
|
|||||||
boostPower: 3 # RingBuffer size扩容参数, 可提高UID生成的吞吐量, 默认:3
|
boostPower: 3 # RingBuffer size扩容参数, 可提高UID生成的吞吐量, 默认:3
|
||||||
paddingFactor: 50 # 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50
|
paddingFactor: 50 # 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50
|
||||||
#scheduleInterval: 60 # 默认:不配置此项, 即不实用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒
|
#scheduleInterval: 60 # 默认:不配置此项, 即不实用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,6 +161,14 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.12</version>
|
<version>1.18.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- lombok 实体类工具-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
<version>2.3.4.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user