我的开发环境是eclipse,首先安装好springboot的插件,我参考的是这篇博客Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件,插件的下载地址是http://spring.io/tools3/sts/all

其次将mongodb安装好,解压版下载地址是http://dl.mongodb.org/dl/win32/x86_64

安装我参考的是这两篇博客

windows安装MongoDB3.4.7解压版本mongoDB(1):windows下安装mongoDB(解压缩版)

如果你想要有mongodb的可视化工具,可以从这个博客Navicat Premium 12**方法知道。

开始正餐

项目结构是

springboot集成mongoDB

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	


	<groupId>com.forezp</groupId>
	<artifactId>springboot-mongodb</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-mongodb</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

CustomerRepository.java

public interface CustomerRepository extends MongoRepository<Customer, String> {

	
}

Customer.java

package com.forezp.entity;

import org.springframework.data.annotation.Id;

/**
 * @author ligz
 */
public class Customer {

    @Id
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

CustomerController.java

package com.forezp.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.forezp.dao.CustomerRepository;
import com.forezp.entity.Customer;

/**
 * @author ligz
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private CustomerRepository repository;
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
    public boolean CreateCustomer(@RequestBody Customer customer) {
        repository.save(customer);
        return true;
    }
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public List<Customer> List() {
		List<Customer> list = repository.findAll();
		return list;
	}
}

application.properties

spring.data.mongodb.uri=mongodb://localhost:27017/mongoDB

启动主程序

package com.forezp;

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

@SpringBootApplication
public class SpringbootMongodbApplication{


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


}

用postman测试save方法可以插入数据

springboot集成mongoDB

springboot集成mongoDB

直接访问http://localhost:8080/customer/list可以看到数据库中的数据

springboot集成mongoDB

相关文章: