这篇文章主要介绍springboot-mongodb如何实现多数据源配置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
在日常工作中,我们可能需要连接多个MongoDB数据源,比如用户库user,日志库log。本章我们来记录连接多个数据源的步骤,以两个数据源为例,多个数据源类推。
1、pom.xml中引入mongodb的依赖
<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>
Lombok - 是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。简单试了以下这个工具还挺好玩的,加上注解我们就不用手动写 getter\setter、构建方式类似的代码了。
2, yml配置:
mongodb: primary: host: 192.168.9.60 port: 20000 database: test secondary: host: 192.168.9.60 port: 20000 database: test1
3, 配置2个库的数据源:
@Data @ConfigurationProperties(prefix = "mongodb") public class MultipleMongoProperties { private MongoProperties primary = new MongoProperties(); private MongoProperties secondary = new MongoProperties(); }
3.1) 第一个库封装:
@Configuration @EnableMongoRepositories(basePackages = "com.neo.model.repository.primary", mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) public class PrimaryMongoConfig { protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; }
3.2) 第二个库封装 :
@Configuration @EnableMongoRepositories(basePackages = "com.neo.model.repository.secondary", mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) public class SecondaryMongoConfig { protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; }
3.3) mongoTemplate:
@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()); } }
4, 创建2个库对应的repository:
@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 + '\'' + '}'; } }
public interface PrimaryRepository extends MongoRepository<PrimaryMongoObject, String> { }
测试:
@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("************************************************************"); } }
以上是“springboot-mongodb如何实现多数据源配置”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。