spring boot springjpa 支持多個(gè)數(shù)據(jù)源的實(shí)例代碼
1.SpringBoot的程序啟動(dòng)類(lèi)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
//@EnableJpaRepositories(basePackages = "com.sonychina.backend.repository")
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
//SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
2.雙數(shù)據(jù)源配置類(lèi)
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import com.test.entity.statistic.SysUser;
import com.test.repository.system.SystemRepository;
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",
basePackageClasses= {SystemRepository.class})
public class GlobalDataConfiguration {
// @Autowired
// private DBConfig dbConfig;
@Autowired
private JpaProperties jpaProperties;
@Bean(name="primaryDataSource")
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
System.out.println("-------------------- primaryDataSource init ---------------------");
return DataSourceBuilder.create().build();
}
@Bean(name="secondaryDataSource")
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
System.out.println("-------------------- secondaryDataSource init ---------------------");
// DataSourceBuilder factory = DataSourceBuilder
// .create(DBConfig.class.getClassLoader())
// .driverClassName(dbConfig.getDriver())
// .url(dbConfig.getUrl())
// .username(dbConfig.getUser())
// .password(dbConfig.getPassword());
// return factory.build();
return DataSourceBuilder.create().build();
}
// @Bean(name = "entityManagerPrimary")
// @Primary
// public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
// return customerEntityManagerFactory(builder).getObject().createEntityManager();
// }
@Bean(name="entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(primaryDataSource())
.properties(getVendorProperties(primaryDataSource()))
.packages(SysUser.class)
.persistenceUnit("system")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
}
3.第二個(gè)jpa實(shí)體管理器
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.test.entity.manage.Banner;
import com.test.repository.manage.BannerRepository;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackageClasses= {BannerRepository.class})
public class SecondEMFBConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired@Qualifier("secondaryDataSource")
private DataSource dataSource;
// @Bean(name = "entityManagerPrimary")
// @Primary
// public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
// return customerEntityManagerFactory(builder).getObject().createEntityManager();
// }
@Bean(name="entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource)
.properties(getVendorProperties(dataSource))
.packages(Banner.class)
.persistenceUnit("customers")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(customerEntityManagerFactory(builder).getObject());
}
}
4.repository類(lèi)舉例
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import com.test.entity.manage.Banner;
public interface BannerRepository extends JpaRepository<Banner, Long> {
@Modifying
@Query("update Banner m set m.name=?1 where m.id=?2")
public void update(String bannerName, Long id);
}
1.5.注意:對(duì)@Primary修飾的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,spring上下文自動(dòng)使用默認(rèn)的JpaTransactionManager,但是對(duì)于第二個(gè)或第三個(gè)等等必須指定TransactionManager??梢詤⒖約pringboot官方文檔中的相關(guān)章節(jié)。
總結(jié)
以上所述是小編給大家介紹的spring boot springjpa 支持多個(gè)數(shù)據(jù)源的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
- springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫(kù)
- Spring boot中使用Spring-data-jpa方便快捷的訪問(wèn)數(shù)據(jù)庫(kù)(推薦)
- Spring boot jpa 刪除數(shù)據(jù)和事務(wù)管理的問(wèn)題實(shí)例詳解
- 詳解基于Spring Boot與Spring Data JPA的多數(shù)據(jù)源配置
- SpringBoot連接MYSQL數(shù)據(jù)庫(kù)并使用JPA進(jìn)行操作
- Spring Boot 添加MySQL數(shù)據(jù)庫(kù)及JPA實(shí)例
- Spring Boot中使用Spring-data-jpa實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪查改
- SpringBoot整合JPA數(shù)據(jù)源方法及配置解析
相關(guān)文章
Mybatis中的resultType和resultMap使用
這篇文章主要介紹了Mybatis中的resultType和resultMap使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09
使用maven的profile構(gòu)建不同環(huán)境配置的方法
這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
mybatis-puls中的resultMap數(shù)據(jù)映射
這篇文章主要介紹了mybatis-puls中的resultMap數(shù)據(jù)映射,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)電影院訂票系統(tǒng)代碼,代碼實(shí)現(xiàn)了界面類(lèi)登錄注冊(cè)類(lèi),用戶類(lèi)等,具有一定參考價(jià)值,需要的朋友可以參考下。2017-11-11
java8根據(jù)某一屬性過(guò)濾去重的實(shí)例
這篇文章主要介紹了java8根據(jù)某一屬性過(guò)濾去重的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
springboot中validator數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)
這篇文章主要介紹了springboot中validator數(shù)據(jù)校驗(yàn)功能,校驗(yàn)分為普通校驗(yàn)和分組校驗(yàn),每種校驗(yàn)方式通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Java(基于Struts2) 分頁(yè)實(shí)現(xiàn)代碼
這篇文章介紹了Java(基于Struts2) 分頁(yè)實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-10-10

