0
点赞
收藏
分享

微信扫一扫

springboot集成mongodb的demo样例

時小白 2022-07-13 阅读 171


文章目录

  • ​​1.首先创建一个maven项目:demo-mongodb​​
  • ​​2.pom.xml中引入相关的jar包​​
  • ​​2.1.由于该项目需要提供controller接口因此引入web的jar包​​
  • ​​2.2.因为项目是springboot集成mongodb因此需要引入mongodb相关包​​
  • ​​2.3.另外,为了简化对象的get set方法,引入lombok包​​
  • ​​2.4.springboot项目必须引入spring-boot-starter-parent​​
  • ​​3.重要类​​
  • ​​3.1.启动类的创建​​
  • ​​3.2.创建对象类​​
  • ​​3.3.创建访问入口的HelloController​​
  • ​​3.4.配置文件​​
  • ​​3.5.备注:这里假设你已经安装成功mongodb​​
  • ​​3.6.验证​​
  • ​​3.6.1.页面请求​​
  • ​​3.6.2.mongodb变化​​
  • ​​4.源代码地址​​

1.首先创建一个maven项目:demo-mongodb

springboot集成mongodb的demo样例_jar包

2.pom.xml中引入相关的jar包

2.1.由于该项目需要提供controller接口因此引入web的jar包

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

2.2.因为项目是springboot集成mongodb因此需要引入mongodb相关包

<!--mongodb集成-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2.3.另外,为了简化对象的get set方法,引入lombok包

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

lombok 说明参考


2.4.springboot项目必须引入spring-boot-starter-parent

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>

3.重要类

3.1.启动类的创建

package com.gaoxinfu.demo.mongodb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-22 16:33
*/
@SpringBootApplication
public class DemoMongodbApp {

public static void main(String[] args) {
SpringApplication.run(DemoMongodbApp.class,args);
}
}

3.2.创建对象类

package com.gaoxinfu.demo.mongodb;

import lombok.Data;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-22 16:54
*/

@Data
public class Person {

private String id;

private String name;

private Integer age;

private String sex;

public Person(String id, String name, Integer age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}

@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}

3.3.创建访问入口的HelloController

package com.gaoxinfu.demo.mongodb;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-22 16:36
*/
@RestController
public class HelloController {

@Resource
private MongoOperations mongoOperations;

@RequestMapping(value = "hello/{name}",method = RequestMethod.GET)
public String hello(@PathVariable String name){

Person person=new Person("1",name,18,"man");
return "HelloController.hello ="+name;
}

@RequestMapping(value = "hello/add",method = RequestMethod.GET)
public String addPerson(){
Person person=new Person("1","gaoxinfu",18,"man");
// return "HelloController.hello ="+name;
mongoOperations.save(person);
return "保存成功";
}
}

3.4.配置文件

server.port=8080
spring.data.mongodb.uri=mongodb://localhost:27017/test

3.5.备注:这里假设你已经安装成功mongodb

3.6.验证

3.6.1.页面请求

springboot集成mongodb的demo样例_jar包_02

3.6.2.mongodb变化

springboot集成mongodb的demo样例_spring_03

4.源代码地址

https://gitee.com/gaoxinfu_admin/demo-mongodb


举报

相关推荐

0 条评论