Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
smileNicky
taoshop
提交
9391325a
T
taoshop
项目概览
smileNicky
/
taoshop
通知
4
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
2
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
T
taoshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9391325a
编写于
12月 17, 2018
作者:
N
nicky
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加Mybatis sql拦截器
上级
96f57342
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
14 addition
and
116 deletion
+14
-116
src/taoshop-common/taoshop-common-core/src/main/java/com/muses/taoshop/common/core/database/config/MybatisSqlInterceptor.java
...op/common/core/database/config/MybatisSqlInterceptor.java
+13
-111
src/taoshop-common/taoshop-common-core/src/test/java/org/muses/commo/MybatisSqlInterceptor.java
.../src/test/java/org/muses/commo/MybatisSqlInterceptor.java
+1
-5
未找到文件。
src/taoshop-common/taoshop-common-core/src/main/java/com/muses/taoshop/common/core/database/config/MybatisSqlInterceptor.java
浏览文件 @
9391325a
package
com.muses.taoshop.common.core.database.config
;
/**
* Author:ZhuShangJin
* Date:2018/8/23
*/
package
com.muses.taoshop.common.core.database.config
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.ibatis.executor.Executor
;
import
org.apache.ibatis.mapping.BoundSql
;
import
org.apache.ibatis.mapping.MappedStatement
;
import
org.apache.ibatis.mapping.SqlCommandType
;
import
org.apache.ibatis.mapping.SqlSource
;
import
org.apache.ibatis.plugin.*
;
import
org.apache.ibatis.reflection.DefaultReflectorFactory
;
import
org.apache.ibatis.reflection.MetaObject
;
import
org.apache.ibatis.reflection.factory.DefaultObjectFactory
;
import
org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory
;
import
org.apache.ibatis.session.ResultHandler
;
import
org.apache.ibatis.session.RowBounds
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
java.sql.SQLException
;
import
java.util.Properties
;
@Component
...
...
@@ -28,22 +19,20 @@ import java.util.Properties;
args
=
{
MappedStatement
.
class
,
Object
.
class
,
RowBounds
.
class
,
ResultHandler
.
class
}))
public
class
MybatisSqlInterceptor
implements
Interceptor
{
Logger
logger
=
LoggerFactory
.
getLogger
(
MybatisSqlInterceptor
.
class
);
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
MybatisSqlInterceptor
.
class
);
@Override
public
Object
intercept
(
Invocation
invocation
)
throws
Throwable
{
// 获取sql
String
sql
=
getSqlByInvocation
(
invocation
);
logger
.
info
(
"获取到的SQL:{}"
+
sql
);
// 拦截sql
Object
[]
args
=
invocation
.
getArgs
();
MappedStatement
statement
=
(
MappedStatement
)
args
[
0
];
Object
parameterObject
=
args
[
1
];
BoundSql
boundSql
=
statement
.
getBoundSql
(
parameterObject
);
String
sql
=
boundSql
.
getSql
();
LOGGER
.
info
(
"获取到的SQL:{}"
+
sql
);
if
(
StringUtils
.
isBlank
(
sql
))
{
return
invocation
.
proceed
();
}
// sql交由处理类处理 对sql语句进行处理 此处是范例 不做任何处理
String
sql2Reset
=
sql
;
// 包装sql后,重置到invocation中
resetSql2Invocation
(
invocation
,
sql2Reset
);
// 返回,继续执行
// 返回
return
invocation
.
proceed
();
}
...
...
@@ -53,93 +42,6 @@ public class MybatisSqlInterceptor implements Interceptor {
}
@Override
public
void
setProperties
(
Properties
arg0
)
{
// doSomething
}
/**
* 获取sql语句
* @param invocation
* @return
*/
private
String
getSqlByInvocation
(
Invocation
invocation
)
{
final
Object
[]
args
=
invocation
.
getArgs
();
MappedStatement
ms
=
(
MappedStatement
)
args
[
0
];
Object
parameterObject
=
args
[
1
];
BoundSql
boundSql
=
ms
.
getBoundSql
(
parameterObject
);
return
boundSql
.
getSql
();
}
/**
* 包装sql后,重置到invocation中
* @param invocation
* @param sql
* @throws SQLException
*/
private
void
resetSql2Invocation
(
Invocation
invocation
,
String
sql
)
throws
SQLException
{
final
Object
[]
args
=
invocation
.
getArgs
();
MappedStatement
statement
=
(
MappedStatement
)
args
[
0
];
Object
parameterObject
=
args
[
1
];
BoundSql
boundSql
=
statement
.
getBoundSql
(
parameterObject
);
MappedStatement
newStatement
=
newMappedStatement
(
statement
,
new
BoundSqlSqlSource
(
boundSql
));
MetaObject
msObject
=
MetaObject
.
forObject
(
newStatement
,
new
DefaultObjectFactory
(),
new
DefaultObjectWrapperFactory
(),
new
DefaultReflectorFactory
());
msObject
.
setValue
(
"sqlSource.boundSql.sql"
,
sql
);
args
[
0
]
=
newStatement
;
}
private
MappedStatement
newMappedStatement
(
MappedStatement
ms
,
SqlSource
newSqlSource
)
{
MappedStatement
.
Builder
builder
=
new
MappedStatement
.
Builder
(
ms
.
getConfiguration
(),
ms
.
getId
(),
newSqlSource
,
ms
.
getSqlCommandType
());
builder
.
resource
(
ms
.
getResource
());
builder
.
fetchSize
(
ms
.
getFetchSize
());
builder
.
statementType
(
ms
.
getStatementType
());
builder
.
keyGenerator
(
ms
.
getKeyGenerator
());
if
(
ms
.
getKeyProperties
()
!=
null
&&
ms
.
getKeyProperties
().
length
!=
0
)
{
StringBuilder
keyProperties
=
new
StringBuilder
();
for
(
String
keyProperty
:
ms
.
getKeyProperties
())
{
keyProperties
.
append
(
keyProperty
).
append
(
","
);
}
keyProperties
.
delete
(
keyProperties
.
length
()
-
1
,
keyProperties
.
length
());
builder
.
keyProperty
(
keyProperties
.
toString
());
}
builder
.
timeout
(
ms
.
getTimeout
());
builder
.
parameterMap
(
ms
.
getParameterMap
());
builder
.
resultMaps
(
ms
.
getResultMaps
());
builder
.
resultSetType
(
ms
.
getResultSetType
());
builder
.
cache
(
ms
.
getCache
());
builder
.
flushCacheRequired
(
ms
.
isFlushCacheRequired
());
builder
.
useCache
(
ms
.
isUseCache
());
return
builder
.
build
();
}
private
String
getOperateType
(
Invocation
invocation
)
{
final
Object
[]
args
=
invocation
.
getArgs
();
MappedStatement
ms
=
(
MappedStatement
)
args
[
0
];
SqlCommandType
commondType
=
ms
.
getSqlCommandType
();
if
(
commondType
.
compareTo
(
SqlCommandType
.
SELECT
)
==
0
)
{
return
"select"
;
}
if
(
commondType
.
compareTo
(
SqlCommandType
.
INSERT
)
==
0
)
{
return
"insert"
;
}
if
(
commondType
.
compareTo
(
SqlCommandType
.
UPDATE
)
==
0
)
{
return
"update"
;
}
if
(
commondType
.
compareTo
(
SqlCommandType
.
DELETE
)
==
0
)
{
return
"delete"
;
}
return
null
;
}
// 定义一个内部辅助类,作用是包装sq
class
BoundSqlSqlSource
implements
SqlSource
{
private
BoundSql
boundSql
;
public
BoundSqlSqlSource
(
BoundSql
boundSql
)
{
this
.
boundSql
=
boundSql
;
}
@Override
public
BoundSql
getBoundSql
(
Object
parameterObject
)
{
return
boundSql
;
}
}
public
void
setProperties
(
Properties
arg0
)
{}
}
\ No newline at end of file
src/taoshop-common/taoshop-common-core/src/test/java/org/muses/commo/MybatisSqlInterceptor.java
浏览文件 @
9391325a
package
org.muses.commo
;
/**
* Author:ZhuShangJin
* Date:2018/8/23
*/
package
org.muses.commo
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.ibatis.executor.Executor
;
import
org.apache.ibatis.mapping.BoundSql
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录