Springmvcマルチデータソース同時使用
3680 ワード
spring xml
<bean id="dataSource_role" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${url_role}" />
<property name="username" value="${username_role}" />
<property name="password" value="${password_role}" />
<property name="driverClassName" value="${driverClassName_role}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<bean id="dataSource_main" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${url_main}" />
<property name="username" value="${username_main}" />
<property name="password" value="${password_main}" />
<property name="driverClassName" value="${driverClassName_main}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<bean id="dataSource_score" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${url_score}" />
<property name="username" value="${username_score}" />
<property name="password" value="${password_score}" />
<property name="driverClassName" value="${driverClassName_score}" />
<property name="maxActive" value="${maxActive}" />
<property name="initialSize" value="${initialSize}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<!-- ======================== mybatis ========================== -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<property name="dataSource" ref="dataSource_score" />
<property name="mapperLocations" value="classpath*:config/mybatis/**/*.xml" />
</bean>
<bean id="_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<property name="dataSource" ref="dataSource_role" />
<property name="mapperLocations" value="classpath*:config/mybatis/**/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="_sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="_sqlSessionFactory" />
</bean>
使用
public SqlSession getSqlSession() {
SqlSessionTemplate sqlSession =
(SqlSessionTemplate) SpringContextUtil.getBean("_sqlSession");
return sqlSession;
}
public void save(String key, Object object) {
getSqlSession().insert(key, object);
}