-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
329ac3c
commit c7c35cf
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
Submodule angular
updated
10 files
+0 −1 | .env | |
+4 −1 | .gitignore | |
+1,278 −758 | .yarn/releases/yarn-1.22.22.cjs | |
+1 −1 | .yarnrc | |
+1 −1 | angular.json | |
+18 −17 | package.json | |
+12 −0 | src/proxy.conf.js | |
+0 −8 | src/proxy.conf.json | |
+0 −8 | src/proxy.conf.online.json | |
+144 −115 | yarn.lock |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/daming/hoteler/config/MyBatisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.daming.hoteler.config; | ||
|
||
import org.daming.hoteler.repository.interceptors.SqlInterceptor; | ||
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author gming001 | ||
* @version 2024-06-16 10:01 | ||
*/ | ||
@Configuration | ||
public class MyBatisConfig { | ||
|
||
@Bean | ||
public ConfigurationCustomizer mybatisConfigurationCustomizer() { | ||
return configuration -> configuration.addInterceptor(new SqlInterceptor()); | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
src/main/java/org/daming/hoteler/repository/interceptors/SqlInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.daming.hoteler.repository.interceptors; | ||
|
||
import org.apache.ibatis.cache.CacheKey; | ||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.mapping.ParameterMapping; | ||
import org.apache.ibatis.plugin.Interceptor; | ||
import org.apache.ibatis.plugin.Intercepts; | ||
import org.apache.ibatis.plugin.Invocation; | ||
import org.apache.ibatis.plugin.Plugin; | ||
import org.apache.ibatis.plugin.Signature; | ||
import org.apache.ibatis.session.ResultHandler; | ||
import org.apache.ibatis.session.RowBounds; | ||
import org.daming.hoteler.base.logger.SqlLoggerUtil; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author gming001 | ||
* @version 2024-06-16 09:29 | ||
*/ | ||
@Intercepts({ | ||
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }), | ||
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }), | ||
@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class} ) | ||
}) | ||
public class SqlInterceptor implements Interceptor { | ||
|
||
@Override | ||
public Object intercept(Invocation invocation) throws Throwable { | ||
var in = Instant.now(); | ||
String sql = ""; | ||
Object[] params = new Object[0]; | ||
try { | ||
Object proceed = invocation.proceed(); | ||
|
||
return proceed; | ||
} catch (Exception ex) { | ||
if (ObjectUtils.isEmpty(sql)) { | ||
sql = this.getSqlStatement(invocation); | ||
} | ||
if (ObjectUtils.isEmpty(params)) { | ||
params = this.getSqlStatementParameter(invocation); | ||
} | ||
SqlLoggerUtil.logSqlException(sql, params, ex); | ||
throw ex; | ||
} finally { | ||
if (ObjectUtils.isEmpty(sql)) { | ||
sql = this.getSqlStatement(invocation); | ||
} | ||
if (ObjectUtils.isEmpty(params)) { | ||
params = this.getSqlStatementParameter(invocation); | ||
} | ||
SqlLoggerUtil.logSql(sql, params, Duration.between(in, Instant.now())); | ||
} | ||
} | ||
|
||
@Override | ||
public Object plugin(Object target) { | ||
return Plugin.wrap(target,this); | ||
} | ||
|
||
private String getSqlStatement(Invocation invocation) { | ||
var statement = (MappedStatement)invocation.getArgs()[0]; | ||
Object parameter = null; | ||
if (invocation.getArgs().length > 1) { | ||
parameter = invocation.getArgs()[1]; | ||
} | ||
var boundSql = statement.getBoundSql(parameter); | ||
var sql = boundSql.getSql(); | ||
sql = sql.replaceAll("[\\s]+", " "); | ||
|
||
return sql; | ||
} | ||
|
||
private Object[] getSqlStatementParameter(Invocation invocation) { | ||
var statement = (MappedStatement)invocation.getArgs()[0]; | ||
Object parameter = null; | ||
if (invocation.getArgs().length > 1) { | ||
parameter = invocation.getArgs()[1]; | ||
} | ||
var configuration = statement.getConfiguration(); | ||
var boundSql = statement.getBoundSql(parameter); | ||
Object parameterObject = boundSql.getParameterObject(); | ||
List<ParameterMapping> params = boundSql.getParameterMappings(); | ||
List<Object> args = new ArrayList<>(); | ||
if (!ObjectUtils.isEmpty(params) && !ObjectUtils.isEmpty(parameterObject)) { | ||
var typeHandlerRegistry = configuration.getTypeHandlerRegistry(); | ||
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { | ||
if (parameterObject instanceof Object[]) { | ||
Stream.of(parameterObject).forEach(args::add); | ||
} else { | ||
args.add(parameterObject); | ||
} | ||
|
||
} else { | ||
for (ParameterMapping param : params) { | ||
var propertyName = param.getProperty(); | ||
var metaObject = configuration.newMetaObject(parameterObject); | ||
if (metaObject.hasGetter(propertyName)) { | ||
Object obj = metaObject.getValue(propertyName); | ||
args.add(obj); | ||
} else if (boundSql.hasAdditionalParameter(propertyName)) { | ||
Object obj = boundSql.getAdditionalParameter(propertyName); | ||
args.add(obj); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return args.toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters