一、 需求
設(shè)計(jì)一個(gè)網(wǎng)站評(píng)論的數(shù)據(jù)庫表設(shè)計(jì),實(shí)現(xiàn)的功能如下
- 對(duì)于一個(gè)問題的主評(píng)論
- 對(duì)于一個(gè)評(píng)論的子評(píng)論
二、 設(shè)計(jì)
| 字段名稱 | 字段含義 | 字段類型 | 備注 |
|---|---|---|---|
| _id | ID | String | 主鍵 |
| articleId | 文章id | String | 文章的id |
| content | 評(píng)論內(nèi)容 | String | |
| userId | 評(píng)論人id | String | |
| nickName | 評(píng)論人昵稱 | String | |
| createDatetime | 評(píng)論時(shí)間 | Date | |
| likeNum | 點(diǎn)贊數(shù) | int32 | |
| replyNum | 回復(fù)數(shù) | int32 | |
| state | 狀態(tài) | String | 0為不可見,1可見 |
| parentId | 父類id | String | 父類id,用于評(píng)論下面添加評(píng)論 |
三、 實(shí)現(xiàn)
-
技術(shù)選型
mongodb-driver
是mongo官方推出的java連接mongodb的驅(qū)動(dòng)包,相當(dāng)于JDBC驅(qū)動(dòng)SpringDataMongoDb
springData家族成員之一,用于操作MongoDb的持久層框架,封裝了底層的mongodb-driver
pom依賴
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
- 文件配置
<!-- application.properties -->
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/articaledb
- 項(xiàng)目核心代碼
-
dao層
public interface CommentRepository extends MongoRepository<Comment, String> {} -
service層
- 接口
public interface CommentService { List<Comment> saveAll(Iterable<Comment> var1); List<Comment> findAll(); List<Comment> findAll(Sort var1); Comment insert(Comment var1); List<Comment> insert(Iterable<Comment> var1); List<Comment> findAll(Example<Comment> var1); List<Comment> findAll(Example<Comment> var1, Sort var2); Page<Comment> findByParentId(String parentId, int page, int size); void update(String id); }- 實(shí)現(xiàn)類
@Service public class CommentServiceImpl implements CommentService { @Autowired CommentRepository commentRepository; @Autowired MongoTemplate mongoTemplate; @Override public List<Comment> saveAll(Iterable<Comment> var1) { return commentRepository.saveAll(var1); } @Override public List<Comment> findAll() { return commentRepository.findAll(); } @Override public List<Comment> findAll(Sort var1) { return commentRepository.findAll(var1); } @Override public Comment insert(Comment var1) { return commentRepository.insert(var1); } @Override public List<Comment> insert(Iterable<Comment> var1) { return commentRepository.insert(var1); } @Override public List<Comment> findAll(Example<Comment> var1) { return commentRepository.findAll(var1); } @Override public List<Comment> findAll(Example<Comment> var1, Sort var2) { return commentRepository.findAll(var1, var2); } @Override public Page<Comment> findByParentId(String parentId, int page, int size) { return commentRepository.findByParentId(parentId, PageRequest.of(page-1,size)); } @Override public void update(String id) { Query query = Query.query(Criteria.where("_id").is(id)); Update update = new Update(); update.inc("likeNum"); mongoTemplate.updateFirst(query, update, "comment"); } } 測(cè)試類
@SpringBootTest @RunWith(SpringRunner.class) class CommentServiceImplTest { @Autowired CommentService commentService; /** 查詢?nèi)? **/ @Test void findAll() { List<Comment> comments = commentService.findAll(); System.out.println(comments); } /** 保存 **/ @Test void insert() { Comment comment = new Comment(); comment.setContent("這是一條評(píng)論"); comment.setPublishDate(new Date()); comment.setUserId("1"); comment.setUserName("馬"); comment.setCreateDatetime(LocalDateTime.now()); comment.setLikeNum(100); comment.setReplyNum(100); comment.setState("1"); comment.setArticaleId("25"); commentService.insert(comment); } @Test void findByParentId() { commentService.findByParentId("3", 1, 2); } @Test void update() { String id="1"; commentService.update(id); } } -