Spring 다 중 데이터 원본 전환
9089 단어 spring
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 코드
<!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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.