Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
關(guān)于
有時(shí)候,隨著業(yè)務(wù)的發(fā)展,項(xiàng)目關(guān)聯(lián)的數(shù)據(jù)來源會變得越來越復(fù)雜,使用的數(shù)據(jù)庫會比較分散,這個(gè)時(shí)候就會采用多數(shù)據(jù)源的方式來獲取數(shù)據(jù)。另外,多數(shù)據(jù)源也有其他好處,例如分布式數(shù)據(jù)庫的讀寫分離,集成多種數(shù)據(jù)庫等等。
下面分享我在實(shí)際項(xiàng)目中配置多數(shù)據(jù)源的案例。話不多說了,來一起看看詳細(xì)的介紹吧
步驟
1.application.yml文件中,配置數(shù)據(jù)庫源。這里primary是主庫,secondary是從庫。
server: port: 8089 # 多數(shù)據(jù)源配置 #primary spring: primary: datasource: url: jdbc:mysql://127.0.0.1:3306/database1?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai username: root password: ****** driver-class-name: com.mysql.jdbc.Driver #secondary secondary: datasource: url: jdbc:mysql://127.0.0.1:3306/database1?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai username: root password: ****** driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: primary-dialect: org.hibernate.dialect.MySQL5Dialect secondary-dialect: org.hibernate.dialect.MySQL5Dialect open-in-view: true show-sql: true
2.創(chuàng)建一個(gè)Spring配置類,其中spring.primary.datasource的路徑參考yml文件的配置。
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.secondary.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
3.分別創(chuàng)建主庫、從庫的配置類。
注意:entity包和dao包的配置,以及@Primary注解指定主庫。
主庫配置類:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.infinitus.yunxiao_data.dao.primary"}) //設(shè)置Repository所在位置
public class PrimaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
.packages("com.infinitus.yunxiao_data.entity.primary") //設(shè)置實(shí)體類所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
從庫的配置類:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySecondary",
transactionManagerRef = "transactionManagerSecondary",
basePackages = {"com.infinitus.yunxiao_data.dao.secondary"}) //設(shè)置Repository所在位置
public class SecondaryConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties(secondaryDataSource))
.packages("com.infinitus.yunxiao_data.entity.secondary") //設(shè)置實(shí)體類所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
4.分別創(chuàng)建主、從庫dao類。
主dao:
@Repository
public interface PrimaryRepository extends JpaRepository<PrimaryEntity, Long> {
@Query(value = "SELECT p FROM PrimaryEntity p")
List<PrimaryEntity> queryList();
}
從dao:
@Repository
public interface SecondaryRepository extends JpaRepository<SecondaryEntity, Long> {
@Query(value = "SELECT p FROM SecondaryEntity p")
List<SecondaryEntity> queryList();
}
5.分別創(chuàng)建主、從庫entity類。
主entity:
@Entity
@Table(name = "holiday_scheme")
@EntityListeners(AuditingEntityListener.class)
public class PrimaryEntity extends AbstractPersistable<Long> {
@Column(name = "date")
public String date;
@Column(name = "hour")
public String hour;
@Column(name = "holiday")
public String holiday;
@Column(name = "holiday_explain")
public String holiday_explain;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHour() {
return hour;
}
public void setHour(String hour) {
this.hour = hour;
}
public String getHoliday() {
return holiday;
}
public void setHoliday(String holiday) {
this.holiday = holiday;
}
public String getHoliday_explain() {
return holiday_explain;
}
public void setHoliday_explain(String holiday_explain) {
this.holiday_explain = holiday_explain;
}
@Override
public String toString() {
return "PrimaryEntity{" +
"date='" + date + '\'' +
", hour='" + hour + '\'' +
", holiday='" + holiday + '\'' +
", holiday_explain='" + holiday_explain + '\'' +
'}';
}
}
從entity:
@Entity
@Table(name = "active_dashboards")
@EntityListeners(AuditingEntityListener.class)
public class SecondaryEntity extends AbstractPersistable<Long> {
@Column(name = "dashboard_id")
public String dashboard_id;
@Column(name = "user_id")
public String user_id;
@Column(name = "order_index")
public String order_index;
public String getDashboard_id() {
return dashboard_id;
}
public void setDashboard_id(String dashboard_id) {
this.dashboard_id = dashboard_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getOrder_index() {
return order_index;
}
public void setOrder_index(String order_index) {
this.order_index = order_index;
}
@Override
public String toString() {
return "SecondaryEntity{" +
"dashboard_id='" + dashboard_id + '\'' +
", user_id='" + user_id + '\'' +
", order_index='" + order_index + '\'' +
'}';
}
}
6.controller請求獲取不同數(shù)據(jù)庫的數(shù)據(jù)。
@RestController
@RequestMapping("/database")
public class MailController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
PrimaryRepository primaryRepository;
@Autowired
SecondaryRepository secondaryRepository;
@RequestMapping("/primary")
@ResponseBody
public String primary() {
return primaryRepository.queryList().toString();
}
@RequestMapping("/secondary")
@ResponseBody
public String secondary() {
return secondaryRepository.queryList().toString();
}
}
注意
下面提兩個(gè)在配置多數(shù)據(jù)源時(shí)遇到的坑點(diǎn),一不注意就掉坑了。
1.Application類不需要配置@EnableJpaRepositories注解,會報(bào)如下錯誤。
A component required a bean named 'entityManagerFactory' that could not be f
2.注意檢查dao類,獲取數(shù)據(jù)的方法上格式是否正確,有沒有某個(gè)字段是表中不存在的,避免啟動異常。如下,SecondaryEntity表中是不存在job_name字段的,所以注釋掉才能啟動成功等。
//@Query(value = "SELECT p FROM SecondaryEntity p where p.job_name = ?1") //List<SecondaryEntity> queryOdcRecord(String job_name);
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java隨機(jī)生成驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹Java隨機(jī)生成驗(yàn)證碼的實(shí)現(xiàn)方法,文中有相關(guān)的實(shí)現(xiàn)代碼供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08
CMD運(yùn)行Intellij Idea編譯后的class文件操作
這篇文章主要介紹了CMD運(yùn)行Intellij Idea編譯后的class文件操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段)
本篇文章主要介紹了Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
logback的FileAppender文件追加模式和沖突檢測解讀
這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
java實(shí)現(xiàn)對excel文件的處理合并單元格的操作
這篇文章主要介紹了java實(shí)現(xiàn)對excel文件的處理合并單元格的操作,開頭給大家介紹了依賴引入代碼,表格操作的核心代碼,代碼超級簡單,需要的朋友可以參考下2021-07-07
Springboot+Thymeleaf+Jpa實(shí)現(xiàn)登錄功能(附源碼)
最近有學(xué)習(xí)到關(guān)于Springboot+Thymeleaf+Jpa的綜合運(yùn)用知識,因此想寫一個(gè)簡單的登錄界面來嘗試一下,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
springboot+jwt實(shí)現(xiàn)token登陸權(quán)限認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了springboot+jwt實(shí)現(xiàn)token登陸權(quán)限認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

