Springboot-Jpa多數據庫配置-2.0+版本

pom.xml增加:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

配置表同JdbcTemplate配置.

主數據源:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.example.demo.p"})
public class PrimaryConfig {
@Bean(name = "primaryDataSource")
@Primary
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDatasource;

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

//@Autowired
//private JpaProperties jpaProperties;

//private Map<String, Object> getVendorProperties() {
// return jpaProperties.getHibernateProperties(new HibernateSettings());//與1.5版本不同,注意.
//}

@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder.
dataSource(primaryDatasource)
//.properties(getVendorProperties())
.packages("com.example.demo.p")
.persistenceUnit("primaryPersistenceUnit")
.build();
}

@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}

主Entity:

@Entity
@Table(name = "xx")
public class Dtl {
@Id
@Column(name = "id")
private Long id;

@Column(name = "TICKER_SYMBOL")
private String tickerSymbol;

public Dtl() {}

public Dtl(String tickerSymbol) {
this.tickerSymbol = tickerSymbol;
}

public Long getId() {
return id;
}

public String getTickerSymbol() {
return tickerSymbol;
}

public void setTickerSymbol(String tickerSymbol) {
this.tickerSymbol = tickerSymbol;
}
}

主Repository:

public interface GetDtlP extends JpaRepository<Dtl, Long> {
List<Dtl> findByTickerSymbol(String tickerSymbol);
}

次數據源:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary",
transactionManagerRef = "transactionManagerSecondary",
basePackages = {"com.example.demo.s"})
public class SecondaryConfig {
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;

@Autowired
private JpaProperties jpaProperties;

private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}

@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
return builder.
dataSource(secondaryDataSource)
.properties(getVendorProperties())
.packages("com.example.demo.s")
.persistenceUnit("SecondaryPersistenceUnit")
.build();
}

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

@Bean(name = "transactionManagerSecondary")
public PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}

次Entity:

@Entity
@Table(name = "xx")
public class Dtl {
@Id
@Column(name = "id")
private Long id;

@Column(name = "TICKER_SYMBOL")
private String tickerSymbol;

public Dtl() {}

public Dtl(String tickerSymbol) {
this.tickerSymbol = tickerSymbol;
}

public Long getId() {
return id;
}

public String getTickerSymbol() {
return tickerSymbol;
}

public void setTickerSymbol(String tickerSymbol) {
this.tickerSymbol = tickerSymbol;
}
}

次Repository:

public interface GetDtlS extends JpaRepository<Dtl, Long> {
List<Dtl> findByTickerSymbol(String tickerSymbol);
}

測試:

@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaTest {

@Autowired
protected GetDtlP getDtlP;

@Autowired
protected GetDtlS getDtlS;

@Test
public void test() throws Exception {
List a = getDtlP.findByTickerSymbol("3");
List b = getDtlS.findByTickerSymbol("3");
Assert.assertEquals(a, b);
}
}

轉載于:https://www.cnblogs.com/ylpb/p/9209802.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397648.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397648.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397648.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Windows虛擬地址轉物理地址(原理+源碼實現,附簡單小工具)

By Lthis 上個月就想寫了&#xff0c;一直沒時間...網上大概搜了一下&#xff0c;原理與操作倒是一大堆&#xff0c;一直沒看到源碼實現&#xff0c;總得有人動手&#xff0c;這回輪到我了。東西寫得很爛&#xff0c;請大牛勿噴。一直覺得靠源碼的方式驅動學習是非常好的一種學…

python裝飾器的使用

借用裝飾器&#xff0c;我們可以批量的對老的函數進行改造或擴展老函數功能&#xff0c;比如需要對函數的接收參數進行過濾&#xff0c;Flash的url路由功能就是使用的這個方式 def dropoushu(): # 這一層函數可以去掉&#xff0c;如果去掉了&#xff0c;則使用checkjiou這種方…

7_文件上傳漏洞

文件上傳漏洞 當文件上傳時&#xff0c;若服務端腳本語言未對上傳的文件進行嚴格驗證和過濾&#xff0c;若惡意用戶上傳惡意的腳本文件時&#xff0c;就有可能控制整個網站甚至是服務器&#xff0c;這就是文件上傳漏洞。 權限 1.網站后臺權限&#xff1a;登陸了后臺&#xff0…

mysql數據庫如何實現分頁查詢_不同數據庫的分頁查詢實現方法總結

分頁查詢是數據庫查詢中經常用到的一項操作&#xff0c;對查詢出來的結果進行分頁查詢可以方便瀏覽。那么Oracle、SQL Server、MySQL是如何實現查詢的呢&#xff1f;本文我們就來介紹這一部分內容。首先我們先看一下SQL Server 數據庫中SQL語句查詢分頁數據的解決方案&#xff…

POJ - 3842 An Industrial Spy dfs(水)

題意:給你一串數字&#xff0c;最少一個&#xff0c;最多七個&#xff0c;問用這里面的數字能組成多少素數&#xff0c;不重復。 思路&#xff1a;之前還遍歷10000000的每一個素數&#xff0c;結果超時&#xff0c;后來發現直接dfs就可以了&#xff0c;只是標記一下做過的數。 …

飛機大戰小游戲1.0版本

