Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gjjs-bd-common
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
s_guodong
gjjs-bd-common
Commits
0a618b6d
Commit
0a618b6d
authored
Jan 13, 2023
by
gechengyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
整合多数据基础版本,待完善。
parent
e2d1815b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
187 additions
and
0 deletions
+187
-0
MyBatisDaoSession.java
...in/java/com/brilliance/mda/support/MyBatisDaoSession.java
+0
-0
mybatis.java
...ess/src/main/java/com/brilliance/mda/support/mybatis.java
+4
-0
DataSourceUtil.java
...src/main/java/com/brilliance/mda/util/DataSourceUtil.java
+45
-0
DataSourceConfig.java
...c/main/java/com/ceb/gjjs/mda/config/DataSourceConfig.java
+125
-0
DynamicDataSource.java
.../main/java/com/ceb/gjjs/mda/config/DynamicDataSource.java
+13
-0
No files found.
gjjs-bd-business/src/main/java/com/brilliance/mda/support/MyBatisDaoSession.java
0 → 100644
View file @
0a618b6d
This diff is collapsed.
Click to expand it.
gjjs-bd-business/src/main/java/com/brilliance/mda/support/mybatis.java
0 → 100644
View file @
0a618b6d
package
com
.
brilliance
.
mda
.
support
;
public
class
mybatis
{
}
gjjs-bd-business/src/main/java/com/brilliance/mda/util/DataSourceUtil.java
0 → 100644
View file @
0a618b6d
package
com
.
brilliance
.
mda
.
util
;
import
com.brilliance.mda.runtime.mda.util.MdaUtils
;
/**
* 数据源切换工具
*/
public
class
DataSourceUtil
{
/**
* 默认数据源
*/
public
static
final
String
DEFAULT_DS
=
"datasource"
;
/**
* 数据源属于一个公共的资源
* 采用可以保证在多线程情况下线程隔离
*/
private
static
final
ThreadLocal
<
String
>
contextHolder
=
new
ThreadLocal
<>();
/**
* 设置数据源名
* @param dbType
*/
public
static
void
setDB
(
String
dbType
)
{
contextHolder
.
set
(
dbType
);
}
/**
* 获取数据源名
* @return
*/
public
static
String
getDB
()
{
String
dbName
=
(
contextHolder
.
get
())
;
if
(
MdaUtils
.
isEmpty
(
dbName
))
return
DEFAULT_DS
;
return
dbName
;
}
/**
* 清除数据源名
*/
public
static
void
clearDB
()
{
contextHolder
.
remove
();
}
}
gjjs-bd-business/src/main/java/com/ceb/gjjs/mda/config/DataSourceConfig.java
0 → 100644
View file @
0a618b6d
package
com
.
ceb
.
gjjs
.
mda
.
config
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.mybatis.spring.SqlSessionFactoryBean
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.jdbc.DataSourceBuilder
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.core.io.support.PathMatchingResourcePatternResolver
;
import
org.springframework.jdbc.datasource.DataSourceTransactionManager
;
import
org.springframework.transaction.PlatformTransactionManager
;
import
javax.sql.DataSource
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 数据库配置
*/
@Configuration
public
class
DataSourceConfig
{
/**
* 数据源1
* spring.datasource.db1:application.properteis中对应属性的前缀
* @return
*/
@Bean
(
name
=
"dataSource"
)
@ConfigurationProperties
(
prefix
=
"spring.datasource.druid"
)
// @Bean(name = "db1")
// @ConfigurationProperties(prefix = "spring.datasource.db1")
public
DataSource
dataSourceOne
()
{
return
DataSourceBuilder
.
create
().
build
();
}
/**
* 数据源2
* spring.datasource.db2:application.properteis中对应属性的前缀
* @return
*/
//@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2")
@Bean
(
name
=
"dataSource2"
)
@ConfigurationProperties
(
prefix
=
"spring.datasource2"
)
public
DataSource
dataSourceTwo
()
{
return
DataSourceBuilder
.
create
().
build
();
}
/**
* 动态数据源: 通过AOP在不同数据源之间动态切换
* @return
*/
@Primary
@Bean
(
name
=
"dynamicDataSource"
)
public
DataSource
dynamicDataSource
()
{
DynamicDataSource
dynamicDataSource
=
new
DynamicDataSource
();
// 默认数据源
dynamicDataSource
.
setDefaultTargetDataSource
(
dataSourceOne
());
// 配置多数据源
Map
<
Object
,
Object
>
dsMap
=
new
HashMap
<>();
dsMap
.
put
(
"dataSource"
,
dataSourceOne
());
dsMap
.
put
(
"dataSource2"
,
dataSourceTwo
());
dynamicDataSource
.
setTargetDataSources
(
dsMap
);
return
dynamicDataSource
;
}
/**
* 配置多数据源后IOC中存在多个数据源了,事务管理器需要重新配置,不然器不知道选择哪个数据源
* 事务管理器此时管理的数据源将是动态数据源dynamicDataSource
* 配置@Transactional注解
* @return
*/
@Bean
public
PlatformTransactionManager
transactionManager
()
{
return
new
DataSourceTransactionManager
(
dynamicDataSource
());
}
@Bean
(
name
=
"sqlSessionFactory1"
)
@Primary
public
SqlSessionFactory
getSqlSessionFactory1
(
@Qualifier
(
"dataSource"
)
DataSource
dataSource
)
throws
Exception
{
SqlSessionFactoryBean
sqlfb
=
new
SqlSessionFactoryBean
();
sqlfb
.
setDataSource
(
dataSource
);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb
.
setMapperLocations
(
new
PathMatchingResourcePatternResolver
().
getResources
(
"classpath:META-INF/orm/mda/*.xml"
));
return
sqlfb
.
getObject
();
}
@Bean
(
name
=
"transactionManager"
)
@Primary
public
DataSourceTransactionManager
getTransactionManager1
(
@Qualifier
(
"dataSource"
)
DataSource
dataSource
)
{
return
new
DataSourceTransactionManager
(
dataSource
);
}
@Bean
(
name
=
"sqlSessionTemplate1"
)
@Primary
public
SqlSessionTemplate
getSqlSessionTemplate1
(
@Qualifier
(
"sqlSessionFactory1"
)
SqlSessionFactory
sqlSessionFactory
)
throws
Exception
{
return
new
SqlSessionTemplate
(
sqlSessionFactory
);
}
//-----------------------------------------
@Bean
(
name
=
"sqlSessionFactory2"
)
public
SqlSessionFactory
getSqlSessionFactory2
(
@Qualifier
(
"dataSource2"
)
DataSource
dataSource
)
throws
Exception
{
SqlSessionFactoryBean
sqlfb
=
new
SqlSessionFactoryBean
();
sqlfb
.
setDataSource
(
dataSource
);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb
.
setMapperLocations
(
new
PathMatchingResourcePatternResolver
().
getResources
(
"classpath:META-INF/orm/mda/*.xml"
));
return
sqlfb
.
getObject
();
}
@Bean
(
name
=
"transactionManager2"
)
public
DataSourceTransactionManager
getTransactionManager2
(
@Qualifier
(
"dataSource2"
)
DataSource
dataSource
)
{
return
new
DataSourceTransactionManager
(
dataSource
);
}
@Bean
(
name
=
"sqlSessionTemplate2"
)
public
SqlSessionTemplate
getSqlSessionTemplate2
(
@Qualifier
(
"sqlSessionFactory2"
)
SqlSessionFactory
sqlSessionFactory
)
throws
Exception
{
return
new
SqlSessionTemplate
(
sqlSessionFactory
);
}
}
gjjs-bd-business/src/main/java/com/ceb/gjjs/mda/config/DynamicDataSource.java
0 → 100644
View file @
0a618b6d
package
com
.
ceb
.
gjjs
.
mda
.
config
;
import
com.brilliance.mda.util.DataSourceUtil
;
import
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
;
/**
* 动态数据源类
*/
public
class
DynamicDataSource
extends
AbstractRoutingDataSource
{
@Override
protected
Object
determineCurrentLookupKey
()
{
return
DataSourceUtil
.
getDB
();
}
}
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