Alt text

(>﹏<)

        最近在做日志采集的工作中用到了MongoDB这款NoSQL数据库,相较于常见的RDBMS灵活且快速;而且非结构化的数据存储格式也极为适合一些应用场景,既节省内存又能提高读写速度。了解如何将MongoDB集成到SpringBoot这样一款非常流行的轻量、快速的开发框架很有必要。

创建Spring Boot工程并导入Maven依赖

注:示例代码使用的Spring Boot版本为:2.0.1.RELEASE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

修改配置文件 application.yml

1
2
3
4
5
6
spring:
data:
mongodb:
host: 192.168.109.128
port: 27017
database: test

创建Employee实体

注:Employee 映射 Document

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Document(collection = "employee") // 集合名
public class Employee {

@Id
private String id;

private String name;

private Double salary;

private Date birthday;

// 省略 getter/setter
}

创建MongoRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.orchoe.repository;

import com.orchoe.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
* @author Austin
* @date 2018/3/07 16:42
*/
@Repository
public class MongoRepository {

@Autowired
private MongoTemplate mongoTemplate;

public void insertOne(Employee employee){
mongoTemplate.insert(employee);
}

public void deleteOne(Employee employee){
mongoTemplate.remove(employee);
}

public User findOne(String id){
return mongoTemplate.findById(id,Employee.class);
}

public List<Employee> findAll(){
return mongoTemplate.findAll(Employee.class);
}

public List<Employee> findByName(String name){
return mongoTemplate.find(Query.query(Criteria.where("name").is(name)),Employee.class);
}
}

创建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.orchoe.test;

import com.orchoe.repository.MongoRepository;
import com.orchoe.entity.Employee;
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.Date;
import java.util.List;

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

@Autowired
private MongoRepository mongoRepository;

@Test
public void contextLoads() {
mongoRepository.insertOne(new Employee("zhangsan", 2000.0D, new Date()));
List<Employee> employees = mongoRepository.findAll();
employees.forEach(employee -> System.out.println(employee));
}
}