Commit b201ae57 authored by frank.xa.zhang's avatar frank.xa.zhang

Merge branch 'dev_eddie' into dev_frank

parents 3ba9ce87 e89d1ad5
...@@ -27,6 +27,15 @@ public class MybatisConfig implements InitializingBean { ...@@ -27,6 +27,15 @@ public class MybatisConfig implements InitializingBean {
return bean; return bean;
} }
@Bean
public static MapperScannerConfigurer vatMapperConfigurer() {
MapperScannerConfigurer bean = new MapperScannerConfigurer();
bean.setMarkerInterface(MyVatMapper.class);
bean.setBasePackage(BASE_PACKAGE);
bean.setSqlSessionTemplateBeanName("dynamicSqlSessionTemplate");
return bean;
}
@Bean @Bean
public static MapperScannerConfigurer userMapperConfigurerMail() { public static MapperScannerConfigurer userMapperConfigurerMail() {
MapperScannerConfigurer bean = new MapperScannerConfigurer(); MapperScannerConfigurer bean = new MapperScannerConfigurer();
......
package pwc.taxtech.atms.common.datasource;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
@Aspect
public class AopShardingHolder {
private static Logger logger = LoggerFactory.getLogger(AopShardingHolder.class);
private static final String DATABASE_NAME_HEADER = "from";
private static final String DATABASE_NAME_SPLIT = "@";
@Autowired
HttpServletRequest request;
@Pointcut("execution(public * pwc.taxtech.atms.controller.*.*(..))")
public void pointCut() {
// 定义Pointcut 此方法不能有返回值,该方法只是一个标示
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
try {
String header = request.getHeader(DATABASE_NAME_HEADER);
if (StringUtils.isNotBlank(header)) {
if (logger.isDebugEnabled()) {
logger.debug("aop set datasource:{}", header);
}
String[] arr = StringUtils.split(header, DATABASE_NAME_SPLIT);
String name = arr[0];
if (name.length() > 0 && !StringUtils.contains(name, DATABASE_NAME_SPLIT)) {
ShardingContextHolder.setDataSourceKey(name);
}
}
} catch (Exception e) {
logger.error("aop set database error.", e);
}
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
}
@AfterThrowing(pointcut = "pointCut()", throwing = "error")
public void afterThrowing(JoinPoint joinPoint, Throwable error) {
}
}
\ No newline at end of file
package pwc.taxtech.atms.common.datasource; package pwc.taxtech.atms.common.datasource;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.beust.jcommander.ParameterException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.KeyedPooledObjectFactory; import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObject;
...@@ -19,6 +20,9 @@ public class DataSourceFactory implements KeyedPooledObjectFactory<String, Druid ...@@ -19,6 +20,9 @@ public class DataSourceFactory implements KeyedPooledObjectFactory<String, Druid
@Override @Override
public PooledObject<DruidDataSource> makeObject(String s) throws Exception { public PooledObject<DruidDataSource> makeObject(String s) throws Exception {
if (StringUtils.isBlank(s)) {
throw new ParameterException("empty database name.");
}
DruidDataSource dynamicDataSource = new DruidDataSource(); DruidDataSource dynamicDataSource = new DruidDataSource();
String url = StringUtils.replace(dataSourceConfig.getUrl(), SYMBOL, s); String url = StringUtils.replace(dataSourceConfig.getUrl(), SYMBOL, s);
dynamicDataSource.setUrl(url); dynamicDataSource.setUrl(url);
......
...@@ -42,6 +42,8 @@ public class DataSourcePoolService extends AbstractRoutingDataSource { ...@@ -42,6 +42,8 @@ public class DataSourcePoolService extends AbstractRoutingDataSource {
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig(); GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setTestOnBorrow(true); poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true); poolConfig.setTestOnReturn(true);
poolConfig.setMaxTotalPerKey(1);
poolConfig.setMaxIdlePerKey(1);
dataSourcePool = new GenericKeyedObjectPool<>(new DataSourceFactory(config), poolConfig); dataSourcePool = new GenericKeyedObjectPool<>(new DataSourceFactory(config), poolConfig);
this.setTargetDataSources(Maps.newHashMap()); this.setTargetDataSources(Maps.newHashMap());
} }
...@@ -52,7 +54,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource { ...@@ -52,7 +54,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource {
} }
@Override @Override
protected DataSource determineTargetDataSource() { protected synchronized DataSource determineTargetDataSource() {
try { try {
String key = ShardingContextHolder.getDataSourceKey(); String key = ShardingContextHolder.getDataSourceKey();
DruidDataSource dataSource = dataSourcePool.borrowObject(key); DruidDataSource dataSource = dataSourcePool.borrowObject(key);
...@@ -81,7 +83,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource { ...@@ -81,7 +83,7 @@ public class DataSourcePoolService extends AbstractRoutingDataSource {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource); bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver() bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml")); .getResources("classpath*:pwc/taxtech/atms/vatDao/*Mapper.xml"));
return bean.getObject(); return bean.getObject();
} }
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
<property name="patterns"> <property name="patterns">
<list> <list>
<value>pwc.taxtech.atms.dao.*</value> <value>pwc.taxtech.atms.dao.*</value>
<value>pwc.taxtech.atms.vatDao.*</value>
</list> </list>
</property> </property>
</bean> </bean>
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
<property name="configLocation" value="classpath:sqlMapConfig.xml" /> <property name="configLocation" value="classpath:sqlMapConfig.xml" />
<property name="mapperLocations"> <property name="mapperLocations">
<array> <array>
<value>classpath:pwc/taxtech/atms/**/*Mapper.xml</value> <value>classpath:pwc/taxtech/atms/dao/*Mapper.xml</value>
</array> </array>
</property> </property>
</bean> </bean>
......
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
<listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param> <context-param>
<param-name>contextConfigLocation</param-name> <param-name>contextConfigLocation</param-name>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment