Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
9ea68741
Commit
9ea68741
authored
Jun 24, 2017
by
naan1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
多数据源完善
parent
f27ef651
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
22 deletions
+74
-22
src/main/java/com/stylefeng/guns/config/MybatisPlusConfig.java
+28
-17
src/main/java/com/stylefeng/guns/modular/biz/service/ITestService.java
+19
-1
src/main/java/com/stylefeng/guns/modular/biz/service/impl/TestServiceImpl.java
+22
-3
src/test/java/com/stylefeng/guns/biz/BizTest.java
+5
-1
No files found.
src/main/java/com/stylefeng/guns/config/MybatisPlusConfig.java
View file @
9ea68741
...
@@ -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
,
dataSource
Guns
);
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
(
dataSource
Guns
);
return
dynamicDataSource
;
return
dynamicDataSource
;
}
}
/**
* mybatis-plus分页插件
*/
@Bean
public
PaginationInterceptor
paginationInterceptor
()
{
PaginationInterceptor
paginationInterceptor
=
new
PaginationInterceptor
();
paginationInterceptor
.
setDialectType
(
DBType
.
MYSQL
.
getDb
());
return
paginationInterceptor
;
}
}
}
src/main/java/com/stylefeng/guns/modular/biz/service/ITestService.java
View file @
9ea68741
...
@@ -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
();
}
}
src/main/java/com/stylefeng/guns/modular/biz/service/impl/TestServiceImpl.java
View file @
9ea68741
...
@@ -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
test
Guns
()
{
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;
}
}
}
src/test/java/com/stylefeng/guns/biz/BizTest.java
View file @
9ea68741
...
@@ -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
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment