文档章节

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

木九天
 木九天
发布于 2018/08/16 11:31
字数 675
阅读 334
收藏 0

多数据源:4个mongodb库!

目录结构图:

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

 

 

© 著作权归作者所有

共有 人打赏支持
木九天

木九天

粉丝 110
博文 195
码字总数 129486
作品 0
海淀
程序员
私信 提问
崛起于Springboot2.X之多节点mongodb实现区块链交易记录(36)

序言:根据前两篇崛起于Springboot2.X之区块链单节点mysql实现交易记录和崛起于Springboot2.X之Mongodb多数据源处理,进行整合,实现区块链多节点mongodb实现交易记录方案。直接上代码了,不...

木九天
2018/08/23
0
0
。W/mongodb-weadmin

Mongodb-WeAdmin 项目介绍 Mongodb网页管理工具,基于Spring Boot2.0,前端采用layerUI实现。 源于线上环境部署mongodb时屏蔽了外网访问mongodb,所以使用不了mongochef这样方便的远程连接工具...

。W
2018/11/22
0
0
Oracle 的 NoSQL 恶梦 MongoDB

Oracle的NoSQL恶梦MongoDB (暨MongoDB发布2.6版本之际) 这次发布描绘出了一个极具前景的公司或将引领未来超过十年的创新。 By Jack Clark, 8 Apr 2014 翻译:无若 快速崛起的数据库MongoDB...

溪边九节
2014/04/09
10.5K
40
MongoDB 4.0 RC 版本强势登陆

MongoDB 因其灵活的文档模型、可扩展分布式设计广受开发者喜爱,在此基础上,MongoDB 4.0 推出了更强大的功能支持,目前4.0第一个RC版本已经发布,本文将介绍 MongoDB 4.0 核心的一些新特性。...

张友东
2018/05/30
0
0
Yum安装MongoDB及数据库管理

MongoDB简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB旨在为WEB应用提供可扩展的高性能数据...

cchenyz
2018/07/13
0
0

没有更多内容

加载失败,请刷新页面

加载更多

composer安装

前言:随着开源的东西越来越多,一些好的代码我们是可以直接拿过来用的,github更是加快了这一节奏,在github上我们可以看到一些开源的项目、代码块、函数库、类结构等,我们可以直接Fork,然...

echojson
21分钟前
0
0
Longest Palindrome(leetcode409)

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for exa......

woshixin
39分钟前
0
0
mysql grant 用户权限总结

用户权限管理主要有以下作用: 1. 可以限制用户访问哪些库、哪些表 2. 可以限制用户对哪些表执行SELECT、CREATE、DELETE、DELETE、ALTER等操作 3. 可以限制用户登录的IP或域名 4. 可以限制用...

Airship
55分钟前
1
0
RabbitMQ学习(3)

1. 消费端 1. 消费端通过推模式或者拉模式从RabbitMQ中获取并消费消息,当消费者确认处理消息后,可以手动确认消息已被接收,然后就会将该消息从RabbitMQ的队列中标记再清除,消费者端还可以...

江左煤郎
今天
1
0
linux mysql(5.7)开启慢查询

一、有3个配置需要设置, 1:相关开关 2:日志目录文件 3:慢查询的时间限制 二、设置完之后重启mysql service mariadb restart 三、重启后做个测试 连接mysql 并查询: select sleep(6); 四...

chro008
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部