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 3d2585d06..ba923f97c 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,6 +3,7 @@ package com.ruoyi.web.controller.note; import java.util.List; import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.note.domain.NoteContentMgDb; import com.ruoyi.note.service.INoteRepositoryService; import org.springframework.security.access.prepost.PreAuthorize; @@ -37,20 +38,6 @@ 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); - } - - /** * 用户查看栏目下的所有便签 @@ -59,9 +46,8 @@ public class NmNoteController extends BaseController @GetMapping("/selectBymenuNote") public TableDataInfo selectBymenuNote(Long menuId) { - SysUser sysUser=getAuthUser(); NmNote nmNote= new NmNote(); - nmNote.setUserId(sysUser.getUserId()); + nmNote.setUserId(getAuthUser().getUserId()); nmNote.setMenuId(menuId); startPage(); List list = nmNoteService.selectNmNoteList(nmNote); @@ -69,17 +55,28 @@ public class NmNoteController extends BaseController } + /** - * 用户新增便签 + * 用户新增便签 直接创建成功 */ @PostMapping("/addUserNote") public AjaxResult userAddNote(@RequestBody NmNote nmNote) { - SysUser sysUser=getAuthUser(); - nmNote.setUserId(sysUser.getUserId()); + + nmNote.setUserId(getAuthUser().getUserId()); return toAjax(nmNoteService.insertNmNote(nmNote)); } + /** + * 实时更新文章信息 不用做缓存 + */ + @PostMapping("/userUpdateNote") + public AjaxResult userUpdateNote(@RequestBody NmNote nmNote) + { + + return toAjax(nmNoteService.userUpdateNote(nmNote)); + } + /** *用户 获取便签详细信息 然后修改 */ 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 index a1f80b4de..3f3ff2dbd 100644 --- 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 @@ -1,11 +1,18 @@ package com.ruoyi.web.test.controller; +import com.github.wujun234.uid.UidGenerator; +import com.ruoyi.common.constant.Constants; +import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.note.domain.NoteContentMgDb; import com.ruoyi.note.service.INoteRepositoryService; +import org.bson.types.ObjectId; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.Resource; +import java.util.Collection; import java.util.List; +import java.util.Map; /** * @Auther: Wang @@ -16,6 +23,16 @@ public class MongdbApplicationTests extends BaseSpringBootTest{ @Autowired private INoteRepositoryService noteRepositoryService; + @Autowired + private RedisCache redisCache; + + + @Resource + private UidGenerator cachedUidGenerator; + + @Resource + private UidGenerator defaultUidGenerator; + /** * 查询所有信息 */ @@ -31,7 +48,7 @@ public class MongdbApplicationTests extends BaseSpringBootTest{ @Test public void save() { NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); - noteContentMgDb.setId(19L); + noteContentMgDb.setId(15108230363503104L); noteContentMgDb.setNoteContent("宋人头"); noteRepositoryService.save(noteContentMgDb); } @@ -43,7 +60,7 @@ public class MongdbApplicationTests extends BaseSpringBootTest{ @Test public void update() { NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); - noteContentMgDb.setId(2L); +// noteContentMgDb.setId(2L); noteContentMgDb.setNoteContent("吴很帅"); noteRepositoryService.update(noteContentMgDb); } @@ -55,4 +72,51 @@ public class MongdbApplicationTests extends BaseSpringBootTest{ public void delete() { noteRepositoryService.delete(3); } + + + + + + + + + + @Test + public void addredis(){ + for (int i=0;i<10;i++){ + redisCache.setCacheObject(Constants.NM_NOTE_CONTENT+defaultUidGenerator.getUID(),i); + } + + } + @Test + public void getRedis(){ + String redis= redisCache.getCacheObject("nm_note:15563127529716224"); + System.out.println("redis:"+redis); + + } + + + /** + * 缓存文章 转义到 mgDB + * + * @param + * @return + */ + @Test + public void redisToMgDB(){ + + + + } + + + + + + + + + + + } diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index bfb405412..97f7a7844 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -82,7 +82,7 @@ token: # 令牌自定义标识 header: Authorization # 令牌密钥 - secret: abcdefghijklmnopqrstuvwxyz + secret: abc4abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) expireTime: 300 diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java index 9ee27077a..8879fffed 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java @@ -2,7 +2,7 @@ package com.ruoyi.common.constant; /** * 通用常量信息 - * + * * @author ruoyi */ public class Constants @@ -121,4 +121,9 @@ public class Constants * 资源映射路径 前缀 */ public static final String RESOURCE_PREFIX = "/profile"; + + /** + * mongodb note文章 + */ + public static final String NM_NOTE_CONTENT = "nm_note:"; } diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/domain/NmNote.java b/ruoyi-note/src/main/java/com/ruoyi/note/domain/NmNote.java index 1793c871e..96cc86d59 100644 --- a/ruoyi-note/src/main/java/com/ruoyi/note/domain/NmNote.java +++ b/ruoyi-note/src/main/java/com/ruoyi/note/domain/NmNote.java @@ -8,8 +8,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import com.ruoyi.common.annotation.Excel; -import javax.persistence.Column; -import javax.persistence.Table; +import javax.persistence.*; import java.util.Date; /** @@ -27,6 +26,8 @@ public class NmNote private static final long serialVersionUID = 1L; /** Note便签ID */ + @Id + @GeneratedValue(generator = "JDBC")//返回自增长主键 @Column(name = "note_id") @Excel(name = "Note便签ID") private Long noteId; @@ -112,4 +113,12 @@ public class NmNote private Date updateTime; + @Column(name = "tiymce_ueditor") + private Long tiymceUeditor; + + /**富文本文章内容*/ + @Transient + private String UeditorContent; + + } diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/service/INmNoteService.java b/ruoyi-note/src/main/java/com/ruoyi/note/service/INmNoteService.java index d9f5d02dc..2d986ec5a 100644 --- a/ruoyi-note/src/main/java/com/ruoyi/note/service/INmNoteService.java +++ b/ruoyi-note/src/main/java/com/ruoyi/note/service/INmNoteService.java @@ -66,4 +66,20 @@ public interface INmNoteService * @return 便签管理 */ public NmNote selectNmNoteuserById(Long noteId,Long userID); + + /** + * 用户便签内容 实时储存到数据库 + * + * @param nmNote + * @return + */ + public int userUpdateNote(NmNote nmNote); + + /** + * redis的文章 转移更新到MongoDb + * + * @param + * @return + */ + public void redisToMongonDB(); } 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 index 45f354940..43d444c68 100644 --- a/ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java +++ b/ruoyi-note/src/main/java/com/ruoyi/note/service/INoteRepositoryService.java @@ -1,6 +1,10 @@ package com.ruoyi.note.service; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.Filters; import com.ruoyi.note.domain.NoteContentMgDb; +import com.sun.corba.se.spi.ior.ObjectId; +import org.springframework.data.mongodb.core.mapping.Document; import java.util.List; @@ -18,4 +22,7 @@ public interface INoteRepositoryService { List findAll(); void delete(Integer id); + + //根据id查询 + List findById(String id); } diff --git a/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NmNoteServiceImpl.java b/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NmNoteServiceImpl.java index 95150238e..6b91fd198 100644 --- a/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NmNoteServiceImpl.java +++ b/ruoyi-note/src/main/java/com/ruoyi/note/service/impl/NmNoteServiceImpl.java @@ -1,13 +1,31 @@ package com.ruoyi.note.service.impl; +import java.util.Collection; import java.util.List; +import java.util.Map; + +import cn.hutool.core.date.DateUtil; +import com.github.wujun234.uid.UidGenerator; +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.ruoyi.common.constant.Constants; +import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.note.domain.NmNoteContent; +import com.ruoyi.note.domain.NoteContentMgDb; +import com.ruoyi.note.service.INmNoteContentService; +import com.ruoyi.note.service.INoteRepositoryService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.stereotype.Service; import com.ruoyi.note.mapper.NmNoteMapper; import com.ruoyi.note.domain.NmNote; import com.ruoyi.note.service.INmNoteService; +import javax.annotation.Resource; + /** * 便签管理Service业务层处理 * @@ -15,11 +33,23 @@ import com.ruoyi.note.service.INmNoteService; * @date 2020-09-12 */ @Service -public class NmNoteServiceImpl implements INmNoteService -{ +public class NmNoteServiceImpl implements INmNoteService { @Autowired private NmNoteMapper nmNoteMapper; + @Resource + private UidGenerator defaultUidGenerator; + + @Autowired + private INmNoteContentService nmNoteContentService; + + @Autowired + private INoteRepositoryService noteRepositoryService; + + @Autowired + private RedisCache redisCache; + + /** * 查询便签管理 * @@ -27,8 +57,7 @@ public class NmNoteServiceImpl implements INmNoteService * @return 便签管理 */ @Override - public NmNote selectNmNoteById(Long noteId) - { + public NmNote selectNmNoteById(Long noteId) { return nmNoteMapper.selectNmNoteById(noteId); } @@ -39,8 +68,7 @@ public class NmNoteServiceImpl implements INmNoteService * @return 便签管理 */ @Override - public List selectNmNoteList(NmNote nmNote) - { + public List selectNmNoteList(NmNote nmNote) { return nmNoteMapper.selectNmNoteList(nmNote); } @@ -51,10 +79,11 @@ public class NmNoteServiceImpl implements INmNoteService * @return 结果 */ @Override - public int insertNmNote(NmNote nmNote) - { + public int insertNmNote(NmNote nmNote) { nmNote.setCreateTime(DateUtils.getNowDate()); - return nmNoteMapper.insertNmNote(nmNote); + nmNote.setTitle(DateUtil.now()); + nmNote.setTiymceUeditor(defaultUidGenerator.getUID()); + return nmNoteMapper.insertSelective(nmNote); } /** @@ -64,8 +93,7 @@ public class NmNoteServiceImpl implements INmNoteService * @return 结果 */ @Override - public int updateNmNote(NmNote nmNote) - { + public int updateNmNote(NmNote nmNote) { nmNote.setUpdateTime(DateUtils.getNowDate()); return nmNoteMapper.updateNmNote(nmNote); } @@ -77,8 +105,7 @@ public class NmNoteServiceImpl implements INmNoteService * @return 结果 */ @Override - public int deleteNmNoteByIds(Long[] noteIds) - { + public int deleteNmNoteByIds(Long[] noteIds) { return nmNoteMapper.deleteNmNoteByIds(noteIds); } @@ -89,8 +116,7 @@ public class NmNoteServiceImpl implements INmNoteService * @return 结果 */ @Override - public int deleteNmNoteById(Long noteId) - { + public int deleteNmNoteById(Long noteId) { return nmNoteMapper.deleteNmNoteById(noteId); } @@ -101,11 +127,71 @@ public class NmNoteServiceImpl implements INmNoteService * @return 便签管理 */ @Override - public NmNote selectNmNoteuserById(Long noteId,Long userID) { + public NmNote selectNmNoteuserById(Long noteId, Long userID) { NmNote nmNote = new NmNote(); nmNote.setNoteId(noteId); nmNote.setUserId(userID); - return nmNoteMapper.selectOne(nmNote); + NmNote isnmNote1 = nmNoteMapper.selectOne(nmNote); + //查询对应的文章数据 + //1.查redis缓存 + String noteContent = redisCache.getCacheObject(Constants.NM_NOTE_CONTENT + isnmNote1.getTiymceUeditor()); + if (noteContent != null && !"".equals(noteContent)) { + isnmNote1.setUeditorContent(noteContent); + } else { + // 2不存在就走mogodb + List NoteContentMgDb = noteRepositoryService.findById(isnmNote1.getTiymceUeditor() + ""); + isnmNote1.setUeditorContent(NoteContentMgDb.get(0).getNoteContent()); + } + return isnmNote1; + } + + /** + * 实时修改数据 保存到缓存中 + * + * @param nmNote + * @return int + */ + @Override + public int userUpdateNote(NmNote nmNote) { + //储存到redis中 只缓存频繁操作的文章内容 + redisCache.setCacheObject(Constants.NM_NOTE_CONTENT+nmNote.getTiymceUeditor(),nmNote.getUeditorContent()); + return 1; + } + + /** + * redis的文章 转移更新到MongoDb + * + * @param + * @return + */ + @Override + public void redisToMongonDB() { + //模糊查询 获取所有的key + Collection listNote= redisCache.keys(Constants.NM_NOTE_CONTENT+"*"); + for(String str:listNote){ + //文章UUID + String mgDbContentUUID=str.replace(Constants.NM_NOTE_CONTENT,""); + //文章 + String redisContent= redisCache.getCacheObject(str); + //查询mongoDb 存在就修改 不存在就新增 + List listMgDbContent = noteRepositoryService.findById(mgDbContentUUID); + if (listMgDbContent!=null&&!listMgDbContent.isEmpty()){ + //修改 + NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); + noteContentMgDb.setId(Long.valueOf(mgDbContentUUID)); + noteContentMgDb.setNoteContent(redisContent); + noteRepositoryService.update(noteContentMgDb); + }else { + //新增 + NoteContentMgDb noteContentMgDb = new NoteContentMgDb(); + noteContentMgDb.setId(Long.valueOf(mgDbContentUUID)); + noteContentMgDb.setNoteContent(redisContent); + noteRepositoryService.save(noteContentMgDb); + } + //删除对应缓存 + redisCache.deleteObject(str); + } + } 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 index bcb9a6ef2..a8c8b2b3b 100644 --- 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 @@ -1,10 +1,15 @@ package com.ruoyi.note.service.impl; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.Filters; import com.ruoyi.note.domain.NoteContentMgDb; import com.ruoyi.note.service.INoteRepositoryService; + +import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; @@ -23,6 +28,8 @@ public class NoteRepositoryServiceImpl implements INoteRepositoryService { @Autowired private MongoTemplate mongoTemplate; + + /** * 新增信息 * @param student @@ -66,4 +73,16 @@ public class NoteRepositoryServiceImpl implements INoteRepositoryService { NoteContentMgDb byId = mongoTemplate.findById(1, NoteContentMgDb.class); mongoTemplate.remove(byId); } + + + + @Override + public List findById(String id) { + //修改的条件 + Query query = new Query(Criteria.where("_id").is(Long.valueOf(id))); + //修改的内容 + return mongoTemplate.find(query, NoteContentMgDb.class); + } + + } diff --git a/ruoyi-note/src/main/resources/mapper/note/NmNoteMapper.xml b/ruoyi-note/src/main/resources/mapper/note/NmNoteMapper.xml index aae3dacdc..986937d3a 100644 --- a/ruoyi-note/src/main/resources/mapper/note/NmNoteMapper.xml +++ b/ruoyi-note/src/main/resources/mapper/note/NmNoteMapper.xml @@ -22,11 +22,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + - select note_id, user_id, title, description, menu_id, background, note_count, note_sort, is_state, read_progress, is_star, is_delete, top_flag, is_share, is_encryption, create_time, update_time from nm_note + select note_id, user_id, title, description, menu_id, background, note_count, note_sort, is_state, read_progress, is_star, is_delete, top_flag, is_share, is_encryption, create_time, update_time,tiymce_ueditor from nm_note