欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis+Proxool+Spring多数据源切换

程序员文章站 2022-06-07 08:26:28
...

话不多说直接上代码,用者自提,不喜勿喷,3Q

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-autowire="byName">
	  
	
	<!-- 数据源 -->
	<bean id="parentDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
	 	<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>  
	 	<property name="trace" value="true"/>
	</bean> 
	
	<bean id="sc" parent="parentDataSource">  
        <property name="driverUrl" value="jdbc:oracle:thin:@172.16.6.220:1521:cmes"/>  
        <property name="user" value="meams"/>  
        <property name="password" value="xwcmes"/>  
    </bean>  
      
    <bean id="lz" parent="parentDataSource">  
        <property name="driverUrl" value="jdbc:oracle:thin:@172.16.6.220:1521:cmes"/>  
        <property name="user" value="geams"/>  
        <property name="password" value="geams"/>  
    </bean>  

	<bean id="dataSource" class="com.core.db.DataSources">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry value-ref="sc" key="MINE_01"/>
				<entry value-ref="lz" key="MINE_02"/>
			</map>
		</property>
	</bean>
	
	<!-- 事务  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 自定义exception处理 -->
	<bean id="exceptionResolver" class="com.core.framework.CommonExceptionHandler"/>
	<!-- 添加Mybatis支持 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:config/ibatis-config.xml"/>
		<!-- 无需配置ibatis-config.xml中的mapper 由spring自动扫描加入 -->
		<property name="mapperLocations" value="classpath:com/dhcc/exchange/ibatis/*.xml"/> 
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
	        <constructor-arg index="0" ref="sqlSessionFactory" />  
	</bean>  
	
	<!-- 自动将映射接口转换为bean -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" 
		p:basePackage="com.dhcc.exchange.*.dao" 
		p:sqlSessionFactory-ref="sqlSessionFactory">
	</bean>
	
	<bean id="storageService" class="com.dhcc.exchange.storage.service.StorageService" />
</beans>  

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC 
	"-//mybatis.org//DTD Config 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
	
<configuration>
	<typeAliases>
		<typeAlias alias="EQUI_STORAGE" type="com.dhcc.exchange.storage.model.Storage" />
	</typeAliases>
</configuration>		
			

 

 

<?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.dhcc.exchange.storage.dao.StorageDao">

	<resultMap type="com.dhcc.exchange.storage.model.Storage" id="EQUI_STORAGE_LIST"/>

	<select id="queryStorageList" resultMap="EQUI_STORAGE_LIST" parameterType="EQUI_STORAGE">
		select id,storage_name from t_equi_storage t
	</select>

</mapper>
 

 

 

 

 package com.core.db;

public class DataSourceConstant {
	public final static String MINE_01 = "MINE_01";
	public final static String MINE_02 = "MINE_02";
}

 

 package com.core.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DataSources extends AbstractRoutingDataSource {
	@Override
	protected Object determineCurrentLookupKey() {
		 return DataSourceSwitch.getDataSourceType();  
	}
}

 

 package com.core.db;

public class DataSourceSwitch{
	private static final ThreadLocal<String> contextHolder=new ThreadLocal<String>();
	
	public static void setDataSourceType(String dataSourceType){
		contextHolder.set(dataSourceType);
	}
	
	public static String getDataSourceType(){
		return (String) contextHolder.get();
	}
	
	public static void clearDataSourceType(){
		contextHolder.remove();
	}
}

 

package com.core.db;

import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.DataSourceFactory;
import org.logicalcobwebs.proxool.ProxoolDataSource;

public class ProxoolDataSourceFactory implements DataSourceFactory {

    private ProxoolDataSource dataSource;    
     
    public DataSource getDataSource() {   
        return dataSource;   
    }   
  
