mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
将getFullSql方法从ConsoleMessageCollector移入AuditMessage
update docs: 将sql打印到日志中,可以通过配置日志级别控制是否输出sql,通过配置日志Appender控制sql输出目的地 补充TableConfig和ColumnConfig中增加的字段及注释
This commit is contained in:
parent
4204a57a95
commit
c7cb9e881f
@ -185,6 +185,9 @@ public class TableConfig {
|
|||||||
|
|
||||||
|
|
||||||
private Class<? extends UpdateListener> updateListenerClass;
|
private Class<? extends UpdateListener> updateListenerClass;
|
||||||
|
|
||||||
|
// 是否启用ATP生成Mapper
|
||||||
|
private Boolean mapperGenerateEnable = Boolean.TRUE;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -211,6 +214,9 @@ public class ColumnConfig implements Serializable {
|
|||||||
private KeyType keyType;
|
private KeyType keyType;
|
||||||
private String keyValue;
|
private String keyValue;
|
||||||
private Boolean keyBefore;
|
private Boolean keyBefore;
|
||||||
|
|
||||||
|
// 是否是租户列
|
||||||
|
private Boolean tenantId;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,30 @@ Flex exec sql taked 3 ms >>> INSERT INTO `tb_account`(`user_name`, `age`, `birt
|
|||||||
|
|
||||||
控制台输出了完整的 SQL,以及 SQL 执行消耗时间,方便我们在开发的时候,对慢 SQL 进行排查和快速定位。
|
控制台输出了完整的 SQL,以及 SQL 执行消耗时间,方便我们在开发的时候,对慢 SQL 进行排查和快速定位。
|
||||||
|
|
||||||
|
或者在spring工程里,将sql打印到日志中,可以通过配置日志级别控制是否输出sql,通过配置日志Appender控制sql输出目的地。
|
||||||
|
```java
|
||||||
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MyBatisFlexConfiguration {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql");
|
||||||
|
|
||||||
|
public MyBatisFlexConfiguration() {
|
||||||
|
//开启审计功能
|
||||||
|
AuditManager.setAuditEnable(true);
|
||||||
|
|
||||||
|
//设置 SQL 审计收集器
|
||||||
|
AuditManager.setMessageCollector(auditMessage ->
|
||||||
|
logger.info("{},{}ms", auditMessage.getFullSql(), auditMessage.getElapsedTime())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## p6spy 方案
|
## p6spy 方案
|
||||||
|
|
||||||
我们可以把数据源配置为 p6spy 数据源,使用 p6spy 的 SQL 输出功能进行 SQL 打印。更多文档参考 p6spy 官方文档:
|
我们可以把数据源配置为 p6spy 数据源,使用 p6spy 的 SQL 输出功能进行 SQL 打印。更多文档参考 p6spy 官方文档:
|
||||||
|
|||||||
@ -16,16 +16,20 @@
|
|||||||
package com.mybatisflex.core.audit;
|
package com.mybatisflex.core.audit;
|
||||||
|
|
||||||
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
||||||
|
import com.mybatisflex.core.util.DateUtil;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
public class AuditMessage implements Serializable {
|
public class AuditMessage implements Serializable {
|
||||||
|
|
||||||
@ -147,6 +151,38 @@ public class AuditMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFullSql() {
|
||||||
|
String sql = getQuery();
|
||||||
|
List<Object> params = getQueryParams();
|
||||||
|
if (params != null) {
|
||||||
|
for (Object value : params) {
|
||||||
|
// null
|
||||||
|
if (value == null) {
|
||||||
|
sql = sql.replaceFirst("\\?", "null");
|
||||||
|
}
|
||||||
|
// number
|
||||||
|
else if (value instanceof Number || value instanceof Boolean) {
|
||||||
|
sql = sql.replaceFirst("\\?", value.toString());
|
||||||
|
}
|
||||||
|
// other
|
||||||
|
else {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("'");
|
||||||
|
if (value instanceof Date) {
|
||||||
|
sb.append(DateUtil.toDateTimeString((Date) value));
|
||||||
|
} else if (value instanceof LocalDateTime) {
|
||||||
|
sb.append(DateUtil.toDateTimeString(DateUtil.toDate((LocalDateTime) value)));
|
||||||
|
} else {
|
||||||
|
sb.append(value);
|
||||||
|
}
|
||||||
|
sb.append("'");
|
||||||
|
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(sb.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
private PreparedStatement createPreparedStatement() {
|
private PreparedStatement createPreparedStatement() {
|
||||||
return (PreparedStatement) Proxy.newProxyInstance(
|
return (PreparedStatement) Proxy.newProxyInstance(
|
||||||
AuditMessage.class.getClassLoader(),
|
AuditMessage.class.getClassLoader(),
|
||||||
|
|||||||
@ -41,38 +41,7 @@ public class ConsoleMessageCollector implements MessageCollector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void collect(AuditMessage message) {
|
public void collect(AuditMessage message) {
|
||||||
String sql = getFullSql(message.getQuery(), message.getQueryParams());
|
printer.print(message.getFullSql(), message.getElapsedTime());
|
||||||
printer.print(sql, message.getElapsedTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getFullSql(String sql, List<Object> params) {
|
|
||||||
if (params != null) {
|
|
||||||
for (Object value : params) {
|
|
||||||
// null
|
|
||||||
if (value == null) {
|
|
||||||
sql = sql.replaceFirst("\\?", "null");
|
|
||||||
}
|
|
||||||
// number
|
|
||||||
else if (value instanceof Number || value instanceof Boolean) {
|
|
||||||
sql = sql.replaceFirst("\\?", value.toString());
|
|
||||||
}
|
|
||||||
// other
|
|
||||||
else {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("'");
|
|
||||||
if (value instanceof Date) {
|
|
||||||
sb.append(DateUtil.toDateTimeString((Date) value));
|
|
||||||
} else if (value instanceof LocalDateTime) {
|
|
||||||
sb.append(DateUtil.toDateTimeString(DateUtil.toDate((LocalDateTime) value)));
|
|
||||||
} else {
|
|
||||||
sb.append(value);
|
|
||||||
}
|
|
||||||
sb.append("'");
|
|
||||||
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(sb.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sql;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface SqlDebugPrinter {
|
public interface SqlDebugPrinter {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user