MongoDB入门+深入(二)--项目实战
admin
2024-01-13 04:10:38

目录

一、Java项目 & MongoDB技术

1、mongodb-driver

2、SpringDataMongoDB

二、Java & MongoDB项目实例

1、项目介绍

2、创建Spring Boot工程 项目名"article"

3、项目代码编写

3.1、分页查询功能

3.2、实现评论点赞


一、Java项目 & MongoDB技术

1、mongodb-driver

        mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动.

2、SpringDataMongoDB

SpringData家族成员之一,用于操作MongDB的持久层框架,封装了底层的mongodb-driver。

Spring-data-mongodb官网

二、Java & MongoDB项目实例

1、项目介绍

文章评论的项目

需求:某网站中的文章评论。评论下还有子评论等。

需要实现以下功能:

1、基本增删改查API

2、根据文章ID查询评论

3、评论点赞

表结构分析

数据库:articledb

文章评论集合:comment

字段名称字段含义字段类型备注
_idIDObjectId或StringMongoDB的主键的字段
articleid文章IDString
content评论内容String
userid评论人IDString
nickname评论人昵称String
createddatetime评论的日期时间Date
likenum点赞数Int32
replnum回复数Int32
state状态String0:不可见;1:可见;
parentid上级IDString如果为0表示文章的顶级评论
1、mongdb数据创建一条
db.comment.insert({_id:'1',content:'我相信是金子,总会有闪光之时。',publishtime:ISODate("20221022 10:10:10"),
userid:'SHK001',nickname:'悟空',createdatetime:ISODate("20221022 20:20:20"),likenum:100,
replynum:200,state:'good',parentid:'wwk334',articleid:'QW987'});

2、创建Spring Boot工程 项目名"article"

创建Spring Boot 工程Spring Boot入门+深入(一)

1、pom.xml


org.springframework.bootspring-boot-starter-parent2.1.6.RELEASE4.0.0articlearticle0.0.1-SNAPSHOTjararticlehttp://maven.apache.orgUTF-83.0.0junitjunittestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testorg.springframework.bootspring-boot-starter-data-mongodborg.projectlomboklombok

2、application.yml

spring: #数据源配置data: mongodb: #主机地址host: 192.168.0.125#数据源database: articledb#也可以使用uri连接#uri: mongodb://192.168.0.125:27017/articledb#默认端口是27017port: 27017username: adminpassword: admin

3、项目启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringMainApplication {public static void main(String[] args) {SpringApplication.run(SpringMainApplication.class,args);}}

上述配置完成,直接启动项目

...
2022-10-22 22:40:46.556  INFO 9032 --- [168.0.125:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1}] to 192.168.0.125:27017
2022-10-22 22:40:46.562  INFO 9032 --- [168.0.125:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=192.168.0.125:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[6, 0, 2]}, minWireVersion=0, maxWireVersion=17, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=4659100}
2022-10-22 22:40:46.757  INFO 9032 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-22 22:40:46.761  INFO 9032 --- [           main] com.lwz.SpringMainApplication            : Started SpringMainApplication in 5.824 seconds (JVM running for 6.232)

控制台没有报错,代表启动成功。

3、项目代码编写

创建实体类

创建包com.lwz.article,包下建包entity用于存放实体类,创建实体类

Comment.java

package com.lwz.article.entity;import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;import lombok.Data;@Data
@Document(collection = "comment")//可以省略,如果省略默认使用类名小写映射集合
//@CompoundIndex(def = "{'userid':1,'nickname':-1}") //复合索引
public class Comment implements Serializable{private static final long serialVersionUID = 1L;@Idprivate String id;//主键@Field("content")//该属性对应mongdb中的字段名/实体类名称和db中的名称不一致时使用private String content;//吐槽内容private Date publishtime;//发布日期@Indexed//添加一个单字段索引private String userid;//发布人idprivate String nickname;//昵称private LocalDateTime createdatetime;//评论的日期时间private Integer likenum;//点赞数private Integer replynum;//回复数private String state;//状态private String parentid;//上级idprivate String articleid;@Overridepublic String toString() {return "Comment [id=" + id + ", content=" + content + ", publishtime=" + publishtime + ", userid=" + userid+ ", nickname=" + nickname + ", createdatetime=" + createdatetime + ", likenum=" + likenum+ ", replynum=" + replynum + ", state=" + state + ", parentid=" + parentid + ", articleid=" + articleid+ "]";}}

CommentRepository.java

package com.lwz.article.repository;import org.springframework.data.mongodb.repository.MongoRepository;import com.lwz.article.entity.Comment;public interface CommentRepository extends MongoRepository{}

CommentService ---文章评论的增删改查

package com.lwz.article.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.lwz.article.entity.Comment;
import com.lwz.article.repository.CommentRepository;@Service
public class CommentService {@Autowiredprivate CommentRepository commentRepository;/*** 保存评论*/public void saveComment(Comment comment) {commentRepository.save(comment);}/*** 更新评论*/public void updateComment(Comment comment) {commentRepository.save(comment);}/*** 根据ID删除评论*/public void deleteCommentById(String id) {commentRepository.deleteById(id);}/*** 查询所有评论*/public List findCommentList() {return commentRepository.findAll();}/*** 根据ID查询评论*/public Comment findCommentById(String id) {return commentRepository.findById(id).get();}
}

CommentServiceTest测试类

package com.lwz.article.service;import java.util.List;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.lwz.article.entity.Comment;@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceTest {@Autowiredprivate CommentService commentService;@Testpublic void findCommentListTest() {List commentList = commentService.findCommentList();System.out.println(commentList);}
}

启动测试类进行测试:

错误1:

Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 192.168.0.125:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)

解决方式:

MongoDB中每个数据库之间是相互独立的,都有独立的权限,正确的做法是使用root账号在【将要操作的数据库】中创建一个【子账号】,在用这个子账号连接mongo:

>use articledbswitched to db articledb> db.createUser({user:"admin",pwd:"admin",roles:[{role:"dbOwner",db:"articledb"}]})
...

错误2:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2022-10-22'; nested exception is java.lang.IllegalArgumentExceptionat org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:112)

解决方式:

实体类Date类型和数据库中数据格式,创建mongdb时间数据使用ISODate函数

db.comment.insert({_id:'1',content:'我相信是金子,总会有闪光之时。',publishtime:ISODate("20221022 10:10:10"),
userid:'SHK001',nickname:'悟空',createdatetime:ISODate("20221022 20:20:20"),likenum:100,
replynum:200,state:'good',parentid:'wwk334',articleid:'QW987'});

问题解决后,再次启动测试类,成功!

控制台会打印如下结果:

[Comment [id=1, content=我相信是金子,总会有闪光之时。, publishtime=Sat Oct 22 18:10:10 CST 2022, userid=SHK001, nickname=悟空, createdatetime=2022-10-23T04:20:20, likenum=100, replynum=200, state=good, parentid=wwk334, articleid=QW987]]

3.1、分页查询功能

根据上级id查询文章评论的分页列表。

(1)、CommentRepository.java新增方法

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;import com.lwz.article.entity.Comment;public interface CommentRepository extends MongoRepository{Page findByParentid(String parentid,Pageable pageable);}

(2)、CommentService新增方法

	/*** 根据上级id分页查询*/public Page findCommentListByParentid(String parentid,int page,int size) {return commentRepository.findByParentid(parentid,PageRequest.of(page-1, size));}

(3)、CommentServiceTest测试类新增测试方法

	@Testpublic void findCommentListByParentidTest() {Page page = commentService.findCommentListByParentid("wwk334",1,2);System.out.println(page.getTotalElements());System.out.println(page.getContent());}

执行测试类:结果

1
[Comment [id=1, content=我相信是金子,总会有闪光之时。, publishtime=Sat Oct 22 18:10:10 CST 2022, userid=SHK001, nickname=悟空, createdatetime=2022-10-23T04:20:20, likenum=100, replynum=200, state=good, parentid=wwk334, articleid=QW987]]

3.2、实现评论点赞

方法1:

	/*** 更新点赞数*/public void updateCommentLikenumById(String id) {Comment comment = commentRepository.findById(id).get();comment.setLikenum(comment.getLikenum()+1);commentRepository.save(comment);}

方法2:

我们可以使用MongoTemplate类来实现对某列的操作。

(1)、修改CommentService

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;--------------------------------------------------------@Autowiredprivate MongoTemplate mongoTemplate;/*** 更新点赞数*/public void updateCommentLikenum(String id) {//查询条件Query query = Query.query(Criteria.where("_id").is(id));//更新条件Update update = new Update();update.inc("likenum");mongoTemplate.updateFirst(query, update, Comment.class);}

(2)、CommentServiceTest测试类新增测试方法

	@Testpublic void updateCommentLikenum() {Comment comment = commentService.findCommentById("1");System.out.println(comment.getLikenum());commentService.updateCommentLikenum("1");Comment comment1 = commentService.findCommentById("1");System.out.println(comment1.getLikenum());}

测试结果:

100
101

每天⽤⼼记录⼀点点。内容也许不重要,但习惯很重要!

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

MongoDB入门+深入(一)

相关内容

热门资讯

5.65亿得主隐身34天损失2... 最淡定得主!8月29日上午,中国第一巨奖5.65亿得主领奖,距他中出巨奖已经过去了34天,第一巨奖得...
深圳投资纳税入户手续 深圳企业... 事项内容   投资纳税入户 法律依据   中华人民共和国户口登记条例 数量及...
外媒费解:高市为何同时得罪中韩... 参考消息网12月22日报道澳大利亚洛伊研究所网站12月19日刊发题为《日韩关系:高市早苗令人迷惑的挑...
深圳本市居民办理市内移居的手续... 事项内容  【市居民办理市内移居 法律依据   中华人民共和国户口登记条例 ...
深圳居民身份证办理手续 深圳居... 事项内容   居民身份证办理 法律依据   中华人民共和国居民身份证法 数量...