崛起于Springboot2.X之Mongodb多数据源处理(35)

多数据源:4个mongodb库!

目录结构图:

崛起于Springboot2.X之Mongodb多数据源处理(35)

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、添加成功!

崛起于Springboot2.X之Mongodb多数据源处理(35)

 

 

转载于:https://my.oschina.net/mdxlcj/blog/1928794