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
f27ef651
Commit
f27ef651
authored
Jun 23, 2017
by
naan1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
引入动态数据源
parent
36ac9a6b
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
240 additions
and
3 deletions
+240
-3
src/main/java/com/stylefeng/guns/common/persistence/dao/TestMapper.java
+17
-0
src/main/java/com/stylefeng/guns/common/persistence/dao/mapping/TestMapper.xml
+11
-0
src/main/java/com/stylefeng/guns/common/persistence/model/Test.java
+43
-0
src/main/java/com/stylefeng/guns/config/MybatisPlusConfig.java
+35
-2
src/main/java/com/stylefeng/guns/config/properties/DruidProperties.java
+1
-1
src/main/java/com/stylefeng/guns/config/properties/MutiDataSourceProperties.java
+52
-0
src/main/java/com/stylefeng/guns/modular/biz/service/ITestService.java
+18
-0
src/main/java/com/stylefeng/guns/modular/biz/service/impl/TestServiceImpl.java
+33
-0
src/main/resources/application.yml
+7
-0
src/test/java/com/stylefeng/guns/biz/BizTest.java
+23
-0
No files found.
src/main/java/com/stylefeng/guns/common/persistence/dao/TestMapper.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
common
.
persistence
.
dao
;
import
com.stylefeng.guns.common.persistence.model.Test
;
import
com.baomidou.mybatisplus.mapper.BaseMapper
;
/**
* <p>
* Mapper 接口
* </p>
*
* @author stylefeng
* @since 2017-06-23
*/
public
interface
TestMapper
extends
BaseMapper
<
Test
>
{
}
\ No newline at end of file
src/main/java/com/stylefeng/guns/common/persistence/dao/mapping/TestMapper.xml
0 → 100644
View file @
f27ef651
<?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.stylefeng.guns.common.persistence.dao.TestMapper"
>
<!-- 通用查询映射结果 -->
<resultMap
id=
"BaseResultMap"
type=
"com.stylefeng.guns.common.persistence.model.Test"
>
<id
column=
"id"
property=
"id"
/>
<result
column=
"value"
property=
"value"
/>
</resultMap>
</mapper>
src/main/java/com/stylefeng/guns/common/persistence/model/Test.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
common
.
persistence
.
model
;
import
com.baomidou.mybatisplus.activerecord.Model
;
import
java.io.Serializable
;
/**
* <p>
*
* </p>
*
* @author stylefeng
* @since 2017-06-23
*/
public
class
Test
extends
Model
<
Test
>
{
private
static
final
long
serialVersionUID
=
1L
;
private
Integer
id
;
private
String
value
;
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
String
getValue
()
{
return
value
;
}
public
void
setValue
(
String
value
)
{
this
.
value
=
value
;
}
@Override
protected
Serializable
pkVal
()
{
return
this
.
id
;
}
}
src/main/java/com/stylefeng/guns/config/MybatisPlusConfig.java
View file @
f27ef651
...
...
@@ -3,13 +3,18 @@ package com.stylefeng.guns.config;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.baomidou.mybatisplus.enums.DBType
;
import
com.baomidou.mybatisplus.plugins.PaginationInterceptor
;
import
com.stylefeng.guns.common.constant.DSEnum
;
import
com.stylefeng.guns.config.properties.DruidProperties
;
import
com.stylefeng.guns.config.properties.MutiDataSourceProperties
;
import
com.stylefeng.guns.core.mutidatesource.DynamicDataSource
;
import
org.mybatis.spring.annotation.MapperScan
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.transaction.annotation.EnableTransactionManagement
;
import
java.util.HashMap
;
/**
* MybatisPlus配置
*
...
...
@@ -17,13 +22,16 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
* @Date 2017/5/20 21:58
*/
@Configuration
@EnableTransactionManagement
(
order
=
2
)
//由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面
@EnableTransactionManagement
(
order
=
2
)
//由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面
@MapperScan
(
basePackages
=
{
"com.stylefeng.guns.modular.*.dao"
,
"com.stylefeng.guns.common.persistence.dao"
})
public
class
MybatisPlusConfig
{
@Autowired
DruidProperties
druidProperties
;
@Autowired
MutiDataSourceProperties
mutiDataSourceProperties
;
/**
* mybatis-plus分页插件
*/
...
...
@@ -40,7 +48,32 @@ public class MybatisPlusConfig {
@Bean
(
initMethod
=
"init"
)
public
DruidDataSource
dataSource
()
{
DruidDataSource
dataSource
=
new
DruidDataSource
();
druidProperties
.
co
i
nfig
(
dataSource
);
druidProperties
.
config
(
dataSource
);
return
dataSource
;
}
/**
* 多数据源配置
*/
@Bean
(
initMethod
=
"init"
)
public
DruidDataSource
bizDataSource
()
{
DruidDataSource
dataSource
=
new
DruidDataSource
();
druidProperties
.
config
(
dataSource
);
mutiDataSourceProperties
.
config
(
dataSource
);
return
dataSource
;
}
/**
* 动态数据源
*/
@Bean
public
DynamicDataSource
dataSource
(
DruidDataSource
dataSource
,
DruidDataSource
bizDataSource
)
{
DynamicDataSource
dynamicDataSource
=
new
DynamicDataSource
();
HashMap
<
Object
,
Object
>
hashMap
=
new
HashMap
();
hashMap
.
put
(
DSEnum
.
DATA_SOURCE_GUNS
,
dataSource
);
hashMap
.
put
(
DSEnum
.
DATA_SOURCE_BIZ
,
bizDataSource
);
dynamicDataSource
.
setTargetDataSources
(
hashMap
);
dynamicDataSource
.
setDefaultTargetDataSource
(
dataSource
);
return
dynamicDataSource
;
}
}
src/main/java/com/stylefeng/guns/config/properties/DruidProperties.java
View file @
f27ef651
...
...
@@ -50,7 +50,7 @@ public class DruidProperties {
private
String
filters
=
"stat"
;
public
void
co
i
nfig
(
DruidDataSource
dataSource
)
{
public
void
config
(
DruidDataSource
dataSource
)
{
dataSource
.
setUrl
(
url
);
dataSource
.
setUsername
(
username
);
...
...
src/main/java/com/stylefeng/guns/config/properties/MutiDataSourceProperties.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
config
.
properties
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
/**
* 多数据源的配置
*
* @author stylefeng
* @Date 2017/6/23 23:05
*/
@Component
@ConfigurationProperties
(
prefix
=
"biz.datasource"
)
public
class
MutiDataSourceProperties
{
private
String
url
=
"jdbc:mysql://127.0.0.1:3306/biz?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull"
;
private
String
username
=
"root"
;
private
String
password
=
"root"
;
public
void
config
(
DruidDataSource
dataSource
)
{
dataSource
.
setUrl
(
url
);
dataSource
.
setUsername
(
username
);
dataSource
.
setPassword
(
password
);
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
String
getUsername
()
{
return
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
}
src/main/java/com/stylefeng/guns/modular/biz/service/ITestService.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
modular
.
biz
.
service
;
/**
* 测试多数据源的服务
*
* @author fengshuonan
* @date 2017-06-23 23:01
*/
public
interface
ITestService
{
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void
test
();
}
src/main/java/com/stylefeng/guns/modular/biz/service/impl/TestServiceImpl.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
modular
.
biz
.
service
.
impl
;
import
com.alibaba.fastjson.JSON
;
import
com.stylefeng.guns.common.annotion.DataSource
;
import
com.stylefeng.guns.common.constant.DSEnum
;
import
com.stylefeng.guns.common.persistence.dao.TestMapper
;
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.Transactional
;
/**
* 测试服务
*
* @author fengshuonan
* @date 2017-06-23 23:02
*/
@Service
public
class
TestServiceImpl
implements
ITestService
{
@Autowired
TestMapper
testMapper
;
@Override
@Transactional
@DataSource
(
name
=
DSEnum
.
DATA_SOURCE_GUNS
)
public
void
test
()
{
Test
test
=
testMapper
.
selectById
(
1
);
System
.
out
.
println
(
JSON
.
toJSONString
(
test
));
}
}
src/main/resources/application.yml
View file @
f27ef651
...
...
@@ -63,6 +63,13 @@ spring:
password
:
root
filters
:
log4j,wall,mergeStat
#多数据源
biz
:
datasource
:
url
:
jdbc:mysql://127.0.0.1:3306/biz?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
username
:
root
password
:
root
logging
:
level
:
debug
file
:
logs/guns.log
...
...
src/test/java/com/stylefeng/guns/biz/BizTest.java
0 → 100644
View file @
f27ef651
package
com
.
stylefeng
.
guns
.
biz
;
import
com.stylefeng.guns.base.BaseJunit
;
import
com.stylefeng.guns.modular.biz.service.ITestService
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* 业务测试
*
* @author fengshuonan
* @date 2017-06-23 23:12
*/
public
class
BizTest
extends
BaseJunit
{
@Autowired
ITestService
testService
;
@Test
public
void
test
()
{
testService
.
test
();
}
}
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