袁雄奎 發表於 2020-4-2 19:43:00

SpringBoot整合SpringData MongoDB

<p>1.创建工程并引入依赖</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-parent<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.1.6.RELEASE<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">relativePath</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> lookup parent from repository </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-data-mongodb<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>2.创建application.yml</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">spring:
#数据源配置
data:
    mongodb:
      #主机地址
      host: </span>192.168.43.182<span style="color: rgba(0, 0, 0, 1)">
      #数据库
      database: articledb
      #默认端口是27017
      port: </span>27017<span style="color: rgba(0, 0, 0, 1)">
      #也可以使用uri连接
      #uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">192.168.43.182:27017/articledb</span>
<span style="color: rgba(0, 0, 0, 1)">      #如果数据库需要密码认证
      #uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">bobo:123456@192.168.43.182:27017/articledb</span></pre>
</div>
<p>3.创建启动类</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@SpringBootApplication
public class ArticleApplication {
    public static void main(String[] args) {
      SpringApplication.run(ArticleApplication.class, args);
    }
}</span></pre>
</div>
<p>4.启动项目,看是否能正常启动,控制台没有错误</p>
<p>5.文章评论实体类的编写</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 文章评论实体类
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Document(collection </span>= "comment")<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以省略,如果省略,则默认使用类名小写映射集合
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 复合索引
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @CompoundIndex( def = "{'userid': 1, 'nickname': -1}")</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Comment <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写</span>
<span style="color: rgba(0, 0, 0, 1)">    @Id
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> String id; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 主键
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该属性对应mongodb的字段的名字,如果一致,则无需该注解</span>
    @Field("content"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> String content; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 吐槽内容</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> Date publishtime; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发布日期
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加了一个单字段的索引</span>
<span style="color: rgba(0, 0, 0, 1)">    @Indexed
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> String userid; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发布人ID</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> String nickname; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 昵称</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> LocalDateTime createdatetime; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 评论的日期时间</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> Integer likenum; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点赞数</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> Integer replynum; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 回复数</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> String state;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">状态</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> String parentid; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 上级ID</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> String articleid; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文章id</span>

    <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">******************* get/set方法 ********************</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>  说明:<br>    索引可以大大提升查询效率,一般在查询字段上添加索引,索引的添加可以通过Mongo的命令来添加,也可以在Java的实体类中通过注解添加。<br>    1)单字段索引注解@Indexed<br>      声明该字段需要索引,建索引可以大大的提高查询效率。<br>      Mongo命令参考:db.comment.createIndex({"userid":1})</p>
<p>    2 )复合索引注解@CompoundIndex<br>      复合索引的声明,建复合索引可以有效地提高多字段的查询效率。<br>      Mongo命令参考:db.comment.createIndex({"userid":1,"nickname":-1})</p>
<p>6.创建数据访问接口</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 评论的持久层接口
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">interface</span> CommentDao <span style="color: rgba(0, 0, 255, 1)">extends</span> MongoRepository&lt;Comment, String&gt;<span style="color: rgba(0, 0, 0, 1)"> {
}</span></pre>
</div>
<p>7.创建业务逻辑类</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 评论的业务层
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Service
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> CommentService {
</span>
<span style="color: rgba(0, 0, 0, 1)">    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CommentDao commentDao;

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 保存一个评论
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> comment
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> saveComment(Comment comment) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果需要自定义主键,可以在这里指定主键;如果不指定主键,MongoDB会自动生成主键</span>
<span style="color: rgba(0, 0, 0, 1)">      commentDao.save(comment);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 更新评论
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> comment
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateComment(Comment comment) {
      commentDao.save(comment);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 根据id删除评论
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteCommentById(String id) {
      commentDao.deleteById(id);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 查询所有评论
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
   <span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Comment&gt;<span style="color: rgba(0, 0, 0, 1)"> findCommentList() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> commentDao.findAll();
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 根据id查询评论
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
   <span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Comment findCommentById(String id) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> commentDao.findById(id).get();
    }
}</span></pre>
</div>
<p>8.测试</p>
<div class="cnblogs_code">
<pre>@RunWith(SpringRunner.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)
@SpringBootTest(classes </span>= ArticleApplication.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> CommentServiceTest {

    @Autowired
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CommentService commentService;

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 保存一个评论
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSaveComment() {
      Comment comment </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Comment();
      comment.setArticleid(</span>"100000"<span style="color: rgba(0, 0, 0, 1)">);
      comment.setContent(</span>"测试添加的数据"<span style="color: rgba(0, 0, 0, 1)">);
      comment.setCreatedatetime(LocalDateTime.now());
      comment.setUserid(</span>"1003"<span style="color: rgba(0, 0, 0, 1)">);
      comment.setNickname(</span>"凯撒大帝"<span style="color: rgba(0, 0, 0, 1)">);
      comment.setState(</span>"1"<span style="color: rgba(0, 0, 0, 1)">);
      comment.setLikenum(</span>0<span style="color: rgba(0, 0, 0, 1)">);
      comment.setReplynum(</span>0<span style="color: rgba(0, 0, 0, 1)">);

      commentService.saveComment(comment);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 查询所有数据
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testFindAll() {
      List</span>&lt;Comment&gt; list =<span style="color: rgba(0, 0, 0, 1)"> commentService.findCommentList();
      System.out.println(list);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 测试根据id查询
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @Test
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testFindCommentById() {
      Comment comment </span>= commentService.findCommentById("1"<span style="color: rgba(0, 0, 0, 1)">);
      System.out.println(comment);
    }
}</span></pre>
</div>
<h4><strong>MongoTemplate 实现评论点赞</strong></h4>
<p><strong>  </strong>低效率的点赞代码: CommentService 新增 updateThumbup方法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 点赞-效率低
* </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateCommentThumbupToIncrementingOld(String id){
    Comment comment </span>=<span style="color: rgba(0, 0, 0, 1)"> commentDao.findById(id).get();
    comment.setLikenum(comment.getLikenum() </span>+ 1<span style="color: rgba(0, 0, 0, 1)">);
    commentDao.save(comment);
}</span></pre>
</div>
<p>  以上方法虽然实现起来比较简单,但是执行效率并不高,因为我们只需要将点赞数加 1 就可以了,没必要查询出所有字段修改后再更新所有字段。(蝴蝶效应)<br>  可以使用MongoTemplate类来实现对某列的操作。</p>
<p>  (1)修改CommentService</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注入MongoTemplate</span>
<span style="color: rgba(0, 0, 0, 1)">@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span> MongoTemplate mongoTemplate;</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 赞数+1
* </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id
* &nbsp;&nbsp;
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateCommentLikenum(String id) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查询对象</span>
    Query query = Query.query(Criteria.where("_id"<span style="color: rgba(0, 0, 0, 1)">).is(id));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 更新对象</span>
    Update update = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Update();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">局部更新,相当于$set
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> update.set(key,value)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 递增$inc
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> update.inc("likenum",1);</span>
    update.inc("likenum"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数1:查询对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数2:更新对象
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数3:集合的名字或实体类的类型Comment.class
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> mongoTemplate.updateFirst(query, update, "comment");</span>
    mongoTemplate.updateFirst(query, update, Comment.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<p>  (2)测试</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 点赞数+1
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testUpdateCommentLikenum() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对3号文档的点赞数+1</span>
    commentService.updateCommentLikenum("3"<span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<h4>&nbsp;SpringDataMongoDB 连接副本集</h4>
<p>  副本集语法:</p>
<p>    mongodb://host1,host2,host3/articledb?connect=replicaSet&amp;slaveOk=true&amp;replicaSet=副本集名字</p>
<p>    slaveOk=true :开启副本节点读的功能,可实现读写分离。<br>    connect=replicaSet :自动到副本集中选择读写的主机。如果slaveOK是打开的,则实现了读写分离<br>  修改配置文件application.yml:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">spring:
#数据源配置
data:
    mongodb:
      #主机地址
      #host: </span>192.168.43.182<span style="color: rgba(0, 0, 0, 1)">
      #数据库
      #database: articledb
      #默认端口是27017
      #port: </span>27017
<span style="background-color: rgba(255, 255, 0, 1)"><span style="color: rgba(0, 0, 0, 1)">      #副本集的连接字符串
      uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">192.168.43.182:27017,192.168.43.182:27018,192.168.43.182:27019/articledb?connect=replicaSet&amp;slaveOk=true&amp;replicaSet=myrs</span></span></pre>
</div>
<p>    注意:主机必须是副本集中所有的主机,包括主节点、副本节点、仲裁节点。<br>  完整的连接字符串的参考:</p>
<p>    MongoDB客户端连接语法</p>
<div class="cnblogs_code">
<pre>mongodb:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[?options]]</span></pre>
</div>
<p>    mongodb:// 这是固定的格式,必须要指定。</p>
<p>    username:password@ 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登陆这个数据库</p>
<p>    host1 必须的指定至少一个host, host1 是这个URI唯一要填写的。它指定了要连接服务器的地址。如果要连接副本集,请指定多个主机地址。</p>
<p>    portX 可选的指定端口,如果不填,默认为27017</p>
<p>    /database 如果指定username:password@,连接并验证登陆指定数据库。若不指定,默认打开test 数据库。</p>
<p>    ?options 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对name=value,键值对之间通过&amp;或;(分号)隔开</p>
<p>    选项(options):</p>
<p>      <img src="https://img2020.cnblogs.com/blog/1369049/202004/1369049-20200403164507616-694913588.png"></p>
<h4>&nbsp;SpringDataMongDB 连接分片集群:</h4>
<p>  其连接的是mongs路由,配置和单机mongod的配置是一样的。<br>  多个路由的时候的SpringDataMongoDB的客户端配置参考如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">spring:
#数据源配置
data:
    mongodb:
      #主机地址
      #host: </span>192.168.43.182<span style="color: rgba(0, 0, 0, 1)">
      #数据库
      #database: articledb
      #默认端口是27017
      #port: </span>27017<span style="color: rgba(0, 0, 0, 1)">
      #副本集的连接字符串
      #uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">192.168.43.182:27017,192.168.43.182:27018,192.168.43.182:27019/articledb?connect=replicaSet&amp;slaveOk=true&amp;replicaSet=myrs</span>
<span style="color: rgba(0, 0, 0, 1)">      #连接路由字符串
      uri: mongodb:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">192.168.43.182:27017,192.168.43.182:27117/articledb</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/roadlandscape/p/12622544.html
頁: [1]
查看完整版本: SpringBoot整合SpringData MongoDB