(简洁版)SpringBoot整合MongoDB(附demo)

SpringBoot整合MongoDB

MOngoDB:非关系型数据库(NoSql),数据以文档形式存在。BSON(document)

目录
在这里插入图片描述

第一步:启动MongoDB服务:
在这里插入图片描述

第二步:开启MongDB可视化工具:
在这里插入图片描述

第三步:导入依赖jar包

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--p6spy的依赖-->
        <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.1</version>
        </dependency>

        <!--druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <!-- MongDB依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <!-- java操作db驱动 -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>

    </dependencies>

第四步:aplication

#端口号:
server.port=8080
#数据库连接
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/text?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=111111
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#数据库同步代码
spring.jpa.hibernate.ddl-auto=update
#显示SQL语句
spring.jpa.show-sql=true


#配置MongoDB
spring.data.mongodb.uri=mongodb://localhost:27017/test

第五步:实体类Student

package springboot_mongodb.entity;

import java.io.Serializable;

public class Student implements Serializable {
    private String stuName;
    private Integer age;

    public Student() {
    }

    public Student(String stuName, Integer age) {
        this.stuName = stuName;
        this.age = age;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

第六步:控制层

package springboot_mongodb.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot_mongodb.entity.Student;

@Controller
public class StudentController {
    @Autowired
    private MongoOperations mongoOperations;
    @RequestMapping("/addStudent")
    @ResponseBody
    public String addStudent(){
         Student student = new Student("王二麻子",12);
         mongoOperations.save(student);
         return "SUCCEED";
    }

}

启动:
在这里插入图片描述

查看MongoDB可视化工具,成功
在这里插入图片描述

最后附上demo地址:

SpringBootMongoDB项目地址

猜你喜欢

转载自blog.csdn.net/weixin_43538859/article/details/86002197