Commit 9ea68741 by naan1993

多数据源完善

parent f27ef651
......@@ -13,6 +13,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.sql.SQLException;
import java.util.HashMap;
/**
......@@ -33,20 +34,9 @@ public class MybatisPlusConfig {
MutiDataSourceProperties mutiDataSourceProperties;
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
return paginationInterceptor;
}
/**
* druid数据库连接池
*/
@Bean(initMethod = "init")
public DruidDataSource dataSource() {
private DruidDataSource dataSourceGuns() {
DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource);
return dataSource;
......@@ -55,25 +45,46 @@ public class MybatisPlusConfig {
/**
* 多数据源配置
*/
@Bean(initMethod = "init")
public DruidDataSource bizDataSource() {
private DruidDataSource bizDataSource() {
DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource);
mutiDataSourceProperties.config(dataSource);
return dataSource;
}
/**
* 动态数据源
*/
@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();
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);
dynamicDataSource.setTargetDataSources(hashMap);
dynamicDataSource.setDefaultTargetDataSource(dataSource);
dynamicDataSource.setDefaultTargetDataSource(dataSourceGuns);
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 {
* @author stylefeng
* @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;
import com.stylefeng.guns.modular.biz.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
......@@ -24,10 +25,28 @@ public class TestServiceImpl implements ITestService {
TestMapper testMapper;
@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)
public void test() {
public void testGuns() {
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 {
@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