小時候大家應該都玩過飛機大戰吧&#xff0c;這就是仿的一個飛機大戰&#xff0c;但是沒有寫的很全&#xff0c;只能玩一次&#xff0c;死掉之后需要刷新頁面玩第二次&#xff0c;話不說多&#xff0c;上代碼&#xff1a; 初始頁面&#xff1a; 整個的html代碼還是很少&#xf…

記一次Jquery獲取值的典型錯誤

直接上代碼&#xff1a; 代碼很簡單&#xff0c;通過Post的形式提交參數&#xff0c;但是發現提交的data總是空&#xff0c;昨晚有點納悶&#xff0c;今天一看才發現。。。 獲取值得時候的順序有問題&#xff0c;獲取值應該是在onclick事件中。 綜上&#xff1a;寫Jquery的時間…

android 調用java接口_android調用java的web service接口

android中通過webservice調用服務器端其實還是很簡單的&#xff0c;只要按部就班的按照下面步驟進行即可&#xff1a;(1)創建HttpTransportSE對象&#xff0c;該對象用于調用WebService操作代碼如下:HttpTransportSE ht new HttpTransportSE(SERVICE_URL);(2)創建SoapSerializ…

iOS: TableView如何刷新指定的cell 或section

/一個section刷新 NSIndexSet *indexSet[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //一個cell刷新 NSIndexPath *indexPath[NSIndexPath indexPathForRow:3 inSection:0…

Skype For Business 2015實戰系列14:創建Office Web App服務器場

Skype For Business 2015實戰系列14&#xff1a;創建Office Web App服務器場前面的操作中我們已經成功的安裝了Office Web App Server&#xff0c;今天我們將創建Office Web App服務器場。具體步驟如下:配置證書&#xff1a;登陸到OWA服務器,打開服務器管理器&#xff0c;點擊“…

https://cwiki.apache.org/confluence/display/FLINK/FLIP-24+-+SQL+Client

https://cwiki.apache.org/confluence/display/FLINK/FLIP-24-SQLClient轉載于:https://www.cnblogs.com/WCFGROUP/p/9214589.html

java ee me se_java EE ME SE有什么關系

1. Java SE(Java Platform&#xff0c;Standard Edition)。Java SE 以前稱為 J2SE。它允許開發和部署在桌面、服務器、嵌入式環境和實時環境中使用的 Java 應用程序。Java SE 包含了支持 Java Web 服務開發的類&#xff0c;為 Java Platform&#xff0c;Enterprise Edition(Jav…

Android第三夜

Paint的設置方法 setAntiAlias&#xff1a;這是畫筆的鋸齒效 setColor setARGB setAlpha setTextSize setStyle setStrokeWidth getColor getAlpha Canvas繪制常見圖形的方法 1&#xff0c;繪制直線 左上角是0.0點 drawLine(float startX,float startY,float stopX,float stopY…

JavaScript websocket 實例

實例: 即時通訊聊天室demo可以打開兩個頁面互相發送消息查看。 websocket.js /* 判斷瀏覽器是否內置了websocket */if (WebSocket in window) {websocket new WebSocket(ws://180.76.144.202:19910/websocket);}websocket->onerror onerror;websocket->onopen onopen…

SQL Server 2008 基礎

SQL Server 2008 基礎SQL流程TDS是一種協議&#xff0c;一系列描述兩個計算機間如何傳輸數據的規則。象別的協議一樣&#xff0c;它定義了傳輸信息的類型和他們傳輸的順序。總之&#xff0c;協議描述了“線上的位”&#xff0c;即數據如何流動。表格數據流協議是建立在TCP/IP N…

python異步處理請求_如何一次在python中發送異步http請求?

1)創建一個Queue.Queue對象2)根據您的喜好制作盡可能多的“工作”線程,從Queue.Queue讀取3)將作業提供給Queue.Queue工作線程將按照它們放置的順序讀取Queue.Queue從文件中讀取行并將它們放入Queue.Queue的示例import sysimport urllib2import urllibfrom Queue import Queueim…

如何通過Git GUI將自己本地的項目上傳至Github

ithud是一個程序員以后成長都會使用到的&#xff0c;先不說很多優秀的開源框架都在這上面發布&#xff0c;光是用來管理自己的demo都已經讓人感到很方便&#xff0c;用得也很順暢。而真正讓我下定決心使用github的原因是因為兩次誤操作&#xff0c;將自己所有的學習demo全都刪除…

lucene解決全文檢索word2003,word2007的辦法

在上一篇文章中 &#xff0c;lucene只能全文檢索word2003&#xff0c;無法檢索2007&#xff0c;并且只能加載部分內容&#xff0c;無法加載全文內容。為解決此問題&#xff0c;找到了如下方法 POI 讀取word (word 2003 和 word 2007) 最近在給客戶做系統的時候&#xff0c;用戶…

【JSP筆記】第三章 JSP內置對象【上】

2019獨角獸企業重金招聘Python工程師標準>>> 1.內置對象簡介&#xff1a;JSP內置對象是WEB容器創建的一組對象&#xff0c;不使用new關鍵就可以是用的對象。 <% out.println(123); %> 2.九大內置對象&#xff1a; outrequestresponsesessionapplication Page …

自定義標簽 —— 實現時間轉換和輸出功能

第一步&#xff1a;導入jar包 jsp-api-2.2-sources.jar <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.…