赞
踩
一)在pom.xml文件中引入MongoDB需要的jar
- <?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.oysept.springboot</groupId>
- <artifactId>oysept-springboot-mongodb</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>oysept-springboot-mongodb</name>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.0.RELEASE</version>
- <relativePath/>
- </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</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- mongodb -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>

二)在application.properties中添加MongoDB配置
- server.port=8080
-
- spring.data.mongodb.uri=mongodb://localhost:27017/newdb
- #spring.data.mongodb.uri=mongodb://name:password@localhost:27017/newdb
三)添加一个MongoDB中的Entity类
- package com.oysept.springboot.entity;
-
- import org.springframework.data.annotation.Id;
- import org.springframework.data.mongodb.core.mapping.Document;
-
- /**
- * 实体类,默认命名customerEntity,也可以指定命名@Document(collection="customer")
- * @author ouyangjun
- */
- @Document(collection="customer")
- public class CustomerEntity {
-
- @Id
- private String id;
- private String firstName;
- private String lastName;
-
- public CustomerEntity() {}
-
- public CustomerEntity (String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
-
- public String getId() {return id;}
- public void setId(String id) {this.id = id;}
-
- public String getFirstName() {return firstName;}
- public void setFirstName(String firstName) {this.firstName = firstName;}
-
- public String getLastName() {return lastName;}
- public void setLastName(String lastName) {this.lastName = lastName;}
- }

四)添加一个MongoDB接口Repository操作工具类
- package com.oysept.springboot.repository;
-
- import java.util.List;
-
- import org.springframework.data.mongodb.repository.MongoRepository;
-
- import com.oysept.springboot.entity.CustomerEntity;
-
- /**
- * mongodb自定义查询
- * @author ouyangjun
- */
- public interface CustomerRepository extends MongoRepository<CustomerEntity, String> {
-
- public CustomerEntity findByFirstName(String firstName);
-
- public List<CustomerEntity> findByLastName(String lastName);
- }

五)新增一个MongoDB main方法测试工具类
- package com.oysept.springboot.test;
-
- 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 com.oysept.springboot.entity.CustomerEntity;
- import com.oysept.springboot.repository.CustomerRepository;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class MongoDBTest {
-
- @Autowired
- private CustomerRepository repository;
-
- @Test
- public void addMongoDB() {
- // 删除
- repository.deleteAll();
-
- // 添加
- repository.save(new CustomerEntity("ouyangjun", "oysept"));
- repository.save(new CustomerEntity("liujie", "oysept"));
-
- // 查询
- System.out.println("查询-------------------------------");
- for (CustomerEntity entity : repository.findAll()) {
- System.out.println("firstName: "+entity.getFirstName()+",\t lastName: "+entity.getLastName());
- }
-
- // 根据firstName查询
- System.out.println("\n根据firstName查询--------------------------------");
- CustomerEntity et = repository.findByFirstName("ouyangjun");
- System.out.println("firstName: "+et.getFirstName()+",\t lastName: "+et.getLastName());
-
- // 根据lastName查询
- System.out.println("\n根据lastName查询--------------------------------");
- for (CustomerEntity entity : repository.findByLastName("oysept")) {
- System.out.println("firstName: "+entity.getFirstName()+",\t lastName: "+entity.getLastName());
- }
- }
- }

六)添加一个springboot启动类,并展示项目结构图
- package com.oysept.springboot;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * springboot启动类
- * @author ouyangjun
- */
- @SpringBootApplication
- public class MongoDBApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(MongoDBApplication.class, args);
- }
- }

项目结构图:

最后启动MongoDBApplication类,可用main方法测试类测试。
识别二维码关注个人微信公众号

本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。