forked from ityouknow/spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
333 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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.neo</groupId> | ||
<artifactId>spring-boot-multi-mongodb</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-multi-mongodb</name> | ||
<description>Demo project for Spring Boot and multi mongodb</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
<version>RELEASE</version> | ||
</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> | ||
<configuration> | ||
<fork>true</fork> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
18 changes: 18 additions & 0 deletions
18
spring-boot-multi-mongodb/src/main/java/com/neo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.neo; | ||
|
||
import com.neo.config.props.MultipleMongoProperties; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@EnableConfigurationProperties(MultipleMongoProperties.class) | ||
@SpringBootApplication(exclude = MongoAutoConfiguration.class) | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
spring-boot-multi-mongodb/src/main/java/com/neo/config/MultipleMongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.neo.config; | ||
|
||
import com.mongodb.MongoClient; | ||
import com.neo.config.props.MultipleMongoProperties; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.mongo.MongoProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.mongodb.MongoDbFactory; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Configuration | ||
public class MultipleMongoConfig { | ||
|
||
@Autowired | ||
private MultipleMongoProperties mongoProperties; | ||
|
||
@Primary | ||
@Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE) | ||
public MongoTemplate primaryMongoTemplate() throws Exception { | ||
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary())); | ||
} | ||
|
||
@Bean | ||
@Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE) | ||
public MongoTemplate secondaryMongoTemplate() throws Exception { | ||
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary())); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception { | ||
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), | ||
mongo.getDatabase()); | ||
} | ||
|
||
@Bean | ||
public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception { | ||
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), | ||
mongo.getDatabase()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-boot-multi-mongodb/src/main/java/com/neo/config/PrimaryMongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Configuration | ||
@EnableMongoRepositories(basePackages = "com.neo.model.repository.primary", | ||
mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) | ||
public class PrimaryMongoConfig { | ||
|
||
protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-boot-multi-mongodb/src/main/java/com/neo/config/SecondaryMongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Configuration | ||
@EnableMongoRepositories(basePackages = "com.neo.model.repository.secondary", | ||
mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) | ||
public class SecondaryMongoConfig { | ||
|
||
protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; | ||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-multi-mongodb/src/main/java/com/neo/config/props/MultipleMongoProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.neo.config.props; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.autoconfigure.mongo.MongoProperties; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Data | ||
@ConfigurationProperties(prefix = "mongodb") | ||
public class MultipleMongoProperties { | ||
|
||
private MongoProperties primary = new MongoProperties(); | ||
private MongoProperties secondary = new MongoProperties(); | ||
} |
28 changes: 28 additions & 0 deletions
28
...boot-multi-mongodb/src/main/java/com/neo/model/repository/primary/PrimaryMongoObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.neo.model.repository.primary; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Document(collection = "first_mongo") | ||
public class PrimaryMongoObject { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String value; | ||
|
||
@Override | ||
public String toString() { | ||
return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' | ||
+ '}'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-boot-multi-mongodb/src/main/java/com/neo/model/repository/primary/PrimaryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.neo.model.repository.primary; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
public interface PrimaryRepository extends MongoRepository<PrimaryMongoObject, String> { | ||
} |
29 changes: 29 additions & 0 deletions
29
...-multi-mongodb/src/main/java/com/neo/model/repository/secondary/SecondaryMongoObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.neo.model.repository.secondary; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Document(collection = "second_mongo") | ||
public class SecondaryMongoObject { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String value; | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "SecondaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' | ||
+ '}'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...t-multi-mongodb/src/main/java/com/neo/model/repository/secondary/SecondaryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.neo.model.repository.secondary; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
public interface SecondaryRepository extends MongoRepository<SecondaryMongoObject, String> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mongodb: | ||
primary: | ||
host: 192.168.9.60 | ||
port: 20000 | ||
database: test | ||
secondary: | ||
host: 192.168.9.61 | ||
port: 20000 | ||
database: test1 |
17 changes: 17 additions & 0 deletions
17
spring-boot-multi-mongodb/src/test/java/com/neo/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.neo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class ApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
System.out.println("hello world"); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
spring-boot-multi-mongodb/src/test/java/com/neo/model/repository/MuliDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.neo.model.repository; | ||
|
||
import com.neo.model.repository.primary.PrimaryMongoObject; | ||
import com.neo.model.repository.primary.PrimaryRepository; | ||
import com.neo.model.repository.secondary.SecondaryMongoObject; | ||
import com.neo.model.repository.secondary.SecondaryRepository; | ||
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.List; | ||
|
||
/** | ||
* Created by neo on 2017/5/6. | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class MuliDatabaseTest { | ||
|
||
@Autowired | ||
private PrimaryRepository primaryRepository; | ||
|
||
@Autowired | ||
private SecondaryRepository secondaryRepository; | ||
|
||
@Test | ||
public void TestSave() { | ||
|
||
System.out.println("************************************************************"); | ||
System.out.println("测试开始"); | ||
System.out.println("************************************************************"); | ||
|
||
this.primaryRepository | ||
.save(new PrimaryMongoObject(null, "第一个库的对象")); | ||
|
||
this.secondaryRepository | ||
.save(new SecondaryMongoObject(null, "第二个库的对象")); | ||
|
||
List<PrimaryMongoObject> primaries = this.primaryRepository.findAll(); | ||
for (PrimaryMongoObject primary : primaries) { | ||
System.out.println(primary.toString()); | ||
} | ||
|
||
List<SecondaryMongoObject> secondaries = this.secondaryRepository.findAll(); | ||
|
||
for (SecondaryMongoObject secondary : secondaries) { | ||
System.out.println(secondary.toString()); | ||
} | ||
|
||
System.out.println("************************************************************"); | ||
System.out.println("测试完成"); | ||
System.out.println("************************************************************"); | ||
} | ||
|
||
} |