當前位置:首頁 » 操作系統 » hibernate連接兩個資料庫

hibernate連接兩個資料庫

發布時間: 2022-05-26 05:24:16

1. hibernate怎麼連接資料庫

首先,我們把hibernate最基本的資料庫連接,使用mysql。 見一個java工程,見一個包名為book, 在book的包下加一個java類Book.java,其代碼如下: package book; public class Book { private Integer id; private String name; private String writer; public Integer get hibernate最基本的資料庫連接,使用mysql。 見一個java工程,見一個包名為「book」

2. hibernate資料庫實現查詢兩個不同的資料庫

配置兩個數據源,將兩個sessionfactory注入到不同的兩個templete中,將兩個templete模板再注入到兩個中,一個調用模板的get方法取出A庫中的學號,根據學號,另一調用get方法取出b庫中的數據對象,然後操作A資料庫的調用sava或者savaorupdate插入到A庫,主要配置好hibernate+spring整合的配置文件

3. hibernate與資料庫連接的幾種方式

三種連接都是以連接MySQl為例。

<!-- JDBC驅動程序 -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/struts?useUnicode=true&characterEncoding=GBK</property> <!-- 資料庫用戶名 -->
<property name="connection.username">root</property> <!-- 資料庫密碼 -->
<property name="connection.password">8888</property>

上面的一段配置,在c3p0和dbcp中,都是必需的,因為hibernate會根據上述的配置來生成connections,再交給c3p0或dbcp管理.但是,proxool則不能,雖然說服文檔上說proxool也可以和hibernate結合,但我按照官方文檔上的說明怎麼配也出錯,而且,到了sun和hibernat有的官方網站上問了幾天,都沒有一個人回復。後來我只能讓proxool自身來生成連接,這在下面再講。

1 C3P0

只需在hibernate.cfg.xml中加入
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.time_out">1800</property>
<property name="c3p0.max_statement">50</property>

還有在classespath中加入c3p0-0.8.4.5.jar

2 dbcp

在hibernate.cfg.xml中加入

<property name="dbcp.maxActive">100</property>
<property name="dbcp.whenExhaustedAction">1</property>
<property name="dbcp.maxWait">60000</property>
<property name="dbcp.maxIdle">10</property>

<property name="dbcp.ps.maxActive">100</property>
<property name="dbcp.ps.whenExhaustedAction">1</property>
<property name="dbcp.ps.maxWait">60000</property>
<property name="dbcp.ps.maxIdle">10</property>

還有在classespath中加入commons-pool-1.2.jar 和commons-dbcp-1.2.1.jar.

3 proxool

在hibernate.cfg.xml中加入

<property name="proxool.pool_alias">pool1</property>
<property name="proxool.xml">ProxoolConf.xml</property>
<property name="connection.provider_class">net.sf.hibernate.connection.ProxoolConnectionProvider</property>

然後,在和hibernate.cfg.xml同一個目錄下,加一個ProxoolConf.xml文件,內容為

<?xml version="1.0" encoding="utf-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>pool1</alias>
<!--proxool只能管理由自己產生的連接-->
<driver-url>jdbc:mysql://localhost:3306/struts?useUnicode=true&characterEncoding=GBK</driver-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="8888"/>
</driver-properties>
<!-- proxool自動偵察各個連接狀態的時間間隔(毫秒),偵察到空閑的連接就馬上回收,超時的銷毀-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 指因未有空閑連接可以分配而在隊列中等候的最大請求數,超過這個請求數的用戶連接就不會被接受-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空閑連接數-->
<prototype-count>5</prototype-count>
<!-- 允許最大連接數,超過了這個連接,再有請求時,就排在隊列中等候,最大的等待請求數由maximum-new-connections決定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小連接數-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>

並在classespath中加入proxool-0.8.3.jar

4. 資料庫表在兩個資料庫中,怎麼進行hibernate映射呢

前提是你數據源配置正確.
映射文件中:
注意看class屬性:
name是你的model類(包名+類名)
table是你資料庫里的表名
catalog是你的資料庫名
<hibernate-mapping>
<class name="com.cwx.model.Xsb" table="XSB" schema="dbo" catalog="XSCJ">
......
</class>
</hibernate-mapping>

5. NHibernate 連接多資料庫怎麼配置

在開發一些項目時,會使用到多個資料庫。例如類A保存在資料庫A,類B保存在資料庫B。NHibernate在BuildSessionFactory之後,ISessionFactory就不能改變資料庫的連接,即是說一個ISessionFactory只能對應一個資料庫連接。但NHibernate可以在同一個應用中實例化多個ISessionFactory。實例化多個ISessionFactory,並讓類A或類B找到自己所對應的ISessionFactory,獲取ISession,即可實現多資料庫連接。

如何通過類型獲取ISessionFactory呢?ISessionFactory的Statistics.EntityNames中保存了所有映射了的實體類的類名。我們可以判斷實體類的類名是否在EntityNames中,確定實體類所對應的ISessionFactory。

根據類型獲取ISessionFactory:


{
<TEntity>()whereTEntity:IEntity;
}

:ISessionFactoryHolder
{
privateIDictionary<string,int>entityDictionary;
privateIDictionary<int,ISessionFactory>factoryDictionary;

publicSessionFactoryHolder()
{
this.entityDictionary=newDictionary<string,int>();
this.factoryDictionary=newDictionary<int,ISessionFactory>();
}

#

public<TEntity>()whereTEntity:IEntity
{
inthashCode=0;

Asserts.Assert<MappingException>(
this.EntityInDictionary(typeof(TEntity).FullName,outhashCode)==false
,string.Format("Nopersisterfor:{0}",typeof(TEntity).FullName));

returnthis.factoryDictionary[hashCode];
}

#endregion

(ISessionFactorysessionFactory)
{
Asserts.IsNotNull(sessionFactory,"sessionFactory");

this.factoryDictionary[sessionFactory.GetHashCode()]=sessionFactory;

this.(sessionFactory.Statistics.EntityNames
,sessionFactory.GetHashCode());
}

privateboolEntityInDictionary(stringentityName,outintsessionFactoryHashCode)
{
returnthis.entityDictionary.TryGetValue(entityName,outsessionFactoryHashCode);
}

privatevoid(string[]entityNames,intsessionFactoryHashCode)
{
foreach(varentityNameinentityNames)
{
this.entityDictionary[entityName]=sessionFactoryHashCode;
}
}
}

根據類型獲取ISession:

publicinterfaceISessionHolder:IDisposable
{
ISessionGetSessionForEntity<TEntity>()whereTEntity:IEntity;
}
publicclassSessionHolder:ISessionHolder,IUnitOfWork
{
;
privateIDictionary<int,ISession>sessionDictionary;

publicSessionHolder()
{
Asserts.IsNotNull(factoryHolder,"factoryHolder");

this.factoryHolder=factoryHolder;
this.sessionDictionary=newDictionary<int,ISession>();
}

#regionISessionHolderMembers

<TEntity>()whereTEntity:IEntity
{
if(this.sessionDictionary.ContainsKey(this.GetFactoryHashCode<TEntity>())==false)
{
this.sessionDictionary[this.GetFactoryHashCode<TEntity>()]=this.OpenNewSession<TEntity>();
}

returnthis.sessionDictionary[this.GetFactoryHashCode<TEntity>()];
}

#endregion

#regionIDisposableMembers
//DisposeCode
#endregion

#regionIUnitOfWork
//IUnitOfWork
#endregion

<TEntity>()whereTEntity:IEntity
{
returnthis.factoryHolder.GetSessionFactoryForEntity<TEntity>();
}

privateintGetFactoryHashCode<TEntity>()whereTEntity:IEntity
{
returnthis.GetFactory<TEntity>().GetHashCode();
}

privateISessionOpenNewSession<TEntity>()whereTEntity:IEntity
{
returnthis.GetFactory<TEntity>().OpenSession();
}
}

Repository:

publicinterfaceIRepository<TEntity>whereTEntity:IEntity
{
voidSave(TEntityentity);
//......
}

<TEntity>:IRepository<TEntity>whereTEntity:IEntity
{
;

publicNHibernateRepository(ISessionHoldersessionHolder)
{
Asserts.IsNotNull(sessionHolder,"sessionHolder");

this.sessionHolder=sessionHolder;
}


{
get
{
returnthis.sessionHolder.GetSessionForEntity<TEntity>();
}
}

publicoverridevoidSave(TEntityentity)
{
this.Session.Save(entity);
}
//......
}

6. hibernate連接兩個不同的資料庫,連接信息分別寫在兩個Spring配置文件里,該怎麼讀這兩個Spring文件

在 applicationContext.xml裡面載入 <import resource="applicationContext-sql.xml"/>
在applicationContext-sql.xml裡面beans標記裡面多寫幾個Bean,分別用SQL Server資料庫和Oracle資料庫的驅動載入進去啊!首先確定兩個資料庫驅動必須有哦!

7. Hibernate 怎樣 連接2個以上的資料庫

<class
name="entity.Person"
table="Person"
schema="dbo"
catalog="hibernate">
用catalog屬性設置是那個資料庫就可以拉

8. 一個java工程如何連接兩個資料庫

可以的,使用jdbc的鏈接代碼,在你需要的地方建立兩個連接即可。每個連接連接到一個資料庫。

9. hibernate可以跨資料庫嗎

首先來說,跨資料庫肯定是可以的!
通常有兩個方法,第一個是笨辦法,就是在配置項里定義兩個數據源,並且這兩個數據源分屬於兩個SessionFaction對象。並且在代碼中也有創建兩個對象分別對應兩個資料庫,這樣做比較麻煩,代碼會很繁瑣,並且執行效率不一定高。

第二個辦法是使用spring開源框架里提供的動態數據源,通過動態的載入,將兩個數據源信息載入到一個SessionnFacgtion對象中。方法一裡面提到的缺點在這里都能夠很好的解決。

大概步驟如下:
1.org.springframework.beans.factory.support.DefaultListableBeanF

actory獲得bean工廠,可以添加銷毀數據源;

2.org.springframework.beans.factory.support.BeanDefinitionBuilder動態創建bean,然後通過
DefaultListableBeanFactory.registerBeanDefinition(dsInfo.getId(), beanDefinitionBuilder.getBeanDefinition()); 注冊數據源事務

3.銷毀數據源
beanFactory.destroySingleton(tsId);
beanFactory.removeBeanDefinition(tsId);

10. springmvc spring hibernate 怎麼配置連接兩個資料庫

persistent.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="userPU" transaction-type="RESOURCE_LOCAL">
<!--jpa的提供者-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!--聲明資料庫連接的驅動-->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!--jdbc資料庫的連接地址-->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/manager?characterEncoding=gbk"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<!--配置方言-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--激活查詢日誌功能-->
<property name="hibernate.show_sql" value="true"/>
<!--優雅地輸出Sql-->
<property name="hibernate.format_sql" value="true"/>
<!--添加一條解釋型標注-->
<property name="hibernate.use_sql_comments" value="false"/>
<!--配置如何根據java模型生成資料庫表結構,常用update,validate-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
applicationContext.xml配置
<!--第二步-->
<!--定義實體的工廠bean-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.">
<property name="persistenceUnitName" value="userPU" />
<property name="persistenceXmlLocation" value="classpath:persistence.xml"></property>
</bean>

<!--第三步-->
<!--定義事務管理器-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

熱點內容
c語言無輸入 發布:2024-10-27 13:33:26 瀏覽:461
箱變里邊主要由哪些配置 發布:2024-10-27 13:28:55 瀏覽:470
安卓自動腳本怎麼寫 發布:2024-10-27 13:25:46 瀏覽:22
java論壇項目 發布:2024-10-27 13:25:31 瀏覽:590
cochrane資料庫 發布:2024-10-27 13:19:39 瀏覽:807
WCL演算法 發布:2024-10-27 13:13:29 瀏覽:293
解壓手寫板怎麼做 發布:2024-10-27 13:06:25 瀏覽:927
pythonetree 發布:2024-10-27 12:48:32 瀏覽:195
cmd非默認埠ftp 發布:2024-10-27 12:41:56 瀏覽:121
android無線網卡 發布:2024-10-27 12:36:07 瀏覽:772