Skip to content

Commit aa27767

Browse files
committed
Spring Boot中增强对MongoDB的配置(连接池等)
1 parent abc8dfd commit aa27767

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

Chapter3-2-11/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter3-2-11</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter3-2-11</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.spring4all</groupId>
45+
<artifactId>mongodb-plus-spring-boot-starter</artifactId>
46+
<version>1.0.0.RELEASE</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<version>1.16.12</version>
53+
<scope>provided</scope>
54+
</dependency>
55+
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
68+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import com.spring4all.mongodb.EnableMongoPlus;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@EnableMongoPlus
8+
@SpringBootApplication
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.data.mongodb.uri=mongodb://localhost:27017/test
2+
3+
spring.data.mongodb.option.min-connection-per-host=20
4+
spring.data.mongodb.option.max-connection-per-host=200
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.didispace;
2+
3+
import com.mongodb.MongoClient;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
12+
@RunWith(SpringRunner.class)
13+
@SpringBootTest
14+
@Slf4j
15+
public class ApplicationTests {
16+
17+
@Autowired
18+
private MongoClient mongoClient;
19+
20+
@Test
21+
public void test() throws Exception {
22+
log.info("MinConnectionsPerHost = {}, MaxConnectionsPerHost = {}",
23+
mongoClient.getMongoClientOptions().getMinConnectionsPerHost(),
24+
mongoClient.getMongoClientOptions().getConnectionsPerHost());
25+
}
26+
27+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#### 工程配置
4141

4242
- chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/)
43-
- chapter2-2-1:[配置文件详解:自定义属性、随机数、多环境配置等](http://blog.didispace.com/springbootproperties/)
4443

4544
#### Web开发
4645

@@ -63,6 +62,7 @@
6362
- chapter3-2-8:[MyBatis注解配置详解](http://blog.didispace.com/mybatisinfo/)
6463
- chapter3-2-9:[使用Flyway来管理数据库版本](http://blog.didispace.com/spring-boot-flyway-db-version/)
6564
- chapter3-2-10:[使用LDAP来统一管理用户信息](http://blog.didispace.com/spring-boot-ldap-user/)
65+
- chapter3-2-11:[Spring Boot中增强对MongoDB的配置(连接池等)](http://blog.didispace.com/springbootmongodb-plus/)
6666

6767
#### 事务管理
6868

0 commit comments

Comments
 (0)