Commit 9ea68741 by naan1993

多数据源完善

parent f27ef651
...@@ -13,6 +13,7 @@ import org.springframework.context.annotation.Bean; ...@@ -13,6 +13,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.sql.SQLException;
import java.util.HashMap; import java.util.HashMap;
/** /**
...@@ -33,20 +34,9 @@ public class MybatisPlusConfig { ...@@ -33,20 +34,9 @@ public class MybatisPlusConfig {
MutiDataSourceProperties mutiDataSourceProperties; MutiDataSourceProperties mutiDataSourceProperties;
/** /**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
return paginationInterceptor;
}
/**
* druid数据库连接池 * druid数据库连接池
*/ */
@Bean(initMethod = "init") private DruidDataSource dataSourceGuns() {
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource); druidProperties.config(dataSource);
return dataSource; return dataSource;
...@@ -55,25 +45,46 @@ public class MybatisPlusConfig { ...@@ -55,25 +45,46 @@ public class MybatisPlusConfig {
/** /**
* 多数据源配置 * 多数据源配置
*/ */
@Bean(initMethod = "init") private DruidDataSource bizDataSource() {
public DruidDataSource bizDataSource() {
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource); druidProperties.config(dataSource);
mutiDataSourceProperties.config(dataSource); mutiDataSourceProperties.config(dataSource);
return dataSource; return dataSource;
} }
/** /**
* 动态数据源 * 动态数据源
*/ */
@Bean @Bean
public DynamicDataSource dataSource(DruidDataSource dataSource, DruidDataSource bizDataSource) { public DynamicDataSource dataSource() {
DruidDataSource dataSourceGuns = dataSourceGuns();
DruidDataSource bizDataSource = bizDataSource();
try {
dataSourceGuns.init();
bizDataSource.init();
}catch (SQLException sql){
sql.printStackTrace();
}
DynamicDataSource dynamicDataSource = new DynamicDataSource(); DynamicDataSource dynamicDataSource = new DynamicDataSource();
HashMap<Object, Object> hashMap = new HashMap(); HashMap<Object, Object> hashMap = new HashMap();
hashMap.put(DSEnum.DATA_SOURCE_GUNS, dataSource); hashMap.put(DSEnum.DATA_SOURCE_GUNS, dataSourceGuns);
hashMap.put(DSEnum.DATA_SOURCE_BIZ, bizDataSource); hashMap.put(DSEnum.DATA_SOURCE_BIZ, bizDataSource);
dynamicDataSource.setTargetDataSources(hashMap); dynamicDataSource.setTargetDataSources(hashMap);
dynamicDataSource.setDefaultTargetDataSource(dataSource); dynamicDataSource.setDefaultTargetDataSource(dataSourceGuns);
return dynamicDataSource; return dynamicDataSource;
} }
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
return paginationInterceptor;
}
} }
...@@ -14,5 +14,23 @@ public interface ITestService { ...@@ -14,5 +14,23 @@ public interface ITestService {
* @author stylefeng * @author stylefeng
* @Date 2017/6/23 23:02 * @Date 2017/6/23 23:02
*/ */
void test(); void testBiz();
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void testGuns();
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void testAll();
} }
...@@ -8,6 +8,7 @@ import com.stylefeng.guns.common.persistence.model.Test; ...@@ -8,6 +8,7 @@ import com.stylefeng.guns.common.persistence.model.Test;
import com.stylefeng.guns.modular.biz.service.ITestService; import com.stylefeng.guns.modular.biz.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
...@@ -24,10 +25,28 @@ public class TestServiceImpl implements ITestService { ...@@ -24,10 +25,28 @@ public class TestServiceImpl implements ITestService {
TestMapper testMapper; TestMapper testMapper;
@Override @Override
@Transactional @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
public void testBiz() {
Test test = testMapper.selectById(1);
test.setId(22);
test.insert();
}
@Override
@DataSource(name = DSEnum.DATA_SOURCE_GUNS) @DataSource(name = DSEnum.DATA_SOURCE_GUNS)
public void test() { public void testGuns() {
Test test = testMapper.selectById(1); Test test = testMapper.selectById(1);
System.out.println(JSON.toJSONString(test)); test.setId(33);
test.insert();
} }
@Override
@Transactional
public void testAll() {
testBiz();
testGuns();
//int i = 1 / 0;
}
} }
...@@ -18,6 +18,10 @@ public class BizTest extends BaseJunit { ...@@ -18,6 +18,10 @@ public class BizTest extends BaseJunit {
@Test @Test
public void test() { public void test() {
testService.test(); //testService.testGuns();
//testService.testBiz();
testService.testAll();
} }
} }
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