用户登录
用户注册

分享至

tomcat hibernate库

  • 作者: 巴黎佐岸丶
  • 来源: 51数据库
  • 2020-12-29
一下方法只能是同一种数据库, 因为数据库不同的话,数据库方言也就不同,这个暂时解决不了

applicationContext.xml:
















hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

hibernate.show_sql=true

hibernate.format_sql=true









































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 zzhs=zzhService.getAll();
System.out.println(zzhs.size());
//设置数据源, 参数为配置文件中的
// 的key
CustomerContextHolder.setCustomerType("new_back");
}

}




  随便你怎么弄啊
针对多个数据库可以配置多个tomcat的数据源
hibernate中DAO就取多个数据源
也可以写几个连接数据的配置文件就行了,然后在调用配置文件下加上你多写的配置文件名字就行了



  想要连接几个数据库,就在配置文件里配置几个数据源,然后分别调用需要使用的数据源



  一下方法只能是同一种数据库, 因为数据库不同的话,数据库方言也就不同,这个暂时解决不了

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");
}

}
软件
前端设计
程序设计
Java相关