详见 http://www.iteye.com/topic/1125897
Applicationcontext.xml
<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>
Ibatis.xml代码
<?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>
Storage.xml
<?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>
Datasourceconstant.java
public class DataSourceConstant { public final static String MINE_01 = "MINE_01"; public final static String MINE_02 = "MINE_02"; }
Datasources
package com.core.db;</SPAN> import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DataSources extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceSwitch.getDataSourceType(); } }
Datasourceswitch
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(); } }
StorageDao .java
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); }
Storageimpl.java
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); } }
Storage .java
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; } }
Storageservice.java
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;
}
}