当前位置:   article > 正文

SpringBoot 1.5 JPA MongoDB 设置多数据源_springboot 1.5 + jpa+多数据源

springboot 1.5 + jpa+多数据源

SpringBoot 1.5 JPA MongoDB 设置多数据源

背景

某些特定场合,需要分库查询。
如:敏感数据分库存储,日志数据分库存储等。

1.环境依赖

基于Springboot 1.5.6.RELEASE

<dependency>         
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver</artifactId>
  <version>3.6.4</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.配置文件

server:
  port: 8081
spring:
  data:
    mongodb:
      primary:
        uri: mongodb://root:123root@127.0.0.1:27017/ltz1
      secondary:
        uri: mongodb://root2:123root@127.0.0.1:27017/ltz2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.Config配置

①.第一数据源配置(primary)

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * @author litong
 * @date 2020/1/19 14:40
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.ltz.demo.primary.dao",
		mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {

	@Bean
	@Primary
	@ConfigurationProperties(prefix = "spring.data.mongodb.primary")
	public MongoProperties primaryMongoProperties() {
		return new MongoProperties();
	}

	@Primary
	@Bean(name = "primaryMongoTemplate")
	public MongoTemplate primaryMongoTemplate() throws Exception {
		return new MongoTemplate(primaryFactory(primaryMongoProperties()));
	}

	@Bean
	@Primary
	public MongoDbFactory primaryFactory(MongoProperties mongoProperties) throws Exception {
		return new SimpleMongoDbFactory(new MongoClientURI(primaryMongoProperties().getUri()));
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

①.第二数据源配置(secondary)

import com.mongodb.MongoClientURI;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * @author litong
 * @date 2020/1/19 14:40
 */
@Configuration
@EnableMongoRepositories(basePackages = "com.ltz.demo.secondary.dao",
		mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {

	@Bean
	@ConfigurationProperties(prefix="spring.data.mongodb.secondary")
	public MongoProperties secondaryMongoProperties() {
		return new MongoProperties();
	}

	@Bean(name = "secondaryMongoTemplate")
	public MongoTemplate secondaryMongoTemplate() throws Exception {
		return new MongoTemplate(secondaryFactory(secondaryMongoProperties()));
	}

	@Bean
	public MongoDbFactory secondaryFactory(MongoProperties mongoProperties) throws Exception {
		return new SimpleMongoDbFactory(new MongoClientURI(secondaryMongoProperties().getUri()));
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

3.特别注意:

①.此处的primarysecondary只是示例,可自行修改名称,但是记得要一并修改Bean的名称。

②.com.ltz.demo.primary.daocom.ltz.demo.secondary.dao两个包名,也可自行修改,然后在包下建立对应的DAO(Repository)。即可实现在该数据源下的JPA操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/55970
推荐阅读
相关标签
  

闽ICP备14008679号