MongoDB之SpringBoot操作篇

                <svg xmlns="http://www.w3.org/2000/svg" style="display: none">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0)"></path>
                </svg>
                <p>本篇博客主讲如何使用SpringBoot操作MongoDB。</p> 

SpringBoot 操作 MongoDB 实现增删改查

(1)pom.xml 引入依赖

   <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-mongodbartifactId>
        dependency>

(2)创建 application.yml

spring:
  data:
    mongodb:
      host: 192.168.72.129
      database: studentdb

(3)创建实体类
创建包 com.changan.mongodb,包下建包 pojo 用于存放实体类,创建实体类

package com.changan.mongdb.pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

@Document(collection = "student")
public class Student implements Serializable {

<span class="hljs-meta">@Id</span>
<span class="hljs-keyword">private</span> <span class="hljs-title class_">Long</span> id;

<span class="hljs-keyword">private</span> <span class="hljs-title class_">String</span> name;

<span class="hljs-keyword">private</span> <span class="hljs-title class_">String</span> sex;

<span class="hljs-keyword">private</span> <span class="hljs-title class_">String</span> age;

<span class="hljs-keyword">private</span> <span class="hljs-title class_">String</span> introduce;

<span class="hljs-keyword">public</span> <span class="hljs-title class_">String</span> <span class="hljs-title function_">getSex</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">return</span> sex;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">setSex</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> sex</span>) {
    <span class="hljs-variable language_">this</span>.<span class="hljs-property">sex</span> = sex;
}

<span class="hljs-keyword">public</span> <span class="hljs-title class_">String</span> <span class="hljs-title function_">getAge</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">return</span> age;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">setAge</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> age</span>) {
    <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age;
}

<span class="hljs-keyword">public</span> <span class="hljs-title class_">String</span> <span class="hljs-title function_">getIntroduce</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">return</span> introduce;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">setIntroduce</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> introduce</span>) {
    <span class="hljs-variable language_">this</span>.<span class="hljs-property">introduce</span> = introduce;
}

<span class="hljs-keyword">public</span> <span class="hljs-title class_">String</span> <span class="hljs-title function_">getName</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">return</span> name;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">setName</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> name</span>) {
    <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name;
}

<span class="hljs-keyword">public</span> <span class="hljs-title class_">Long</span> <span class="hljs-title function_">getId</span>(<span class="hljs-params"></span>) {
    <span class="hljs-keyword">return</span> id;
}

<span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">setId</span>(<span class="hljs-params">Long id</span>) {
    <span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span> = id;
}

}

(4)创建数据访问接口
com.changan.mongodb 包下创建 dao 包,包下创建接口

package com.changan.mongdb.dao;

import com.changan.mongdb.pojo.Student;

import java.util.List;
import java.util.Map;

public interface StudentDao {

<span class="hljs-keyword">void</span> <span class="hljs-title function_">save</span><span class="hljs-params">(Student student)</span>;

<span class="hljs-keyword">void</span> <span class="hljs-title function_">update</span><span class="hljs-params">(Student student)</span>;

List<Student> <span class="hljs-title function_">findAll</span><span class="hljs-params">()</span>;

<span class="hljs-keyword">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(Integer id)</span>;

}

(5)创建业务逻辑类
com.changan.mongodb 包下创建 impl 包,包下创建类

package com.changan.mongdb.dao.impl;

import com.changan.mongdb.dao.StudentDao;
import com.changan.mongdb.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
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.Component;

import java.util.List;

@Component
public class StudentDaoImpl implements StudentDao {

<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> MongoTemplate mongoTemplate;

<span class="hljs-comment">/**
 * 新增信息
 * <span class="hljs-doctag">@param</span> student
 */</span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">save</span><span class="hljs-params">(Student student)</span> {
    mongoTemplate.save(student);
}

<span class="hljs-comment">/**
 * 修改信息
 * <span class="hljs-doctag">@param</span> student
 */</span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">update</span><span class="hljs-params">(Student student)</span> {
    <span class="hljs-comment">//修改的条件</span>
    <span class="hljs-type">Query</span> <span class="hljs-variable">query</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Query</span>(Criteria.where(<span class="hljs-string">"id"</span>).is(student.getId()));

    <span class="hljs-comment">//修改的内容</span>
    <span class="hljs-type">Update</span> <span class="hljs-variable">update</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Update</span>();
    update.set(<span class="hljs-string">"name"</span>,student.getName());

    mongoTemplate.updateFirst(query,update,Student.class);
}

<span class="hljs-comment">/**
 * 查询所有信息
 * <span class="hljs-doctag">@return</span>
 */</span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> List<Student> <span class="hljs-title function_">findAll</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> mongoTemplate.findAll(Student.class);
}

<span class="hljs-comment">/**
 * 根据id查询所有信息
 * <span class="hljs-doctag">@param</span> id
 */</span>
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(Integer id)</span> {
    <span class="hljs-type">Student</span> <span class="hljs-variable">byId</span> <span class="hljs-operator">=</span> mongoTemplate.findById(<span class="hljs-number">1</span>,Student.class);
    mongoTemplate.remove(byId);
}

}

(6)创建测试类

package com.changan.mongdb;

import com.changan.mongdb.dao.StudentDao;
import com.changan.mongdb.pojo.Student;
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 java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongdbApplicationTests {

<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> StudentDao studentDao;

<span class="hljs-comment">/**
 * 查询所有信息
 */</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">findAll</span><span class="hljs-params">()</span> {
    List<Student> all = studentDao.findAll();
    System.out.println(all.size());
}

<span class="hljs-comment">/**
 * 新增信息
 */</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">save</span><span class="hljs-params">()</span> {
    <span class="hljs-type">Student</span> <span class="hljs-variable">student</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>();
    student.setId(<span class="hljs-number">6l</span>);
    student.setName(<span class="hljs-string">"宋人头"</span>);
    studentDao.save(student);
}

<span class="hljs-comment">/**
 * 修改信息
 */</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">update</span><span class="hljs-params">()</span> {
    <span class="hljs-type">Student</span> <span class="hljs-variable">student</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>();
    student.setId(<span class="hljs-number">2l</span>);
    student.setName(<span class="hljs-string">"吴很帅"</span>);
    studentDao.update(student);
}

<span class="hljs-comment">/**
 * 删除信息
 */</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">()</span> {
    studentDao.delete(<span class="hljs-number">3</span>);
}

}

在这里插入图片描述