当前位置:首页 » 操作系统 » 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>

热点内容
学cnc数控编程 发布:2024-10-27 11:04:56 浏览:226
mallat算法 发布:2024-10-27 11:03:57 浏览:33
讲脚本 发布:2024-10-27 10:47:54 浏览:713
存储免疫细胞 发布:2024-10-27 10:46:56 浏览:493
粉标溯源码 发布:2024-10-27 10:44:28 浏览:139
网吧服务器如何搭建 发布:2024-10-27 10:27:49 浏览:391
安卓手机如何设置不让玩王者荣耀 发布:2024-10-27 10:27:43 浏览:762
没有配置的游戏怎么玩 发布:2024-10-27 10:25:32 浏览:431
c语言好看 发布:2024-10-27 10:14:25 浏览:604
java写入数组 发布:2024-10-27 10:06:59 浏览:178