1.jeesite.properties 添加数据源信息,(url2,username2,pawwword2)
- #mysql database setting
- jdbc.type=mysql
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
- #mysql2 database setting
- #jdbc.type=mysql
- #jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url2=jdbc:mysql://218.28.123.82:3306/stkcentervideosys?useUnicode=true&characterEncoding=utf-8
- jdbc.username2=root
- jdbc.password2=root
2.修改spring-context.xml(src/main/resources/),3处需要修改/添加
第一处(spring-context.xml):
- <!-- 第一个数据源配置, 使用 BoneCP 数据库连接池 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
- <property name="driverClassName" value="${jdbc.driver}" />
- <!-- 基本属性 url、user、password -->
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- <!-- 配置初始化大小、最小、最大 -->
- <property name="initialSize" value="${jdbc.pool.init}" />
- <property name="minIdle" value="${jdbc.pool.minIdle}" />
- <property name="maxActive" value="${jdbc.pool.maxActive}" />
- <!-- 配置获取连接等待超时的时间 -->
- <property name="maxWait" value="60000" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <property name="validationQuery" value="${jdbc.testSql}" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
- <property name="poolPreparedStatements" value="true" />
- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
- <!-- 配置监控统计拦截的filters -->
- <property name="filters" value="stat" />
- </bean>
- <!-- 第二个数据源配置, 使用 BoneCP 数据库连接池 -->
- <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
- <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
- <property name="driverClassName" value="${jdbc.driver}" />
- <!-- 基本属性 url、user、password -->
- <property name="url" value="${jdbc.url2}" />
- <property name="username" value="${jdbc.username2}" />
- <property name="password" value="${jdbc.password2}" />
- <!-- 配置初始化大小、最小、最大 -->
- <property name="initialSize" value="${jdbc.pool.init}" />
- <property name="minIdle" value="${jdbc.pool.minIdle}" />
- <property name="maxActive" value="${jdbc.pool.maxActive}" />
- <!-- 配置获取连接等待超时的时间 -->
- <property name="maxWait" value="60000" />
- <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="60000" />
- <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <property name="validationQuery" value="${jdbc.testSql}" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
- <property name="poolPreparedStatements" value="true" />
- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
- <!-- 配置监控统计拦截的filters -->
- <property name="filters" value="stat" />
- </bean>
- <!-- 动态数据源 -->
- <bean id="dynamicDataSource" class="com.thinkgem.jeesite.common.db.DynamicDataSource">
- <property name="defaultTargetDataSource" ref="dataSource"/>
- <property name="targetDataSources">
- <map>
- <entry key="dataSource" value-ref="dataSource"/>
- <entry key="dataSource2" value-ref="dataSource2"/>
- </map>
- </property>
- </bean>
第二处(spring-context.xml):修改为dynamicDataSource
- <!-- MyBatis begin -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dynamicDataSource"/>
- <property name="typeAliasesPackage" value="com.thinkgem.jeesite"/>
- <property name="typeAliasesSuperType" value="com.thinkgem.jeesite.common.persistence.BaseEntity"/>
- <property name="mapperLocations" value="classpath:/mappings/**/*.xml"/>
- <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
- </bean>
第三处(spring-context.xml):修改为dynamicDataSource
- <!-- 定义事务 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dynamicDataSource" />
- </bean>
3.添加DynamicDataSource.java (com.thinkgem.jeesite.common.db.DynamicDataSource.java)
- package com.thinkgem.jeesite.common.db;
- import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
- /**
- * Mysql 多数据源切换
- *
- * @author sa
- * @version V1.0
- * @Description:
- * @date 2015/10/09
- */
- public class DynamicDataSource extends AbstractRoutingDataSource {
- private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
- /**
- *
- * @author sa
- * @date 2012-5-18 下午4:06:44
- * @return the currentLookupKey
- */
- public static String getCurrentLookupKey() {
- return (String) contextHolder.get();
- }
- /**
- *
- * @author sa
- * @date 2012-5-18 下午4:06:44
- * @param currentLookupKey
- * the currentLookupKey to set
- */
- public static void setCurrentLookupKey(String currentLookupKey) {
- contextHolder.set(currentLookupKey);
- }
- /*
- * (non-Javadoc)
- *
- * @see
- * org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#
- * determineCurrentLookupKey()
- */
- @Override
- protected Object determineCurrentLookupKey() {
- return getCurrentLookupKey();
- }
- }
4.在Controller中切换(在service层切换不管用,还没查原因)
- //切换数据源dataSource2,默认数据源dataSource
- DynamicDataSource.setCurrentLookupKey("dataSource2");
- List list = stkScenerySpotService.findAll();
- model.addAttribute("list",list);
- DynamicDataSource.setCurrentLookupKey("dataSource");
注:
要对切换的数据源dataSource2 中的表手动写映射和三层
实体:com.thinkgem.jeesite.modules.cms.entity.StkScenerySpot.java
service:com.thinkgem.jeesite.modules.cms.service.StkScenerySpotService.java
mapper:StkScenerySpotDao.xml (src/main/resources/modules/cms)
我只查所有,所以sql很简单
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.thinkgem.jeesite.modules.cms.dao.StkScenerySpotDao">
- <sql id="stkScenerySpotColumns">
- a.Spot_Id AS "id",
- a.CamId AS "camId",
- a.Name AS "name",
- a.Comment AS "commnet"
- </sql>
- <sql id="stkScenerySpotJoins">
- </sql>
- <select id="findList" resultType="StkScenerySpot">
- SELECT
- <include refid="stkScenerySpotColumns"/>
- FROM stk_scenery_spot a
- <include refid="stkScenerySpotJoins"/>
- <where>
- 1 = 1
- </where>
- <choose>
- <otherwise>
- ORDER BY a.Spot_Id DESC
- </otherwise>
- </choose>
- </select>
- </mapper>