Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gjjs-bd-runtime
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
model-driver
gjjs-bd-runtime
Commits
6319f3ee
Commit
6319f3ee
authored
Dec 08, 2022
by
s_guodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将mybatis迁移到runtime
parent
bee42545
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
1160 additions
and
0 deletions
+1160
-0
DBLocker.java
...ain/java/com/brilliance/mda/support/mybatis/DBLocker.java
+42
-0
FileDisplayManager.java
...om/brilliance/mda/support/mybatis/FileDisplayManager.java
+59
-0
MyBatisDaoSession.java
...com/brilliance/mda/support/mybatis/MyBatisDaoSession.java
+484
-0
MybatisArgumentAdapter.java
...rilliance/mda/support/mybatis/MybatisArgumentAdapter.java
+368
-0
Cnt.java
...in/java/com/brilliance/mda/support/mybatis/count/Cnt.java
+8
-0
CounterImplWithOracleTransaction.java
...pport/mybatis/count/CounterImplWithOracleTransaction.java
+28
-0
CounterImplWithSpringTransaction.java
...pport/mybatis/count/CounterImplWithSpringTransaction.java
+52
-0
CounterService.java
.../brilliance/mda/support/mybatis/count/CounterService.java
+5
-0
CounterMapper.java
...iance/mda/support/mybatis/count/mapper/CounterMapper.java
+47
-0
DbExecute.java
...va/com/brilliance/mda/support/mybatis/dync/DbExecute.java
+7
-0
DbExecuteMapper.java
...ance/mda/support/mybatis/dync/mapper/DbExecuteMapper.java
+24
-0
Column.java
...ava/com/brilliance/mda/support/mybatis/entity/Column.java
+10
-0
Table.java
...java/com/brilliance/mda/support/mybatis/entity/Table.java
+10
-0
StartListener.java
...rilliance/mda/support/mybatis/listener/StartListener.java
+16
-0
No files found.
src/main/java/com/brilliance/mda/support/mybatis/DBLocker.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
;
import
com.brilliance.mda.runtime.mda.ILocker
;
import
com.brilliance.mda.runtime.mda.LockInfo
;
import
org.springframework.stereotype.Component
;
import
java.io.Serializable
;
@Component
public
class
DBLocker
implements
ILocker
{
@Override
public
boolean
lock
(
Serializable
key
)
{
return
false
;
}
@Override
public
boolean
lock
(
Serializable
key
,
int
expireflg
)
{
return
false
;
}
@Override
public
boolean
lock
(
Serializable
key
,
long
timeout
)
{
return
false
;
}
@Override
public
LockInfo
lock
(
String
userName
,
Serializable
key
)
{
return
null
;
}
@Override
public
boolean
unlock
(
Serializable
key
)
{
return
false
;
}
@Override
public
boolean
unlock
(
String
lockname
,
Serializable
key
)
{
return
false
;
}
}
src/main/java/com/brilliance/mda/support/mybatis/FileDisplayManager.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
;
import
com.brilliance.mda.runtime.mda.IDisplay
;
import
org.apache.commons.io.IOUtils
;
import
org.springframework.stereotype.Component
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
/**
* 快照保存器
* @author fukai
*
*/
@Component
public
class
FileDisplayManager
implements
IDisplay
{
@Override
public
boolean
saveDisplay
(
String
filePathOrKey
,
String
data
)
{
File
file
=
new
File
(
filePathOrKey
);
File
parentFolder
=
file
.
getParentFile
();
if
(!
parentFolder
.
exists
()
&&
!
parentFolder
.
mkdirs
())
return
false
;
FileWriter
fr
=
null
;
try
{
file
.
createNewFile
();
fr
=
new
FileWriter
(
file
);
IOUtils
.
write
(
data
,
fr
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
return
true
;
}
@SuppressWarnings
(
"deprecation"
)
@Override
public
String
readDisplay
(
String
filePathOrKey
)
{
File
file
=
new
File
(
filePathOrKey
);
if
(!
file
.
exists
())
return
null
;
FileReader
fr
=
null
;
try
{
fr
=
new
FileReader
(
file
);
return
IOUtils
.
toString
(
fr
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
IOUtils
.
closeQuietly
(
fr
);
}
return
null
;
}
}
src/main/java/com/brilliance/mda/support/mybatis/MyBatisDaoSession.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
;
import
com.brilliance.mda.runtime.mda.*
;
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.count.CounterService
;
import
com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper
;
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
java.io.Serializable
;
import
java.sql.SQLException
;
import
java.util.*
;
import
static
com
.
brilliance
.
mda
.
support
.
mybatis
.
MybatisArgumentAdapter
.
AdaType
.
DELETE
;
import
static
com
.
brilliance
.
mda
.
support
.
mybatis
.
MybatisArgumentAdapter
.
AdaType
.
SELECT
;
import
static
com
.
brilliance
.
mda
.
support
.
mybatis
.
MybatisArgumentAdapter
.
HOLDER_SQL
;
/**
* 基于Spring SqlSessionTemplate的实现.需要满足以下要求:
* 1,src/main/resources/mapper中的xml文件需要加入到mapper文件的扫描路径中
* 2,所有mybatis的mapper.xml文件namespace命名规则遵循
* "[namespacePackage].[BO.class.getSimpleName()]Mapper"
* namespacePackage : 类属性
* BO.class.getSimpleName(): BO类的名称
* <p>
* 3,所有的mybatis的mapper.xml文件中包含以下两类sql模板
* a,基于Mybatis官方Generator生成的: insert,updateByPrimaryKey,delete
* b,特殊的:
* deleteByIds(采用in方式删除主键匹配)
* <delete id="deleteByIds" parameterClass="java.util.List">
* sql...
* </delete>
* <p>
* dyncRead(实现Argument方式的动态查询)
* <select id="dyncRead" >
* ${sql}
* </select>
* <p>
* dyncDelete(实现Argument方式的动态删除)
* <delete id="dyncDelete" >
* ${sql}
* </delete>
*
* @author hulei
*/
@Component
public
class
MyBatisDaoSession
extends
AbstractDaoSession
implements
IDaoSession
{
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
MyBatisDaoSession
.
class
);
private
static
ThreadLocal
<
ResultSet
>
resultLocal
=
new
ThreadLocal
<>();
//定义mapper文件中的sql语句id
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"
;
@Autowired
CounterService
counter
;
@Autowired
DbExecuteMapper
dbExecuteMapper
;
@Autowired
SqlSessionTemplate
sqlSessionTemplate
;
/**
* mapper.xml文件中namespace指向类的package
*/
private
String
namespacePackage
=
"com.ceb.gjjs.mapper"
;
public
void
setNamespacePackage
(
String
namespacePackage
)
{
this
.
namespacePackage
=
namespacePackage
;
}
/**
* mybatis sqlid 格式化模板,
* %s: namespackPackage
* %s: BO类名
* %s:具体sqlId
*/
private
String
sqlIdTemplate
=
"%s.%sMapper.%s"
;
public
void
setSqlIdTemplate
(
String
sqlIdTemplate
)
{
this
.
sqlIdTemplate
=
sqlIdTemplate
;
}
@Override
public
int
dbCounter
(
String
seqname
)
{
return
this
.
counter
.
dbCounter
(
seqname
);
}
@Override
public
int
dbInsert
(
IModule
module
)
{
String
sqlId
=
getSqlId
(
module
.
getClass
(),
SQLID_INSERT
);
return
executeUpdate
(
sqlId
,
module
);
}
@Override
public
int
dbUpdate
(
IModule
module
,
Object
v1
,
String
whereCol1
)
{
return
0
;
}
@Override
public
int
dbUpdate
(
IModule
module
,
Object
v1
,
String
whereCol1
,
Object
v2
,
String
whereCol2
)
{
return
0
;
}
@Override
public
<
T
extends
IModule
>
int
dbUpdate
(
T
module
)
{
String
sqlId
=
getSqlId
(
module
.
getClass
(),
SQLID_UPDATE
);
return
executeUpdate
(
sqlId
,
module
);
}
@Override
public
int
dbDelete
(
IModule
module
)
{
String
sqlId
=
getSqlId
(
module
.
getClass
(),
SQLID_DELETE
);
return
executeUpdate
(
sqlId
,
module
);
}
@Override
public
int
dbDelete
(
IModule
m
,
Argument
<?
extends
Object
>...
args
)
{
Class
clazz
=
(
Class
)
m
.
getClass
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
DELETE
,
clazz
,
args
);
return
executeUpdate
(
getSqlId
(
clazz
,
SQLID_DYNCDEL
),
adapter
.
getSqlParams
());
}
public
<
T
extends
IModule
>
int
dbDelete
(
T
module
,
Serializable
...
id
)
{
Class
<
T
>
clazz
=
(
Class
<
T
>)
module
.
getClass
();
String
sqlId
=
getSqlId
(
clazz
,
SQLID_DELBYIDS
);
return
executeUpdate
(
sqlId
,
Arrays
.
asList
(
id
));
}
public
<
T
extends
IModule
>
int
dbDelete
(
Class
<
T
>
clazz
,
Map
paramMap
)
{
throw
new
UnsupportedOperationException
(
"暂不支持该方法!"
);
}
@Override
public
int
dbDelete
(
IModule
module
,
Object
val
,
String
col
)
{
return
0
;
}
@Override
public
int
dbDelete
(
IModule
module
,
Object
val
,
String
col
,
Object
val2
,
String
col2
)
{
return
0
;
}
@Override
public
<
T
extends
IModule
>
T
dbRead
(
T
module
,
Argument
...
args
)
{
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
SELECT
,
module
.
getClass
(),
args
);
T
entity
=
this
.
dyncReadOne
(
module
.
getClass
(),
adapter
);
if
(
entity
!=
null
)
{
MdaDriver
.
copyValues
(
module
,
entity
);
}
return
entity
;
}
@Override
public
<
T
extends
IModule
>
T
dbRead
(
T
module
,
Object
...
args
)
{
return
null
;
}
@Override
public
<
T
extends
IModule
>
T
dbReadNoData
(
T
module
,
Object
...
args
)
{
return
null
;
}
@Override
public
<
T
extends
IModule
>
T
dbReadByArguments
(
T
module
,
Argument
<?>...
args
)
{
return
null
;
}
@Override
public
<
T
extends
IModule
>
T
dbReadHold
(
T
module
,
Object
...
args
)
{
return
null
;
}
@Override
public
<
T
extends
IModule
>
int
dbReadSet
(
IModuleList
<
T
>
list
,
Object
...
args
)
{
return
0
;
}
@Override
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
int
limitSize
,
Argument
...
args
)
{
list
.
clear
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
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
);
}
else
{
//TODO 实现分页查询
// Page<T> page = new Page<>(1,limitSize);
// IPage<T> pageData = baseMapper.selectPage(page, wrapper);
// list.addAll(pageData.getRecords());
}
return
list
.
size
();
}
@Override
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
Object
...
args
)
{
list
.
clear
();
if
(
args
instanceof
Argument
[])
{
this
.
dbReadset
(
list
,
0
,
(
Argument
[])
args
);
}
return
list
.
size
();
}
@Override
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
String
whereSql
,
Object
[]
objects
)
{
list
.
clear
();
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
SELECT
,
list
.
getDataClass
(),
whereSql
,
objects
);
List
<
T
>
result
=
this
.
dyncRead
(
list
.
getDataClass
(),
adapter
);
list
.
addAll
(
result
);
return
list
.
size
();
}
@Override
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>[]
lists
,
int
maxSize
,
String
whereClause
,
Object
[]
datas
)
{
List
<
Class
<?
extends
IModule
>>
clazzList
=
new
ArrayList
<>();
for
(
IModuleList
iModule
:
lists
)
{
clazzList
.
add
(
iModule
.
getDataClass
());
}
MybatisArgumentAdapter
adapter
=
new
MybatisArgumentAdapter
(
SELECT
,
clazzList
,
whereClause
,
datas
);
for
(
IModuleList
iModule
:
lists
)
{
List
<
T
>
result
=
this
.
dyncRead
(
iModule
.
getDataClass
(),
adapter
);
iModule
.
addAll
(
result
);
}
return
lists
[
0
].
size
();
}
@Override
public
void
dbReadset
(
IModuleList
[]
lists
,
String
whereClause
,
Object
[]
datas
)
{
}
@Override
public
void
dbReadset
(
IModuleList
list
,
int
maxSize
,
String
sql
)
{
}
/**
* 方法存在SQL注入风险,不建议使用
*/
@Deprecated
@Override
public
<
T
extends
IModule
>
int
dbReadset
(
IModuleList
<
T
>
list
,
String
whereSql
)
{
return
this
.
dbReadset
(
list
,
whereSql
,
null
);
}
@Override
public
<
T
extends
IModule
>
int
dbReadSetByArguments
(
IModuleList
<
T
>
list
,
Argument
<?>...
args
)
{
return
0
;
}
@Override
public
void
dbConnect
()
{
sqlSessionTemplate
.
getSqlSessionFactory
().
openSession
(
false
);
}
@Override
public
void
dbDisconnect
()
{
try
{
sqlSessionTemplate
.
getConnection
().
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
void
dbExecuteSQL
(
IStream
stm
)
{
}
@Override
public
void
dbExecuteSQL
(
IStream
sql
,
IStream
retstream
)
{
}
@Override
public
String
dbName
()
{
return
sqlSessionTemplate
.
getConfiguration
().
getDatabaseId
();
}
@Override
public
void
dbExecuteSQL
(
String
sql
,
Object
...
objects
)
{
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
);
}
}
@Override
public
void
dbFetch
(
IModule
module
)
{
}
@Override
public
void
dbFetchStream
(
IStream
args
)
{
}
@Override
public
void
dbFetchFields
(
Object
...
arguments
)
{
if
(
arguments
==
null
||
arguments
.
length
==
0
)
return
;
if
(
resultLocal
.
get
()
==
null
)
{
log
.
error
(
"must execute dbExecuteSQL first !"
);
setNoMoreRow
();
return
;
}
if
(!
resultLocal
.
get
().
hasNext
())
{
setNoMoreRow
();
return
;
}
Map
<
String
,
Object
>
row
=
resultLocal
.
get
().
next
();
for
(
Object
o
:
arguments
)
{
if
(
o
instanceof
Argument
)
{
Argument
argument
=
(
Argument
)
o
;
setArgumentValue
(
argument
,
row
.
get
(
argument
.
getFieldName
().
toUpperCase
()));
}
}
}
@Override
public
void
dbFetchFields
(
Argument
<?>...
arguments
)
{
}
private
void
setArgumentValue
(
Argument
<?
extends
Object
>
argument
,
Object
obj
)
{
if
(
obj
==
null
)
return
;
Argument
<
Object
>
arg
=
(
Argument
<
Object
>)
argument
;
arg
.
value
=
obj
;
}
@Override
public
void
dbCloseCursor
()
{
//mybatis方式不直接访问结果集,不需要关闭结果集
}
@Override
public
String
dbSqlDate
(
Date
endDat
)
{
return
null
;
}
@Override
public
void
dbFreeAll
()
{
}
@Override
public
void
setWaitCursor
()
{
}
@Override
public
void
dbHold
(
IModule
module
)
{
}
@Override
public
void
dbFree
(
IModule
module
)
{
}
@Override
public
void
dbSelect
(
IModule
module
,
String
string
,
String
sql
,
Object
...
val
)
{
}
@Override
public
void
DBOpen
(
IModule
module
)
{
}
@Override
public
void
dbSelectCursor
(
IModule
module
,
String
whereSql
,
Object
...
params
)
{
}
@Override
public
void
putDao
(
Class
<?>
key
,
Object
dao
)
{
throw
new
UnsupportedOperationException
(
"不支持的方法!"
);
}
private
<
T
>
String
getSqlId
(
Class
<
T
>
clazz
,
String
sid
)
{
return
String
.
format
(
sqlIdTemplate
,
namespacePackage
,
clazz
.
getSimpleName
(),
sid
);
}
private
<
T
>
T
dyncReadOne
(
Class
<?
extends
IModule
>
clazz
,
MybatisArgumentAdapter
adapter
)
{
String
sqlId
=
getSqlId
(
clazz
,
SQLID_DYNCREAD
);
T
entity
=
sqlSessionTemplate
.
selectOne
(
sqlId
,
adapter
.
getSqlParams
());
if
(
entity
==
null
)
{
setNoMoreRow
();
}
return
entity
;
}
private
<
T
>
List
<
T
>
dyncRead
(
Class
<
T
>
clazz
,
MybatisArgumentAdapter
adapter
)
{
String
sqlId
=
getSqlId
(
clazz
,
SQLID_DYNCREAD
);
List
<
T
>
list
=
sqlSessionTemplate
.
selectList
(
sqlId
,
adapter
.
getSqlParams
());
if
(
list
==
null
||
list
.
size
()
==
0
)
{
setNoMoreRow
();
}
return
new
ArrayList
<>(
list
);
}
private
<
T
>
List
<
T
>
dyncRead
(
Class
<
T
>
clazz
,
Map
params
)
{
String
sqlId
=
getSqlId
(
clazz
,
SQLID_DYNCREAD
);
List
<
T
>
list
=
sqlSessionTemplate
.
selectList
(
sqlId
,
params
);
if
(
list
==
null
||
list
.
size
()
==
0
)
{
setNoMoreRow
();
}
return
new
ArrayList
<>(
list
);
}
public
static
class
ResultSet
{
private
int
index
;
private
List
<
Map
<
String
,
Object
>>
data
;
public
ResultSet
(
List
<
Map
<
String
,
Object
>>
data
)
{
this
.
index
=
0
;
this
.
data
=
data
;
}
public
Map
<
String
,
Object
>
next
()
{
if
(
index
<
data
.
size
())
{
return
data
.
get
(
index
++);
}
return
null
;
}
public
boolean
hasNext
()
{
return
data
!=
null
&&
index
<
data
.
size
();
}
}
private
void
setNoMoreRow
()
{
MdaEnv
.
getContext
().
setErrorCode
(
Constants
.
NO_MORE_ROW
);
}
private
int
executeUpdate
(
String
mybatisSqlId
,
Object
params
)
{
int
cnt
=
sqlSessionTemplate
.
update
(
mybatisSqlId
,
params
);
return
cnt
;
}
}
src/main/java/com/brilliance/mda/support/mybatis/MybatisArgumentAdapter.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
;
import
com.brilliance.mda.runtime.mda.IModule
;
import
com.brilliance.mda.runtime.mda.impl.Argument
;
import
com.brilliance.mda.support.mybatis.entity.Column
;
import
com.brilliance.mda.support.mybatis.entity.Table
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.util.StringUtils
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
static
com
.
brilliance
.
mda
.
runtime
.
mda
.
OpType
.*;
/**
* @author hulei
* bd代码中sql语句与mybatis sql之间的适配转换器
*/
public
class
MybatisArgumentAdapter
{
private
static
Map
<
String
,
String
>
annotCache
=
new
ConcurrentHashMap
<>();
public
static
final
String
HOLDER_SQL
=
"sql"
;
public
static
final
String
HOLDER_CONDS
=
"conditions"
;
public
static
enum
AdaType
{
SELECT
(
"SELECT * FROM %s WHERE %s"
),
DELETE
(
"DELETE FROM %s WHERE %s"
);
private
String
value
;
private
AdaType
(
String
val
)
{
this
.
value
=
val
;
}
public
String
getValue
()
{
return
value
;
}
}
private
Logger
log
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
private
Class
<?
extends
IModule
>
clazz
;
private
List
<
Class
<?
extends
IModule
>>
clazzList
;
private
Argument
[]
args
;
private
String
sqlTemplate
;
private
Map
<
String
,
Object
>
sqlParams
;
private
int
idx
=
0
;
private
String
whereSql
;
private
String
pattern
;
private
Object
[]
params
;
private
String
tableName
;
public
MybatisArgumentAdapter
(
AdaType
type
,
Class
<?
extends
IModule
>
clazz
,
Argument
...
args
)
{
this
.
clazz
=
clazz
;
this
.
pattern
=
type
.
getValue
();
this
.
args
=
args
;
this
.
tableName
=
convertTableName
();
this
.
init
();
}
public
MybatisArgumentAdapter
(
AdaType
type
,
Class
<?
extends
IModule
>
clazz
,
String
whereSql
)
{
this
(
type
,
clazz
,
whereSql
,
null
);
}
public
MybatisArgumentAdapter
(
AdaType
type
,
Class
<?
extends
IModule
>
clazz
,
String
whereSql
,
Object
[]
params
)
{
this
.
clazz
=
clazz
;
this
.
pattern
=
type
.
getValue
();
this
.
whereSql
=
whereSql
;
this
.
params
=
params
;
this
.
tableName
=
convertTableName
();
this
.
init
();
}
public
MybatisArgumentAdapter
(
AdaType
type
,
List
<
Class
<?
extends
IModule
>>
clazzList
,
String
whereSql
,
Object
[]
params
)
{
this
.
clazzList
=
clazzList
;
this
.
pattern
=
type
.
getValue
();
this
.
whereSql
=
whereSql
;
this
.
params
=
params
;
this
.
tableName
=
convertTableNames
();
this
.
init
();
}
public
void
init
()
{
sqlParams
=
new
HashMap
<>();
String
conditions
=
""
;
if
(
whereSql
!=
null
&&
!
whereSql
.
trim
().
equals
(
""
))
{
if
(
whereSql
.
toUpperCase
().
indexOf
(
"WHERE"
)
>
-
1
)
{
pattern
=
pattern
.
replace
(
"WHERE"
,
""
);
}
if
(
params
!=
null
&&
params
.
length
>
0
)
{
conditions
=
convertPlaceHolder
(
whereSql
,
params
,
sqlParams
);
}
else
{
conditions
=
whereSql
;
}
}
else
{
if
(
this
.
args
!=
null
&&
this
.
args
.
length
>
0
)
{
conditions
=
parserArgument
(
this
.
args
);
}
else
{
conditions
=
" 1=1 "
;
}
}
this
.
sqlTemplate
=
String
.
format
(
pattern
,
tableName
,
conditions
);
this
.
sqlParams
.
put
(
HOLDER_SQL
,
this
.
sqlTemplate
);
this
.
sqlParams
.
put
(
HOLDER_CONDS
,
conditions
);
}
public
String
getSqlTemplate
()
{
return
sqlTemplate
;
}
public
Map
<
String
,
Object
>
getSqlParams
()
{
return
sqlParams
;
}
/**
* 获取直接WHERE条件进行参数替换
*/
protected
final
String
parserArgument
(
String
whereClause
,
Object
[]
args
)
{
StringBuffer
sb
=
new
StringBuffer
(
" "
);
Matcher
m
=
Pattern
.
compile
(
"\\?"
).
matcher
(
whereClause
);
int
offset
=
0
,
i
=
0
;
while
(
m
.
find
())
{
sb
.
append
(
whereClause
.
substring
(
offset
,
m
.
start
()));
sb
.
append
(
"#{args["
+
i
+
"]}"
);
i
++;
offset
=
m
.
end
();
}
sb
.
append
(
whereClause
.
substring
(
offset
));
if
(
log
.
isDebugEnabled
())
log
.
debug
(
sb
.
toString
());
return
sb
.
toString
();
}
/**
* 获取生成对应的SQL条件(这里完成最终的组装)
*/
protected
final
String
parserArgument
(
Argument
...
arguments
)
{
List
<
String
>
orders
=
new
ArrayList
();
List
<
String
>
criterions
=
new
ArrayList
();
for
(
int
i
=
0
;
i
<
arguments
.
length
;
i
++)
process
(
arguments
[
i
],
i
,
orders
,
criterions
);
StringBuffer
sb
=
new
StringBuffer
();
String
conn
=
""
;
for
(
String
criterion
:
criterions
)
{
sb
.
append
(
conn
).
append
(
criterion
);
conn
=
" AND "
;
}
conn
=
" ORDER BY "
;
for
(
String
criterion
:
orders
)
{
sb
.
append
(
conn
).
append
(
criterion
);
conn
=
","
;
}
if
(
log
.
isDebugEnabled
())
log
.
debug
(
sb
.
toString
());
return
sb
.
toString
();
}
/**
* 按Order和Criterion来处理Argument
*/
private
final
void
process
(
Argument
argument
,
int
index
,
List
orders
,
List
criterions
)
{
if
(
argument
.
opType
==
ASC
)
orders
.
add
(
argument
.
fieldName
);
else
if
(
argument
.
opType
==
DESC
)
orders
.
add
(
argument
.
fieldName
+
" DESC"
);
else
{
String
criterion
=
processCriterion
(
argument
,
index
);
if
(
criterion
!=
null
)
criterions
.
add
(
criterion
);
}
}
private
final
String
processCriterion
(
Argument
argument
,
int
idx
)
{
if
(
argument
==
null
||
argument
.
opType
==
null
)
return
null
;
StringBuffer
sb
;
String
conn
;
String
holderName
;
switch
(
argument
.
opType
)
{
case
ISNULL:
return
convertColumn
(
argument
.
fieldName
)
+
" IS NULL"
;
case
ISNOTNULL:
return
convertColumn
(
argument
.
fieldName
)
+
" IS NOT NULL"
;
case
NE:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
"!="
+
placeholder
(
argument
.
value
,
holderName
);
case
LE:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
"<="
+
placeholder
(
argument
.
value
,
holderName
);
case
GE:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
">="
+
placeholder
(
argument
.
value
,
holderName
);
case
LT:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
"<"
+
placeholder
(
argument
.
value
,
holderName
);
case
GT:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
">"
+
placeholder
(
argument
.
value
,
holderName
);
case
NOTLIKE:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
" NOT LIKE "
+
placeholder
(
argument
.
value
,
holderName
);
case
LIKE:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
" LIKE "
+
placeholder
(
argument
.
value
,
holderName
);
case
BETWEEN:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
Object
[]
ary
=
(
Object
[])
argument
.
value
;
sqlParams
.
put
(
holderName
+
"_0"
,
ary
[
0
]);
sqlParams
.
put
(
holderName
+
"_1"
,
ary
[
1
]);
return
convertColumn
(
argument
.
fieldName
)
+
" BETWEEN "
+
placeholder
(
ary
[
0
],
holderName
+
"_0"
)
+
" AND "
+
placeholder
(
ary
[
1
],
holderName
+
"_1"
);
case
IN:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sb
=
new
StringBuffer
(
convertColumn
(
argument
.
fieldName
)).
append
(
" IN ("
);
conn
=
""
;
Object
[]
values
=
(
Object
[])
argument
.
value
;
for
(
int
i
=
0
;
i
<
values
.
length
;
i
++)
{
sqlParams
.
put
(
holderName
+
"_"
+
i
,
values
[
i
]);
sb
.
append
(
conn
).
append
(
placeholder
(
values
[
i
],
holderName
+
"_"
+
i
));
conn
=
","
;
}
return
sb
.
append
(
")"
).
toString
();
case
AND:
case
OR:
sb
=
new
StringBuffer
(
"("
);
conn
=
""
;
Argument
[]
arguments
=
(
Argument
[])
argument
.
value
;
for
(
int
i
=
0
;
i
<
arguments
.
length
;
i
++)
{
sb
.
append
(
conn
).
append
(
processCriterion
(
arguments
[
i
],
i
));
conn
=
argument
.
opType
==
AND
?
" AND "
:
" OR "
;
}
return
sb
.
append
(
")"
).
toString
();
case
NOT:
return
"!("
+
processCriterion
((
Argument
)
argument
.
value
,
idx
)
+
")"
;
default
:
holderName
=
argument
.
fieldName
+
"_"
+
this
.
idx
++;
sqlParams
.
put
(
holderName
,
argument
.
value
);
return
convertColumn
(
argument
.
fieldName
)
+
"="
+
placeholder
(
argument
.
value
,
holderName
);
}
}
private
final
String
placeholder
(
Object
value
,
String
name
)
{
return
String
.
format
(
"#{%s}"
,
name
);
}
// /**
// * 获取指定属性名中@Column注解的值
// * @param clazz
// * @param name
// * @return
// */
// public static String getFieldAnnotation(Class<? extends IModule> clazz,String name){
//
// }
/**
* 将标准SQL占位符替换为mybatis格式的占位符,并将参数数组转换为HashMap
*
* @param sql
* @param objects
* @param map
* @return
*/
public
static
String
convertPlaceHolder
(
String
sql
,
Object
[]
objects
,
Map
<
String
,
Object
>
map
)
{
Pattern
p
=
Pattern
.
compile
(
"[?]"
);
Matcher
m
=
p
.
matcher
(
sql
);
StringBuffer
buf
=
new
StringBuffer
();
for
(
int
i
=
0
;
m
.
find
()
&&
i
<
objects
.
length
;
i
++)
{
String
holder
=
"var_"
+
i
;
m
.
appendReplacement
(
buf
,
String
.
format
(
"#{%s}"
,
holder
));
map
.
put
(
holder
,
objects
[
i
]);
}
m
.
appendTail
(
buf
);
return
buf
.
toString
();
}
public
String
convertTableName
()
{
if
(
annotCache
.
containsKey
(
clazz
.
getName
()))
{
return
annotCache
.
get
(
clazz
.
getName
());
}
String
value
=
clazz
.
getSimpleName
();
if
(
clazz
.
isAnnotationPresent
(
Table
.
class
))
{
Table
t
=
clazz
.
getAnnotation
(
Table
.
class
);
if
(
t
.
value
()
!=
null
)
{
value
=
t
.
value
();
}
}
annotCache
.
put
(
clazz
.
getName
(),
value
);
return
value
;
}
private
String
convertTableNames
()
{
StringBuffer
sb
=
new
StringBuffer
(
""
);
for
(
Class
c
:
clazzList
)
{
sb
.
append
(
c
.
getName
());
}
if
(
annotCache
.
containsKey
(
sb
.
toString
()))
{
return
annotCache
.
get
(
sb
.
toString
());
}
List
<
String
>
tb
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
clazzList
.
size
();
i
++)
{
int
idx
=
i
+
1
;
Class
<?
extends
IModule
>
c
=
clazzList
.
get
(
i
);
String
value
=
c
.
getSimpleName
();
if
(
c
.
isAnnotationPresent
(
Table
.
class
))
{
Table
t
=
c
.
getAnnotation
(
Table
.
class
);
if
(
t
.
value
()
!=
null
)
{
value
=
t
.
value
();
}
}
tb
.
add
(
value
+
" m"
+
idx
);
}
String
value
=
String
.
join
(
","
,
tb
);
annotCache
.
put
(
sb
.
toString
(),
value
);
return
value
;
}
public
String
convertColumn
(
String
fieldName
)
{
String
key
=
String
.
format
(
"%s.%s"
,
clazz
.
getName
(),
fieldName
);
if
(
annotCache
.
containsKey
(
key
))
{
return
annotCache
.
get
(
key
);
}
try
{
Field
field
=
clazz
.
getDeclaredField
(
fieldName
);
String
value
=
fieldName
;
if
(
field
.
isAnnotationPresent
(
Column
.
class
))
{
Column
col
=
field
.
getAnnotation
(
Column
.
class
);
if
(!
StringUtils
.
isEmpty
(
col
.
value
()))
{
value
=
col
.
value
();
}
}
annotCache
.
put
(
key
,
value
);
}
catch
(
NoSuchFieldException
e
)
{
annotCache
.
put
(
key
,
fieldName
);
return
fieldName
;
}
return
annotCache
.
get
(
key
);
}
}
src/main/java/com/brilliance/mda/support/mybatis/count/Cnt.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
count
;
public
class
Cnt
{
private
String
nam
;
//配置名称
private
int
val
;
//配置名称
private
int
stp
;
//配置名称
}
src/main/java/com/brilliance/mda/support/mybatis/count/CounterImplWithOracleTransaction.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
count
;
import
com.brilliance.mda.support.mybatis.count.mapper.CounterMapper
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
javax.annotation.Resource
;
/**
* @author hulei
* dbcounter基于oracle自主事务的实现
*/
//@Service
public
class
CounterImplWithOracleTransaction
implements
CounterService
{
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
().
getName
());
@Resource
public
CounterMapper
counterMapper
;
public
int
dbCounter
(
String
seqname
)
{
String
seq
=
"SEQ_"
+
seqname
.
toUpperCase
();
return
counterMapper
.
seqNextval
(
seq
);
}
}
src/main/java/com/brilliance/mda/support/mybatis/count/CounterImplWithSpringTransaction.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
count
;
import
com.brilliance.mda.runtime.mda.RuleExecuteException
;
import
com.brilliance.mda.support.mybatis.count.mapper.CounterMapper
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
javax.annotation.Resource
;
import
java.util.concurrent.locks.ReentrantLock
;
@Service
public
class
CounterImplWithSpringTransaction
implements
CounterService
{
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
().
getName
());
@Resource
public
CounterMapper
counterMapper
;
public
ReentrantLock
lock
=
new
ReentrantLock
();
@Transactional
(
propagation
=
Propagation
.
REQUIRES_NEW
)
public
int
dbCounter
(
String
seqname
)
{
Integer
cnt
=
counterMapper
.
getCountValWithUpdate
(
seqname
);
if
(
cnt
==
null
)
cnt
=
0
;
if
(
cnt
==
0
)
{
lock
.
lock
();
try
{
cnt
=
counterMapper
.
getCountValWithUpdate
(
seqname
);
if
(
cnt
==
null
)
{
cnt
=
0
;
counterMapper
.
insertNewCounter
(
seqname
,
1
);
//插入新数
}
else
counterMapper
.
updateCounter
(
seqname
);
//计算器增加
}
catch
(
Exception
e
)
{
throw
(
new
RuleExecuteException
(
"主键生成异常"
,
e
));
}
finally
{
lock
.
unlock
();
}
}
else
counterMapper
.
updateCounter
(
seqname
);
//计算器增加
return
cnt
;
}
}
src/main/java/com/brilliance/mda/support/mybatis/count/CounterService.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
count
;
public
interface
CounterService
{
int
dbCounter
(
String
seqname
);
}
src/main/java/com/brilliance/mda/support/mybatis/count/mapper/CounterMapper.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
count
.
mapper
;
import
org.apache.ibatis.annotations.Param
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
import
java.util.Map
;
@Component
public
class
CounterMapper
{
final
String
FNAME
=
this
.
getClass
().
getName
();
@Autowired
public
SqlSessionTemplate
template
;
public
Integer
getCountValWithUpdate
(
@Param
(
"nam"
)
String
seqName
){
return
template
.
selectOne
(
FNAME
+
".getCountValWithUpdate"
,
seqName
);
}
public
int
insertNewCounter
(
@Param
(
"nam"
)
String
nam
,
@Param
(
"stp"
)
int
stp
){
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"nam"
,
nam
);
map
.
put
(
"start"
,
1
);
map
.
put
(
"stp"
,
stp
);
return
template
.
insert
(
FNAME
+
".insertNewCounter"
,
map
);
}
public
void
updateCounter
(
@Param
(
"nam"
)
String
seqName
){
template
.
selectOne
(
FNAME
+
".updateCounter"
,
seqName
);
}
public
int
seqNextval
(
String
seqName
)
{
return
template
.
selectOne
(
FNAME
+
".seqNextval"
,
seqName
);
}
public
int
dbCounter
(
String
seqName
){
return
template
.
selectOne
(
FNAME
+
".dbCounter"
,
seqName
);
}
}
\ No newline at end of file
src/main/java/com/brilliance/mda/support/mybatis/dync/DbExecute.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
dync
;
import
com.brilliance.mda.runtime.mda.impl.AbstractModule
;
public
class
DbExecute
extends
AbstractModule
{
}
src/main/java/com/brilliance/mda/support/mybatis/dync/mapper/DbExecuteMapper.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
dync
.
mapper
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Map
;
@Component
public
class
DbExecuteMapper
{
@Autowired
SqlSessionTemplate
sqlSessionTemplate
;
final
String
FNAME
=
this
.
getClass
().
getName
();
public
List
<
Map
<
String
,
Object
>>
dyncReadForMap
(
Map
<
String
,
Object
>
params
){
return
sqlSessionTemplate
.
selectList
(
FNAME
+
".dyncReadForMap"
,
params
);
}
public
int
dyncUpdateForMap
(
Map
<
String
,
Object
>
params
){
return
sqlSessionTemplate
.
update
(
FNAME
+
".dyncUpdateForMap"
,
params
);
}
}
src/main/java/com/brilliance/mda/support/mybatis/entity/Column.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
entity
;
import
java.lang.annotation.*
;
@Inherited
@Target
(
ElementType
.
FIELD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
Column
{
public
String
value
();
}
src/main/java/com/brilliance/mda/support/mybatis/entity/Table.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
entity
;
import
java.lang.annotation.*
;
@Inherited
@Target
(
ElementType
.
TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
Table
{
public
String
value
();
}
src/main/java/com/brilliance/mda/support/mybatis/listener/StartListener.java
0 → 100644
View file @
6319f3ee
package
com
.
brilliance
.
mda
.
support
.
mybatis
.
listener
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.event.ContextRefreshedEvent
;
import
org.springframework.stereotype.Component
;
@Component
public
class
StartListener
implements
ApplicationListener
<
ContextRefreshedEvent
>{
@Override
public
void
onApplicationEvent
(
ContextRefreshedEvent
event
)
{
ApplicationContext
context
=
event
.
getApplicationContext
();
}
}
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