springboot配置多数据源

springboot配置多数据源

springboot版本:1.5.9.RELEASE

1、jar包依赖文件pom

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
</dependencies>

2、属性配置文件

#数据源 school
datasource.school.url=jdbc:mysql://ip:3306/school?useUnicode=true&useSSL=true
datasource.school.username=root
datasource.school.password=123456
datasource.school.driverClassName=com.mysql.jdbc.Driver

#数据源 social
datasource.social.url=jdbc:mysql://ip:3306/social?useUnicode=true&useSSL=true
datasource.social.username=root
datasource.social.password=123456
datasource.social.driverClassName=com.mysql.jdbc.Driver

3、多数据school源配置

1)数据源school配置
/**
 * school数据源
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySchool",
transactionManagerRef = "transactionManagerSchool",
basePackages = {"com.wit.ctw.datasourcemultiple.web.repository.school" }) // 设置Repository所在位置
public class SchoolDataSource {

	@Autowired
	@Qualifier("school")
	private DataSource schoolDataSource;

	@Resource
	private JpaProperties jpaProperties;

	@Bean(name = "entityManagerSchool")
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
		return entityManagerFactorySchool(builder).getObject().createEntityManager();
	}

	@Bean(name = "entityManagerFactorySchool")
	public LocalContainerEntityManagerFactoryBean entityManagerFactorySchool(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(schoolDataSource).properties(getVendorProperties(schoolDataSource))
				// 设置实体类所在位置
				.packages("com.wit.ctw.datasourcemultiple.web.entity.school")
				//可用schoolPersistenceUnit进行自定义sql查询
				.persistenceUnit("schoolPersistenceUnit").build();
	}

	private Map<String, String> getVendorProperties(DataSource dataSource) {
		return jpaProperties.getHibernateProperties(dataSource);
	}

	@Bean(name = "transactionManagerSchool")
	public PlatformTransactionManager transactionManagerSchool(EntityManagerFactoryBuilder builder) {
		return new JpaTransactionManager(entityManagerFactorySchool(builder).getObject());
	}
}
2)将数据源school注入到容器中
/**
 * school数据眼部配置
 */
@Configuration
public class SchoolDataSourceConfig {

	/**
	 *
	 * school 数据源名称不要太长、太长会导致数据源初始化失败
	 */
	@Qualifier("school")
	@Bean(name = "school")
	//根据文件前缀,从属性文件中加载数据库配置
	@ConfigurationProperties(prefix = "datasource.school")
	public DataSource auditDataSource() {
		return DataSourceBuilder.create().build();
	}
}

4 多数据social源配置

1) 数据源social配置
/**
 *
 * social数据源
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySocial",
transactionManagerRef = "transactionManagerSocial",
// 设置Repository所在位置
basePackages = {"com.wit.ctw.datasourcemultiple.web.repository.social" })
public class SocialDataSource {
	
	@Resource
	private JpaProperties jpaProperties;

	@Autowired
	@Qualifier("social")
	private DataSource socialDataSource;

	@Primary
	@Bean(name = "entityManagerSocial")
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
		return entityManagerFactorySocial(builder).getObject().createEntityManager();
	}

	@Primary
	@Bean(name = "entityManagerFactorySocial")
	public LocalContainerEntityManagerFactoryBean entityManagerFactorySocial(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(socialDataSource).properties(getVendorProperties(socialDataSource))
				// 设置实体类所在位置
				.packages("com.wit.ctw.datasourcemultiple.web.entity.social")
				//可用socialPersistenceUnit进行自定义sql查询
				.persistenceUnit("socialPersistenceUnit").build();
	}

	private Map<String, String> getVendorProperties(DataSource dataSource) {
		return jpaProperties.getHibernateProperties(dataSource);
	}

	@Primary
	@Bean(name = "transactionManagerSocial")
	public PlatformTransactionManager transactionFactorySocial(EntityManagerFactoryBuilder builder) {
		return new JpaTransactionManager(entityManagerFactorySocial(builder).getObject());
	}
}
2)将数据源social注入到容器中
/**
 * social数据源配置
 */
@Configuration
public class SocialDataSourceConfig {

	/**
	 * 基础数据源、默认数据源
	 * 数据源名称不要太长、太长会导致数据源初始化失败
	 * @return
	 */
	@Primary
	@Qualifier("social")
	@Bean(name = "social")
	//根据文件前缀,从属性文件中加载数据库配置
	@ConfigurationProperties(prefix = "datasource.social")
	public DataSource dataSource() {
		DataSource dataSource = DataSourceBuilder.create().build();
		return dataSource;
	}
}
5、spring data jpa 自定义sql查询

可通过以下方法,注入对应的EntityManager,进行自定义sql查询。使用EntityManager可以进行自定义查询,可以进行jpa查询。

@PersistenceContext(unitName = "schoolPersistenceUnit")
EntityManager school;

@PersistenceContext(unitName = "socialPersistenceUnit")
EntityManager social;

github代码

注:多数据源和动态数据源是不同的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值