如何用连接池,Hibernate,Spring连多个SQL数据库
3个回答
展开全部
1:在proxool-conf.xml中配置两个数据库连接池
<proxool>
<alias>dataSource_ksmaeaII</alias>
<driver-url>jdbc:jtds:sqlserver://192.10.110.192:1433;databaseName=ksmaeaII</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="" />
</driver-properties>
<maximum-new-connections>100</maximum-new-connections>
<prototype-count>1</prototype-count>
<maximum-connection-count>400</maximum-connection-count>
<minimum-connection-count>20</minimum-connection-count>
<house-keeping-test-sql>select getdate()</house-keeping-test-sql>
</proxool>
<proxool>
<alias>ds1</alias>
<driver-url>jdbc:jtds:sqlserver://192.10.110.192:1433;databaseName=ksmaeaIIdbmove</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="" />
</driver-properties>
<maximum-new-connections>100</maximum-new-connections>
<prototype-count>1</prototype-count>
<maximum-connection-count>400</maximum-connection-count>
<minimum-connection-count>20</minimum-connection-count>
<house-keeping-test-sql>select getdate()</house-keeping-test-sql>
</proxool>
2:要连几个库就配置几个hibernate.cfg.xml,下面我配置两个数据库为例
创建hibernate.cfg.xml:里面写第一个数据库的映射文件(本地数据库),如<mapping
resource="com/wisoft/datachange/data/hibernate/dao/SbInfApply.hbm.xml" />
创建hibernate.cfg1.xml:里面写第二个数据库的映射文件(前置库,别人的数据库),如<mapping
resource="com/wisoft/datachange/data/hibernate/dao/InfApply.hbm.xml" />
3:创建一个类,继承HibernateDaoSupport。(直接拷过去就能直接用,我的第二个数据库就是用了这里面的factory1,第一个数据库用了Hibernate原来的factory,如果要连多个库,则多创建几个factory)
package com.wisoft.datachange.data;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class DebugBean extends HibernateDaoSupport {
private SessionFactory factory1;
// private SessionFactory factory2;
protected HibernateTemplate hibernateTemplate1;
// protected HibernateTemplate hibernateTemplate2;
public void setFactory1(SessionFactory factory1) {
this.factory1 = factory1;
}
// public void setFactory2(SessionFactory factory2) {
// this.factory2 = factory2;
// }
public SessionFactory getFactory1() {
return factory1;
}
// public SessionFactory getFactory2() {
// return factory2;
// }
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
return super.createHibernateTemplate(sessionFactory);
}
protected void initDao() throws Exception {
hibernateTemplate1 = super.createHibernateTemplate(factory1);
// hibernateTemplate2 = super.createHibernateTemplate(factory2);
super.initDao();
}
}
4:编写自己的测试BO:在BO里注入DAO,如下
public class InfApplyBO implements IInfApplyBO {
private IInfApplyDAO myInfApplyDAO;
public IInfApplyDAO getMyInfApplyDAO() {
return myInfApplyDAO;
}
public void setMyInfApplyDAO(IInfApplyDAO myInfApplyDAO) {
this.myInfApplyDAO = myInfApplyDAO;
}
//底下编写自己的业务代码
//用this.myInfApplyDAO直接调用Dao层的方法,由DAO对数据库进行操作
}
5:编写自己的测试DAO:继承DebugBean如下
public class MyInfApplyDAO extends DebugBean implements IInfApplyDAO {
//对数据库的操作均可写在这里,可以直接用模版save
//如要对第一个数据库操作,只需this.getHibernateTemplate().find(hql);
//如要对第二个数据库操作,只需hibernateTemplate1.save(obj);就行
//注意:在对数据库操作之前不要忘了写applicationContext.xml的配置文件,看第6步
}
6:写applicationContext.xml的配置文件
<!--对第一个数据库连接池的配置-->
<bean id="dataSource_ksmaeaII" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.dataSource_ksmaeaII</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource_ksmaeaII" />
</property>
<property name="lobHandler" ref="lobHandler" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.AllowCreate">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.default_schema">dbo</prop>
</props>
</property>
</bean>
<!--对第二个数据库连接池的配置-->
<bean id="ds1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.ds1</value>
</property>
</bean>
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="ds1" />
</property>
<property name="lobHandler" ref="lobHandler" />
<property name="configLocation">
<value>classpath:hibernate1.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.AllowCreate">true</prop>
</props>
</property>
</bean>
<!--配置事务-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory1"/>
</property>
</bean>
<!--工厂模版-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="write">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="makeWorkNoOfDep">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true" />
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler">
<!-- <property name="nativeJdbcExtractor">
<ref bean="nativeJdbcExtractor" />
</property> -->
</bean>
<bean id="debugbean" class="com.wisoft.datachange.data.DebugBean" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="factory1" ref="sessionFactory1" />
<!-- <property name="factory2" ref="sessionFactory2" />-->
</bean>
<bean id="myInfApplyDAO"
class="com.wisoft.datachange.data.hibernate.MyInfApplyDAO" parent="debugbean" >
</bean>
<bean id="InfApplyBO" parent="txProxyTemplate">
<property name="target">
<bean class="com.wisoft.datachange.bo.impl.InfApplyBO">
<property name="myInfApplyDAO" ref="myInfApplyDAO" />
</bean>
</property>
</bean>
到此,数据库就连起来啦\(^o^)/~
<proxool>
<alias>dataSource_ksmaeaII</alias>
<driver-url>jdbc:jtds:sqlserver://192.10.110.192:1433;databaseName=ksmaeaII</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="" />
</driver-properties>
<maximum-new-connections>100</maximum-new-connections>
<prototype-count>1</prototype-count>
<maximum-connection-count>400</maximum-connection-count>
<minimum-connection-count>20</minimum-connection-count>
<house-keeping-test-sql>select getdate()</house-keeping-test-sql>
</proxool>
<proxool>
<alias>ds1</alias>
<driver-url>jdbc:jtds:sqlserver://192.10.110.192:1433;databaseName=ksmaeaIIdbmove</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="" />
</driver-properties>
<maximum-new-connections>100</maximum-new-connections>
<prototype-count>1</prototype-count>
<maximum-connection-count>400</maximum-connection-count>
<minimum-connection-count>20</minimum-connection-count>
<house-keeping-test-sql>select getdate()</house-keeping-test-sql>
</proxool>
2:要连几个库就配置几个hibernate.cfg.xml,下面我配置两个数据库为例
创建hibernate.cfg.xml:里面写第一个数据库的映射文件(本地数据库),如<mapping
resource="com/wisoft/datachange/data/hibernate/dao/SbInfApply.hbm.xml" />
创建hibernate.cfg1.xml:里面写第二个数据库的映射文件(前置库,别人的数据库),如<mapping
resource="com/wisoft/datachange/data/hibernate/dao/InfApply.hbm.xml" />
3:创建一个类,继承HibernateDaoSupport。(直接拷过去就能直接用,我的第二个数据库就是用了这里面的factory1,第一个数据库用了Hibernate原来的factory,如果要连多个库,则多创建几个factory)
package com.wisoft.datachange.data;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class DebugBean extends HibernateDaoSupport {
private SessionFactory factory1;
// private SessionFactory factory2;
protected HibernateTemplate hibernateTemplate1;
// protected HibernateTemplate hibernateTemplate2;
public void setFactory1(SessionFactory factory1) {
this.factory1 = factory1;
}
// public void setFactory2(SessionFactory factory2) {
// this.factory2 = factory2;
// }
public SessionFactory getFactory1() {
return factory1;
}
// public SessionFactory getFactory2() {
// return factory2;
// }
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
return super.createHibernateTemplate(sessionFactory);
}
protected void initDao() throws Exception {
hibernateTemplate1 = super.createHibernateTemplate(factory1);
// hibernateTemplate2 = super.createHibernateTemplate(factory2);
super.initDao();
}
}
4:编写自己的测试BO:在BO里注入DAO,如下
public class InfApplyBO implements IInfApplyBO {
private IInfApplyDAO myInfApplyDAO;
public IInfApplyDAO getMyInfApplyDAO() {
return myInfApplyDAO;
}
public void setMyInfApplyDAO(IInfApplyDAO myInfApplyDAO) {
this.myInfApplyDAO = myInfApplyDAO;
}
//底下编写自己的业务代码
//用this.myInfApplyDAO直接调用Dao层的方法,由DAO对数据库进行操作
}
5:编写自己的测试DAO:继承DebugBean如下
public class MyInfApplyDAO extends DebugBean implements IInfApplyDAO {
//对数据库的操作均可写在这里,可以直接用模版save
//如要对第一个数据库操作,只需this.getHibernateTemplate().find(hql);
//如要对第二个数据库操作,只需hibernateTemplate1.save(obj);就行
//注意:在对数据库操作之前不要忘了写applicationContext.xml的配置文件,看第6步
}
6:写applicationContext.xml的配置文件
<!--对第一个数据库连接池的配置-->
<bean id="dataSource_ksmaeaII" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.dataSource_ksmaeaII</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource_ksmaeaII" />
</property>
<property name="lobHandler" ref="lobHandler" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.AllowCreate">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.default_schema">dbo</prop>
</props>
</property>
</bean>
<!--对第二个数据库连接池的配置-->
<bean id="ds1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.ds1</value>
</property>
</bean>
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="ds1" />
</property>
<property name="lobHandler" ref="lobHandler" />
<property name="configLocation">
<value>classpath:hibernate1.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.AllowCreate">true</prop>
</props>
</property>
</bean>
<!--配置事务-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory1"/>
</property>
</bean>
<!--工厂模版-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="write">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="makeWorkNoOfDep">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true" />
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler">
<!-- <property name="nativeJdbcExtractor">
<ref bean="nativeJdbcExtractor" />
</property> -->
</bean>
<bean id="debugbean" class="com.wisoft.datachange.data.DebugBean" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="factory1" ref="sessionFactory1" />
<!-- <property name="factory2" ref="sessionFactory2" />-->
</bean>
<bean id="myInfApplyDAO"
class="com.wisoft.datachange.data.hibernate.MyInfApplyDAO" parent="debugbean" >
</bean>
<bean id="InfApplyBO" parent="txProxyTemplate">
<property name="target">
<bean class="com.wisoft.datachange.bo.impl.InfApplyBO">
<property name="myInfApplyDAO" ref="myInfApplyDAO" />
</bean>
</property>
</bean>
到此,数据库就连起来啦\(^o^)/~
2018-07-26 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
你用了spring,但却没有看到你把session注入到配置文件中, applicationContext.xml也没有看到关于事物的配置。 用了spring后这些应该都交给spring来做的,我们是不用管close的,不然用spring还有什么用,配置半天还和原来一样,那还不如不用呢
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你用了spring,但却没有看到你把session注入到配置文件中, applicationContext.xml也没有看到关于事物的配置。 用了spring后这些应该都交给spring来做的,我们是不用管close的,不然用spring还有什么用,配置半天还和原来一样,那还不如不用呢
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询