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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
648 additions
and
0 deletions
+648
-0
MyBatisDaoSession.java
...in/java/com/brilliance/mda/support/MyBatisDaoSession.java
+461
-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
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package
com
.
brilliance
.
mda
.
support
;
import
com.brilliance.mda.runtime.mda.IDaoSession
;
import
com.brilliance.mda.runtime.mda.IModule
;
import
com.brilliance.mda.runtime.mda.IModuleList
;
import
com.brilliance.mda.runtime.mda.IStream
;
import
com.brilliance.mda.runtime.mda.driver.MdaDriver
;
import
com.brilliance.mda.runtime.mda.driver.MdaEnv
;
import
com.brilliance.mda.runtime.mda.impl.AbstractDaoSession
;
import
com.brilliance.mda.runtime.mda.impl.Argument
;
import
com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType
;
import
com.brilliance.mda.support.mybatis.count.CounterService
;
import
com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper
;
import
com.brilliance.mda.util.DataSourceUtil
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.io.Serializable
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
static
com
.
brilliance
.
mda
.
support
.
mybatis
.
MybatisArgumentAdapter
.
HOLDER_SQL
;
@Component
public
class
MyBatisDaoSession
extends
AbstractDaoSession
implements
IDaoSession
{
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
MyBatisDaoSession
.
class
);
private
static
ThreadLocal
<
ResultSet
>
resultLocal
=
new
ThreadLocal
();
private
static
final
String
SQLID_INSERT
=
"insert"
;
private
static
final
String
SQLID_UPDATE
=
"updateByPrimaryKey"
;
private
static
final
String
SQLID_DELETE
=
"deleteByPrimaryKey"
;
private
static
final
String
SQLID_DELBYIDS
=
"deleteByIds"
;
private
static
final
String
SQLID_DYNCREAD
=
"dyncRead"
;
private
static
final
String
SQLID_DYNCDEL
=
"dyncDelete"
;
private
static
ThreadLocal
<
String
>
dbLocal
=
new
ThreadLocal
<>();
private
static
final
String
DATASOURCE_DB1
=
"dataSource"
;
private
static
final
String
DATASOURCE_DB2
=
"dataSource2"
;
@Autowired
CounterService
counter
;
@Autowired
DbExecuteMapper
dbExecuteMapper
;
@Autowired
@Resource
(
name
=
"sqlSessionTemplate1"
)
SqlSessionTemplate
sqlSessionTemplate1
;
@Autowired
@Resource
(
name
=
"sqlSessionTemplate2"
)
SqlSessionTemplate
sqlSessionTemplate2
;
private
String
namespacePackage
=
"com.ceb.gjjs.mapper"
;
private
String
sqlIdTemplate
=
"%s.%sMapper.%s"
;
public
MyBatisDaoSession
()
{
}
public
void
setNamespacePackage
(
String
namespacePackage
)
{
this
.
namespacePackage
=
namespacePackage
;
}
public
void
setSqlIdTemplate
(
String
sqlIdTemplate
)
{
this
.
sqlIdTemplate
=
sqlIdTemplate
;
}
public
int
dbCounter
(
String
seqname
)
{
return
this
.
counter
.
dbCounter
(
seqname
);
}
public
int
dbInsert
(
IModule
module
)
{
String
sqlId
=
this
.
getSqlId
(
module
.
getClass
(),
"insert"
);
return
this
.
executeUpdate
(
sqlId
,
module
);
}
public
int
dbUpdate
(
IModule
module
,
Object
v1
,
String
whereCol1
)
{
return
0
;
}
public
int
dbUpdate
(
IModule
module
,
Object
v1
,
String
whereCol1
,
Object
v2
,
String
whereCol2
)
{
return
0
;
}
public
<
T
extends
IModule
>
int
dbUpdate
(
T
module
)
{
String
sqlId
=
this
.
getSqlId
(
module
.
getClass
(),
"updateByPrimaryKey"
);
return
this
.
executeUpdate
(
sqlId
,
module
);
}
public
int
dbDelete
(
IModule
module
)
{
String
sqlId
=
this
.
getSqlId
(
module
.
getClass
(),
"deleteByPrimaryKey"
);
return
this
.
executeUpdate
(
sqlId
,
module
);
}
public
int
dbDelete
(
IModule
m
,
Argument
<?
extends
Object
>...
args
)
{
Class
clazz
=
m
.
getClass
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
AdaType
.
DELETE
,
clazz
,
args
);
return
this
.
executeUpdate
(
this
.
getSqlId
(
clazz
,
"dyncDelete"
),
adapter
.
getSqlParams
());
}
public
<
T
extends
IModule
>
int
dbDelete
(
T
module
,
Serializable
...
id
)
{
Class
<
T
>
clazz
=
(
Class
<
T
>)
module
.
getClass
();
String
sqlId
=
this
.
getSqlId
(
clazz
,
"deleteByIds"
);
return
this
.
executeUpdate
(
sqlId
,
Arrays
.
asList
(
id
));
}
public
<
T
extends
IModule
>
int
dbDelete
(
Class
<
T
>
clazz
,
Map
paramMap
)
{
throw
new
UnsupportedOperationException
(
"暂不支持该方法!"
);
}
public
int
dbDelete
(
IModule
module
,
Object
val
,
String
col
)
{
return
0
;
}
public
int
dbDelete
(
IModule
module
,
Object
val
,
String
col
,
Object
val2
,
String
col2
)
{
return
0
;
}
public
<
T
extends
IModule
>
T
dbRead
(
T
module
,
Argument
...
args
)
{
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
AdaType
.
SELECT
,
module
.
getClass
(),
args
);
T
entity
=
(
T
)
this
.
dyncReadOne
(
module
.
getClass
(),
adapter
);
if
(
entity
!=
null
)
{
MdaDriver
.
copyValues
(
module
,
entity
);
}
return
entity
;
}
public
<
T
extends
IModule
>
T
dbRead
(
T
module
,
Object
...
args
)
{
return
null
;
}
public
<
T
extends
IModule
>
T
dbReadNoData
(
T
module
,
Object
...
args
)
{
return
null
;
}
public
<
T
extends
IModule
>
T
dbReadByArguments
(
T
module
,
Argument
<?>...
args
)
{
return
null
;
}
public
<
T
extends
IModule
>
T
dbReadHold
(
T
module
,
Object
...
args
)
{
return
null
;
}
public
<
T
extends
IModule
>
int
dbReadSet
(
IModuleList
<
T
>
list
,
Object
...
args
)
{
return
0
;
}
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
int
limitSize
,
Argument
...
args
)
{
list
.
clear
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
AdaType
.
SELECT
,
list
.
getDataClass
(),
args
);
String
temp
=
adapter
.
getSqlTemplate
();
Map
<
String
,
Object
>
params
=
adapter
.
getSqlParams
();
if
(
limitSize
==
0
)
{
List
<
T
>
resultList
=
this
.
dyncRead
(
list
.
getDataClass
(),
params
);
list
.
addAll
(
resultList
);
}
return
list
.
size
();
}
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
Object
...
args
)
{
list
.
clear
();
if
(
args
instanceof
Argument
[])
{
this
.
dbReadset
(
list
,
0
,
(
Argument
[])((
Argument
[])((
Argument
[])
args
)));
}
return
list
.
size
();
}
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
String
whereSql
,
Object
[]
objects
)
{
list
.
clear
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
AdaType
.
SELECT
,
list
.
getDataClass
(),
whereSql
,
objects
);
List
<
T
>
result
=
this
.
dyncRead
(
list
.
getDataClass
(),
adapter
);
list
.
addAll
(
result
);
return
list
.
size
();
}
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>[]
lists
,
int
maxSize
,
String
whereClause
,
Object
[]
datas
)
{
List
<
Class
<?
extends
IModule
>>
clazzList
=
new
ArrayList
();
IModuleList
[]
var6
=
lists
;
int
var7
=
lists
.
length
;
int
var8
;
for
(
var8
=
0
;
var8
<
var7
;
++
var8
)
{
IModuleList
iModule
=
var6
[
var8
];
clazzList
.
add
(
iModule
.
getDataClass
());
}
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
AdaType
.
SELECT
,
clazzList
,
whereClause
,
datas
);
IModuleList
[]
var13
=
lists
;
var8
=
lists
.
length
;
for
(
int
var14
=
0
;
var14
<
var8
;
++
var14
)
{
IModuleList
iModule
=
var13
[
var14
];
List
<
T
>
result
=
this
.
dyncRead
(
iModule
.
getDataClass
(),
adapter
);
iModule
.
addAll
(
result
);
}
return
lists
[
0
].
size
();
}
public
void
dbReadset
(
IModuleList
[]
lists
,
String
whereClause
,
Object
[]
datas
)
{
}
public
void
dbReadset
(
IModuleList
list
,
int
maxSize
,
String
sql
)
{
}
/** @deprecated */
@Deprecated
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
String
whereSql
)
{
return
this
.
dbReadset
((
IModuleList
)
list
,
whereSql
,
(
Object
[])
null
);
}
public
<
T
extends
IModule
>
int
dbReadSetByArguments
(
IModuleList
<
T
>
list
,
Argument
<?>...
args
)
{
return
0
;
}
public
void
dbConnect
()
{
//this.sqlSessionTemplate.getSqlSessionFactory().openSession(false);
this
.
getSqlSessionTemplate
(
null
).
getSqlSessionFactory
().
openSession
(
false
);
}
// @Override
// public void dbConnect(String dbName) {
// //切换数据库
// DataSourceUtil.setDB(dbName);
// }
public
SqlSessionTemplate
getSqlSessionTemplate
(
String
dbName
)
{
SqlSessionTemplate
sqlSessionTemplate
=
this
.
sqlSessionTemplate1
;
if
(
DATASOURCE_DB1
.
equals
(
dbName
))
{
sqlSessionTemplate
=
this
.
sqlSessionTemplate1
;
}
if
(
DATASOURCE_DB2
.
equals
(
dbName
))
{
sqlSessionTemplate
=
this
.
sqlSessionTemplate2
;
}
return
sqlSessionTemplate
;
}
public
<
T
>
T
dyncReadOneTest
(
String
dbName
,
Class
<?
extends
IModule
>
clazz
,
MybatisArgumentAdapter
adapter
)
{
SqlSessionTemplate
sqlSessionTemplate
=
getSqlSessionTemplate
(
dbName
);
String
sqlId
=
this
.
getSqlId
(
clazz
,
"dyncRead"
);
T
entity
=
sqlSessionTemplate
.
selectOne
(
sqlId
,
adapter
.
getSqlParams
());
if
(
entity
==
null
)
{
this
.
setNoMoreRow
();
}
return
entity
;
}
public
void
dbDisconnect
()
{
try
{
this
.
getSqlSessionTemplate
(
null
).
getConnection
().
close
();
}
catch
(
SQLException
var2
)
{
var2
.
printStackTrace
();
}
}
public
void
dbExecuteSQL
(
IStream
stm
)
{
}
public
void
dbExecuteSQL
(
IStream
sql
,
IStream
retstream
)
{
}
public
String
dbName
()
{
return
this
.
getSqlSessionTemplate
(
null
).
getConfiguration
().
getDatabaseId
();
}
public
void
dbExecuteSQL
(
String
sql
,
Object
...
objects
)
{
// Map<String, Object> map = new HashMap();
// if (objects != null) {
// sql = MybatisArgumentAdapter.convertPlaceHolder(sql, objects, map);
// }
//
// map.put("sql", sql);
// if (sql.toUpperCase().startsWith("SELECT")) {
// List<Map<String, Object>> result = this.dbExecuteMapper.dyncReadForMap(map);
// if (result == null || result.size() == 0) {
// this.setNoMoreRow();
// return;
// }
//
// resultLocal.set(new ResultSet(result));
// } else {
// this.dbExecuteMapper.dyncUpdateForMap(map);
// }
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
if
(
objects
!=
null
)
{
sql
=
MybatisArgumentAdapter
.
convertPlaceHolder
(
sql
,
objects
,
map
);
}
map
.
put
(
HOLDER_SQL
,
sql
);
if
(
sql
.
toUpperCase
().
startsWith
(
"SELECT"
))
{
List
<
Map
<
String
,
Object
>>
result
=
dbExecuteMapper
.
dyncReadForMap
(
map
);
if
(
result
==
null
||
result
.
size
()
==
0
)
{
setNoMoreRow
();
return
;
}
resultLocal
.
set
(
new
ResultSet
(
result
));
}
else
{
int
cnt
=
dbExecuteMapper
.
dyncUpdateForMap
(
map
);
}
}
public
void
dbFetch
(
IModule
module
)
{
}
public
void
dbFetchStream
(
IStream
args
)
{
}
public
void
dbFetchFields
(
Object
...
arguments
)
{
if
(
arguments
!=
null
&&
arguments
.
length
!=
0
)
{
if
(
resultLocal
.
get
()
==
null
)
{
log
.
error
(
"must execute dbExecuteSQL first !"
);
this
.
setNoMoreRow
();
}
else
if
(!((
ResultSet
)
resultLocal
.
get
()).
hasNext
())
{
this
.
setNoMoreRow
();
}
else
{
Map
<
String
,
Object
>
row
=
((
ResultSet
)
resultLocal
.
get
()).
next
();
Object
[]
var3
=
arguments
;
int
var4
=
arguments
.
length
;
for
(
int
var5
=
0
;
var5
<
var4
;
++
var5
)
{
Object
o
=
var3
[
var5
];
if
(
o
instanceof
Argument
)
{
Argument
argument
=
(
Argument
)
o
;
this
.
setArgumentValue
(
argument
,
row
.
get
(
argument
.
getFieldName
().
toUpperCase
()));
}
}
}
}
}
public
void
dbFetchFields
(
Argument
<?>...
arguments
)
{
}
private
void
setArgumentValue
(
Argument
<
Object
>
argument
,
Object
obj
)
{
if
(
obj
!=
null
)
{
argument
.
value
=
obj
;
}
}
public
void
dbCloseCursor
()
{
}
public
String
dbSqlDate
(
Date
endDat
)
{
return
null
;
}
public
void
dbFreeAll
()
{
}
public
void
setWaitCursor
()
{
}
public
void
dbHold
(
IModule
module
)
{
}
public
void
dbFree
(
IModule
module
)
{
}
public
void
dbSelect
(
IModule
module
,
String
string
,
String
sql
,
Object
...
val
)
{
}
public
void
DBOpen
(
IModule
module
)
{
}
public
void
dbSelectCursor
(
IModule
module
,
String
whereSql
,
Object
...
params
)
{
}
public
void
putDao
(
Class
<?>
key
,
Object
dao
)
{
throw
new
UnsupportedOperationException
(
"不支持的方法!"
);
}
private
<
T
>
String
getSqlId
(
Class
<
T
>
clazz
,
String
sid
)
{
return
String
.
format
(
this
.
sqlIdTemplate
,
this
.
namespacePackage
,
clazz
.
getSimpleName
(),
sid
);
}
private
<
T
>
T
dyncReadOne
(
Class
<?
extends
IModule
>
clazz
,
MybatisArgumentAdapter
adapter
)
{
String
sqlId
=
this
.
getSqlId
(
clazz
,
"dyncRead"
);
T
entity
=
this
.
getSqlSessionTemplate
(
null
).
selectOne
(
sqlId
,
adapter
.
getSqlParams
());
if
(
entity
==
null
)
{
this
.
setNoMoreRow
();
}
return
entity
;
}
private
<
T
>
List
<
T
>
dyncRead
(
Class
<
T
>
clazz
,
MybatisArgumentAdapter
adapter
)
{
String
sqlId
=
this
.
getSqlId
(
clazz
,
"dyncRead"
);
List
<
T
>
list
=
this
.
getSqlSessionTemplate
(
null
).
selectList
(
sqlId
,
adapter
.
getSqlParams
());
if
(
list
==
null
||
list
.
size
()
==
0
)
{
this
.
setNoMoreRow
();
}
return
new
ArrayList
(
list
);
}
private
<
T
>
List
<
T
>
dyncRead
(
Class
<
T
>
clazz
,
Map
params
)
{
String
sqlId
=
this
.
getSqlId
(
clazz
,
"dyncRead"
);
List
<
T
>
list
=
this
.
getSqlSessionTemplate
(
null
).
selectList
(
sqlId
,
params
);
if
(
list
==
null
||
list
.
size
()
==
0
)
{
this
.
setNoMoreRow
();
}
return
new
ArrayList
(
list
);
}
private
void
setNoMoreRow
()
{
MdaEnv
.
getContext
().
setErrorCode
(
228
);
}
private
int
executeUpdate
(
String
mybatisSqlId
,
Object
params
)
{
int
cnt
=
this
.
getSqlSessionTemplate
(
null
).
update
(
mybatisSqlId
,
params
);
return
cnt
;
}
public
static
class
ResultSet
{
private
int
index
=
0
;
private
List
<
Map
<
String
,
Object
>>
data
;
public
ResultSet
(
List
<
Map
<
String
,
Object
>>
data
)
{
this
.
data
=
data
;
}
public
Map
<
String
,
Object
>
next
()
{
return
this
.
index
<
this
.
data
.
size
()
?
(
Map
)
this
.
data
.
get
(
this
.
index
++)
:
null
;
}
public
boolean
hasNext
()
{
return
this
.
data
!=
null
&&
this
.
index
<
this
.
data
.
size
();
}
}
}
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