當前位置:首頁 » 操作系統 » hibernate多資料庫

hibernate多資料庫

發布時間: 2022-05-08 02:51:05

⑴ hibernate跨資料庫查詢

<!-- DataSource -->
以Mysql資料庫為例: <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://遠程資料庫的地址:埠號/資料庫名" />
<property name="user" value="遠程資料庫的用戶名" />
<property name="password" value="遠程資料庫的密碼" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="10" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="60" /> <!--單位秒-->
</bean>說白了,就是取得遠程資料庫的地址、埠號、用戶名、密碼就可以了。與連接本機區別不大。

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

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

⑶ hibernate如何支持多資料庫

一個sessionFactory對應一個資料庫 配置多個不同名的sf 寫一個容器管理他們

⑷ NHibernate 連接多資料庫怎麼配置

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

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

根據類型獲取ISessionFactory:

java">
{
<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);
}
//......
}

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

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

⑹ java web項目中hibernate +spring +tomcat如何實現多資料庫

一下方法只能是同一種資料庫, 因為資料庫不同的話,資料庫方言也就不同,這個暫時解決不了

applicationContext.xml:
<bean id="parentDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 設置連接池初始值 -->
<property name="initialSize" value="5" />
<!-- 設置連接池最大值 -->
<property name="maxActive" value="5" />
<!-- 設置連接池最小空閑值 -->
<property name="minIdle" value="2" />
<!-- 設置連接池最大空閑值 -->
<property name="maxIdle" value="5" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
<!-- 設置資料庫方言 -->
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
<!-- 輸出SQL語句到控制台 -->
hibernate.show_sql=true
<!-- 格式化輸出到控制台的SQL語句 -->
hibernate.format_sql=true

</value>
</property>
<property name="packagesToScan" value="com.zxw.link.business.**.entity"></property>
</bean>

<bean id="dataSource" class="com.zxw.link.commons.data.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="zxw" value-ref="zxw_DataSource"/>
<entry key="new_back" value-ref="new_back_DataSource"/>
<!--更多的數據源,根據下面的配置文件再寫一個datasource 並且在這里配置entry -->
</map>
</property>
<property name="defaultTargetDataSource" ref="zxw_DataSource"/>
</bean>

<bean id="zxw_DataSource" parent="parentDataSource">
<!-- 設置JDBC驅動名稱 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<!-- 設置JDBC連接URL -->
<property name="url" value="jdbc:oracle:thin:@148.20.20.17:1521:oradb" />
<!-- 設置資料庫用戶名 -->
<property name="username" value="zhou" />
<!-- 設置資料庫密碼 -->
<property name="password" value="zhou" />
</bean>

<!-- #################### 新備份 ################ -->
<bean id="new_back_DataSource" parent="parentDataSource">
<!-- 設置JDBC驅動名稱 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<!-- 設置JDBC連接URL -->
<property name="url" value="jdbc:oracle:thin:@148.20.20.15:1521:ora10" />
<!-- 設置資料庫用戶名 -->
<property name="username" value="report" />
<!-- 設置資料庫密碼 -->
<property name="password" value="report" />
</bean>
<!-- #################### 新備份 ################ -->

DynamicDataSource .java:

public class DynamicDataSource extends AbstractRoutingDataSource {

protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return CustomerContextHolder.getCustomerType();
}

}

CustomerContextHolder .java:

public class CustomerContextHolder {
private static final ThreadLocal contextHolder =
new ThreadLocal();

public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}

public static String getCustomerType() {
return (String) contextHolder.get();
}

public static void clearCustomerType() {
contextHolder.remove();
}
}

action中調用
public class ZzhAction {

@Resource
private ZzhService zzhService;

@RequestMapping("/zzh/test.do")
public void test(){

List<Zzh> zzhs=zzhService.getAll();
System.out.println(zzhs.size());
//設置數據源, 參數為配置文件中的
//<entry key="new_back" value-ref="new_back_DataSource"/> 的key
CustomerContextHolder.setCustomerType("new_back");
}

}

⑺ 程序中hibernate怎樣實現多個資料庫多表互相聯查

程序中hibernate怎樣實現多個資料庫多表互相聯查
參考如下
例如:student表和score表需要做聯合查詢。
1)sql:
select
s.id,s.name,sc.score
from
student
as
s,score
as
sc
where
s.id
=
sc.userId;
(欄位都是用的資料庫中欄位名稱)
2)HQL:
select
s.id,s.name,sc.score
from
Student
as
s,Score
as
sc
where
s.id
=
sc.userId;
(上面欄位都是
javabean的屬性)
如果按1)查詢的話,必須調用
session.createSQLQuery();方法
如果按2)查詢,還是調用
session.createQuery();

⑻ 基於hibernate 技術的多種資料庫管理系統

說的專業,其實就是基於Hibernate的javaweb開發,創建SSH項目,資料庫操作使用HIbernate操作

⑼ Hibernate 向資料庫一次插入多條數據

把你需要插入的數據全部放到一個集合裡面,然後遍歷插入,個人覺得用set比較合適因為set是不可重復的,這樣才比較符合資料庫。

⑽ 怎樣用Hibernate配置多個數據源

如果用xml配置的話,那就寫兩個配置文件,可以不再用「hibernate.cfg.xml」做文件名,隨便什麼都可以,像「mysql.cfg.xml」或「sqlserver.xml」都行。用Configuration類獲取SessionFactory的代碼:

SessionFactory mysqlFactory = new Configuration().configure("mysql.cfg.xml").buildSessionFactory();

SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.xml").buildSessionFactory();

如果你用spring,多資料庫就更簡單了,像這段代碼可以完成所有配置:

<beans>
<bean id="mysqlDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>

<bean id="mysqlFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="mysqlDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>

<bean id="sqlserverDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:odbc:test</value>
</property>
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>

<bean id="sqlserverFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="sqlserverDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>

.......

</beans>

hibernate和spring還有很多可行的配置,可以參考他們的references,有很詳細地說明的。

熱點內容
在團競模式中怎麼重置配置 發布:2024-10-08 02:12:54 瀏覽:289
寶馬遠程伺服器如何啟用 發布:2024-10-08 02:02:57 瀏覽:391
c語言freadfwrite 發布:2024-10-08 02:01:15 瀏覽:854
腳本還不簡單嗎 發布:2024-10-08 01:54:43 瀏覽:423
安卓手機如何像平板一樣橫屏 發布:2024-10-08 01:33:26 瀏覽:510
wapi認證伺服器ip 發布:2024-10-08 01:33:24 瀏覽:507
centos自帶python 發布:2024-10-08 00:53:31 瀏覽:340
android串口調試助手 發布:2024-10-08 00:45:03 瀏覽:405
sqlserver2008亂碼 發布:2024-10-08 00:39:59 瀏覽:220
華為電腦伺服器系統進不去提示 發布:2024-10-08 00:13:42 瀏覽:493