    /**  
     * 此方法中实现对ProxoolDataSource类中相关属性的设置  
     * house-keeping-sleep-time
     * 线程保持休眠时间,house keeper负责检查所有连接的状态,并测试是否需要销毁或者创建,默认时间为30秒
     * house-keeping-test-sql
     * 如果house keep 线程发现空闲的连接,他会测试使用这个sql进行测试,这个语句应该快速的被执行。像查询日期的sql语句。
     * maximum-active-time
     * 最大线程活动时间。
     * maximum-connection-count
     * 数据库最大连接数(默认值为15)
     * maximum-connection-count
     * 一个连接存在的最长保持活动的时间。默认值是4小时,单位是毫秒
     * overload-without-refusal-lifetime
     * 这个帮助我们确定连接池的状态。如果在这个时间阀值内(单位为毫秒)拒绝了一个连接,就认为是过载了。默认时间60秒。
     */  
    public void initialize(Map<String,String> map) {   
        dataSource = new ProxoolDataSource();   
        dataSource.setDriver(map.get("driver"));   
        dataSource.setDriverUrl(map.get("driverUrl"));   
        dataSource.setUser(map.get("user"));   
        dataSource.setPassword(map.get("password"));   
        dataSource.setAlias("alias"); 
          
        //线程保持休眠时间,house keeper负责检查所有连接的状态,并测试是否需要销毁或者创建,默认时间为30秒
        if(map.containsKey("house-keeping-sleep-time")){
            dataSource.setHouseKeepingSleepTime(Integer.parseInt(map.get("house-keeping-sleep-time").toString()));
        }
        //如果house keep 线程发现空闲的连接,他会测试使用这个sql进行测试,这个语句应该快速的被执行。像查询日期的sql语句。
        if(map.containsKey("house-keeping-test-sql")){
            dataSource.setHouseKeepingTestSql(map.get("house-keeping-test-sql").toString());
        }
        //最大线程活动时间。
        //如果housekeeper 遇到一个线程活动时间超过定义的时间,将会终止这个线程。
        //所以你需要设置这个时间大于预计最慢响应的时间(默认时间是5分钟)。
        if(map.containsKey("maximum-active-time")){
            dataSource.setMaximumActiveTime(Integer.parseInt(map.get("maximum-active-time").toString()));
        }
        //数据库最大连接数(默认值为15)
        if(map.containsKey("maximum-connection-count")){
            dataSource.setMaximumConnectionCount(Integer.parseInt(map.get("maximum-connection-count").toString()));
        }
        //一个连接存在的最长保持活动的时间。默认值是4小时,单位是毫秒。
        if(map.containsKey("maximum-connection-lifetime")){
            dataSource.setMaximumConnectionLifetime(Integer.parseInt(map.get("maximum-connection-lifetime").toString()));
        }
        //最小连接保持打开的个数,不管是否需要,默认值是5个。
        if(map.containsKey("minimum-connection-count")){
            dataSource.setMaximumConnectionLifetime(Integer.parseInt(map.get("minimum-connection-count").toString()));
        }
        //这个帮助我们确定连接池的状态。如果在这个时间阀值内(单位为毫秒)拒绝了一个连接,就认为是过载了。默认时间60秒。
        if(map.containsKey("overload-without-refusal-lifetime")){
            dataSource.setMaximumConnectionLifetime(Integer.parseInt(map.get("overload-without-refusal-lifetime").toString()));
        }      
    }

	@Override
	public void setProperties(Properties arg0) {
		
	}   
}

 
 

 

 

package com.dhcc.exchange.storage.dao;

import com.dhcc.exchange.storage.model.Storage;
import java.util.List;

public abstract interface StorageDao {
	public abstract List<Storage> queryStorageList(Storage paramVO);
}
 

 

 

 

package com.dhcc.exchange.storage.dao.impl;

import java.util.List;

import com.core.framework.BaseDao;
import com.dhcc.exchange.storage.dao.StorageDao;
import com.dhcc.exchange.storage.model.Storage;

public class StorageImpl extends BaseDao implements StorageDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Storage> queryStorageList(Storage paramVO) {
		return this.getSqlSession().selectList("queryStorageList", paramVO);
	}

}

 

 

 

package com.dhcc.exchange.storage.model;

public class Storage {
	
	private String ID;
	private String storage_name;
	
	public String getID() {
		return ID;
	}
	public void setID(String ID) {
		this.ID = ID;
	}
	
	public String getStorage_name() {
		return storage_name;
	}
	public void setStorage_name(String storage_name) {
		this.storage_name = storage_name;
	}
}
 

 

 

 

package com.dhcc.exchange.storage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.db.DataSourceConstant;
import com.core.db.DataSourceSwitch;
import com.core.framework.BaseService;
import com.core.framework.ServiceException;
import com.core.util.Tracer;
import com.dhcc.exchange.storage.dao.StorageDao;
import com.dhcc.exchange.storage.model.Storage;

@Transactional
@Service
public class StorageService extends BaseService {

	@Autowired
	private StorageDao storageDao;
	public String getStroage() throws ServiceException{
		String returnVal = "";
		try {
			DataSourceSwitch.setDataSourceType(DataSourceConstant.MINE_01);
			List<Storage> storages = storageDao.queryStorageList(new Storage());
			returnVal = binder.toJson(storages);
			Tracer.trace(returnVal);
			log.debug("[StorageService.getStroage]JSON IS:"+returnVal);
		} catch (Exception e) {
			throw new ServiceException("查询菜单列表失败", e);
		}
		return returnVal;
	}
}
 

上一篇: 为Gradle安装Tomcat插件

下一篇: 排序