ssh项目(使用的是oracle数据库)配置多数据源:
1、首先配置多个数据库的连接:在applicationContext_ds.xml中使用配置数据库的连接:
第一个数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" >
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="数据库1的连接"/>
<property name="user" value="用户名"/>
<property name="password" value="密码"/>
<property name="initialPoolSize" value="1"/>
<property name="minPoolSize" value="2"/>
<property name="maxPoolSize" value="2"/>
<property name="acquireIncrement" value="1"/>
<property name="maxIdleTime" value="10"/>
<property name="maxStatements" value="0"/>
</bean>
第二个数据源
<bean id="newDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" >
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="数据库2的连接"/>
<property name="user" value="用户名"/>
<property name="password" value="密码"/>
<property name="initialPoolSize" value="1"/>
<property name="minPoolSize" value="2"/>
<property name="maxPoolSize" value="2"/>
<property name="acquireIncrement" value="1"/>
<property name="maxIdleTime" value="10"/>
<property name="maxStatements" value="0"/>
</bean>
2、在applicationContext_transaction.xml文件中配置事务的管理(要有两个这个文件,文件名做下区分,如我的一个是applicationContext_transaction.xml(对应第一个数据源的配置),一个是applicationContext_transaction_new.xml(对应的是第二个数据源的配置),但是记得在web.xml中扫描配置文件的时候都要扫的到才可以),下面举一个文件的例子
注意:要保证这个文件的头部和下面的一致
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="sessionFactory(第二个中配置的是newSessionFactory)" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource(第二个中配置的是第二个数据源的id newDataSource)"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.jdbc.fetch_size">200</prop>
<prop key="hibernate.jdbc.batch_size">200</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<!--配置需要扫描的包-->
</list>
</property>
</bean>
<!-- 事务管理器配置, Hibernate单数据源事务 -->
<bean id="transactionManager(第二个配置的是newTransactionManager)"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory(第二个配置的是newSessionFactory)"/>
</bean>
<!-- 1.使用annotation定义事务 -->
<!-- proxy-target-class ture:动态代理(需cglib类库的支持) false:接口(Spring默认方式) -->
<tx:annotation-driven transaction-manager="transactionManager(第二个配置的是newTransactionManager)"
proxy-target-class="true"/>
<!-- proxy-target-class true:CGLIB代理 false:JDK代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 2.声明式事务配置 -->
<aop:config>
<aop:pointcut id="serviceMethods(第二个配置的是newServiceMethods)"
expression="execution(配置需要添加事务的service层)"/>
<aop:advisor advice-ref="txAdvice(第二个配置的是newTxAdvice)" pointcut-ref="serviceMethods(第二个配置的是newServiceMethods)"/>
</aop:config>
<!-- 通知 默认事务管理器:transaction-manager="transactionManager" -->
<tx:advice id="txAdvice(第二个配置的是newTxAdvice)" transaction-manager="transactionManager(第二个配置的是newTransactionManager)">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="page*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="plot*" read-only="true"/>
<tx:method name="login*" propagation="REQUIRED" read-only="true"/>
<tx:method name="set*" propagation="REQUIRED"
rollback-for="自己定义的异常"/>
<tx:method name="start*" propagation="REQUIRED"
rollback-for="自己定义的异常"/>
<tx:method name="process*" propagation="REQUIRED"
rollback-for="自己定义的异常"/>
<tx:method name="audit*" propagation="REQUIRED"
rollback-for="自己定义的异常"/>
.........
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
3、配置完了以后就可以在dao层进行使用了。 在dao层先注入这个sessionFactory工厂,然后在从这个工厂中取出Session
@Autowired
@Qualifier(value="newSessionFactory")
protected SessionFactory sessionFactory;
public Session getCurrentSession(){
return this.sessionFactory.getCurrentSession();
}
在操作数据库的时候先使用这个Session得到一个Query对象,然后在去操作数据库
String sql="自己的sql语句";
Query query = this.getCurrentSession() .createSQLQuery(sql);
注:由于项目使用的是hibernate ,所以在查询的时候需要将自己映射的po或者vo的属性加进来,否则就无法映射成自己想要的结果,因此我最终得到的Query对象如下:
Query query = this.getCurrentSession().createSQLQuery(sql.toString())
.addScalar("id", StandardBasicTypes.LONG)
.addScalar("programName", StandardBasicTypes.STRING)
.setResultTransformer(Transformers.aliasToBean(GoldenTimeVo.class));
List<GoldenTimeVo> list= query.list();