1. 程式人生 > 實用技巧 >崛起於Springboot2.X之Mongodb多資料來源處理(35)

崛起於Springboot2.X之Mongodb多資料來源處理(35)

>>> hot3.png

多資料來源:4個mongodb庫!

目錄結構圖:

d4e353524f89a87f0f78f2e958d7ec1d661.jpg

1、新增pom依賴

<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.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2、application.properties

spring.data.mongodb.first.database=node1
spring.data.mongodb.first.uri=localhost:27017

spring.data.mongodb.second.database=node2
spring.data.mongodb.second.uri=localhost:27017

spring.data.mongodb.third.database=node3
spring.data.mongodb.third.uri=localhost:27017

spring.data.mongodb.fourth.database=node4
spring.data.mongodb.fourth.uri=localhost:27017

3、配置類,一個主類,四個各自的mongodb配置類

@Configuration
public class MultipleMongoProperties {

    @Bean(name="firstMongoProperties")
    @Primary
    @ConfigurationProperties(prefix="spring.data.mongodb.first")
    public MongoProperties firstMongoProperties() {
        System.out.println("-------------------- statisMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="secondMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.second")
    public MongoProperties secondMongoProperties() {
        System.out.println("-------------------- listMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="thirdMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.third")
    public MongoProperties thirdMongoProperties() {
        System.out.println("-------------------- thirdMongoProperties init ---------------------");
        return new MongoProperties();
    }

    @Bean(name="fourthMongoProperties")
    @ConfigurationProperties(prefix="spring.data.mongodb.fourth")
    public MongoProperties fourthMongoProperties() {
        System.out.println("-------------------- fourthMongoProperties init ---------------------");
        return new MongoProperties();
    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.first", mongoTemplateRef = "firstMongo")
public class FirstMongoTemplate {

    @Autowired
    @Qualifier("firstMongoProperties")
    private MongoProperties mongoProperties;

    @Primary
    @Bean(name = "firstMongo")
    public MongoTemplate firstMongoTemplate() throws Exception {
        return new MongoTemplate(firstFactory(this.mongoProperties));
    }

    @Bean
    @Primary
    public MongoDbFactory firstFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.second", mongoTemplateRef = "secondMongo")
public class SecondMongoTemplate {
    @Autowired
    @Qualifier("secondMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "secondMongo")
    public MongoTemplate secondTemplate() throws Exception {
        return new MongoTemplate(secondFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory secondFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.third", mongoTemplateRef = "thirdMongo")
public class ThirdMongoTemplate {

    @Autowired
    @Qualifier("thirdMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "thirdMongo")
    public MongoTemplate thirdTemplate() throws Exception {
        return new MongoTemplate(thirdFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory thirdFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }
}
@Configuration
@EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.fourth", mongoTemplateRef = "fourthMongo")
public class FourthMongoTemplate {

    @Autowired
    @Qualifier("fourthMongoProperties")
    private MongoProperties mongoProperties;

    @Bean(name = "fourthMongo")
    public MongoTemplate fourthTemplate() throws Exception {
        return new MongoTemplate(fourthFactory(this.mongoProperties));
    }

    @Bean
    public MongoDbFactory fourthFactory(MongoProperties mongoProperties) throws Exception {

        ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());

        return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());

    }

}

4、dao層

看我剛剛的目錄結構圖:在dao下面建立四個first、second、third、fourth資料夾,對應下面的四個mongodb資料庫repository

public interface FirstRepository extends MongoRepository<People,String> {
}
public interface SecondRepository extends MongoRepository<People,String> {
}
public interface ThirdRepository extends MongoRepository<People,String> {
}
public interface FourthRepository extends MongoRepository<People,String> {
}

5、entity層

也是如上的操作,他們沒有在同一個資料夾下面

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "first_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "second_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "third_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "fourth_people")
public class People {

    @Id
    private String id;

    private String value;

    @Override
    public String toString() {
        return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''
                + '}';
    }
}

6、controller層

@Controller
@RequestMapping(value = "/mongodb")
public class TestController {

    @Autowired
    FirstRepository firstRepository;

    @Autowired
    SecondRepository secondRepository;

    @Autowired
    ThirdRepository thirdRepository;

    @Autowired
    FourthRepository fourthRepository;

    @GetMapping(value = "/test1")
    public void test1(){
        firstRepository.save(new com.dtb.mongodb.entity.first.People("1","第一個"));
    }

    @GetMapping(value = "/test2")
    public void test2(){
        secondRepository.save(new People("2","新增第二個資料"));
    }


    @GetMapping(value = "/test3")
    public void test3(){
        thirdRepository.save(new com.dtb.mongodb.entity.third.People("1","第三個"));
    }

    @GetMapping(value = "/test4")
    public void test4(){
        fourthRepository.save(new com.dtb.mongodb.entity.fourth.People("1","第四個"));
    }
}

7、新增成功!

8c82f5a5213cc61a070723aca5e8be78342.jpg

轉載於:https://my.oschina.net/mdxlcj/blog/1928794