Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 package com.mandi.system.utils;
A 2
3 import org.apache.ibatis.executor.Executor;
4 import org.apache.ibatis.mapping.BoundSql;
5 import org.apache.ibatis.mapping.MappedStatement;
6 import org.apache.ibatis.mapping.ParameterMapping;
7 import org.apache.ibatis.mapping.ParameterMode;
8 import org.apache.ibatis.plugin.*;
9 import org.apache.ibatis.reflection.MetaObject;
10 import org.apache.ibatis.session.Configuration;
11 import org.apache.ibatis.session.ResultHandler;
12 import org.apache.ibatis.session.RowBounds;
13 import org.apache.ibatis.type.TypeHandlerRegistry;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17
18
19 import com.mandi.common.Jacksonmethod;
20
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.regex.Matcher;
27
28 @Intercepts({
29         @Signature(type = Executor.class, method = "query", args = {
30                 MappedStatement.class, Object.class, RowBounds.class,
31                 ResultHandler.class }),
32         @Signature(type = Executor.class, method = "update", args = {
33                 MappedStatement.class, Object.class }), })
34 public class MybatisSqlInterceptor implements Interceptor  {
35
36     private Logger log = LoggerFactory
37             .getLogger(MybatisSqlInterceptor.class);
38     private Properties properties;
39     private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
40             "yyyy-MM-dd HH:mm:ss");
41     private static final DateFormat  SSS_FORMAT = new SimpleDateFormat(
42             "hh:mm:ss.SSS");
43
44     @Override
45     public Object intercept(Invocation invocation) throws Throwable{
46         MappedStatement mappedStatement = (MappedStatement) invocation
47                 .getArgs()[0];
48         Object parameterObject = null;
49         if (invocation.getArgs().length > 1) {
50             parameterObject = invocation.getArgs()[1];
51         }
52         long start = System.currentTimeMillis();
53         if(log.isInfoEnabled())
54             log.info("s::intercept::"+mappedStatement.getId()+";sT:"+SSS_FORMAT.format(new Date()));
55         Object result= invocation.proceed();
56         long end = System.currentTimeMillis();
57         if(log.isInfoEnabled())
58             log.info("intercept::;eT:"+SSS_FORMAT.format(new Date())+";params::"+Jacksonmethod.tojson(parameterObject, false));
59         long timing = end - start;
60
61 //        BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
62 //        Configuration configuration = mappedStatement.getConfiguration();
63 //        String sql = getSql(boundSql, parameterObject, configuration);
64         if (log.isInfoEnabled()&&timing>Long.parseLong(String.valueOf(properties.get("notifyTime")))) {
65             log.info("耗时:" + timing + " ms" );
66         }
67         return result;
68     }
69
70     @Override
71     public Object plugin(Object target) {
72         if (target instanceof Executor) {
73             return Plugin.wrap(target, this);
74         }
75         return target;
76     }
77
78     @Override
79     public void setProperties(Properties properties) {
80          this.properties = properties;
81     }
82
83     private String getSql(BoundSql boundSql, Object parameterObject,
84             Configuration configuration) {
85         String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
86         List<ParameterMapping> parameterMappings = boundSql
87                 .getParameterMappings();
88         TypeHandlerRegistry typeHandlerRegistry = configuration
89                 .getTypeHandlerRegistry();
90         if (parameterMappings != null) {
91             for (int i = 0; i < parameterMappings.size(); i++) {
92                 ParameterMapping parameterMapping = parameterMappings.get(i);
93                 if (parameterMapping.getMode() != ParameterMode.OUT) {
94                     Object value;
95                     String propertyName = parameterMapping.getProperty();
96                     if (boundSql.hasAdditionalParameter(propertyName)) {
97                         value = boundSql.getAdditionalParameter(propertyName);
98                     } else if (parameterObject == null) {
99                         value = null;
100                     } else if (typeHandlerRegistry
101                             .hasTypeHandler(parameterObject.getClass())) {
102                         value = parameterObject;
103                     } else {
104                         MetaObject metaObject = configuration
105                                 .newMetaObject(parameterObject);
106                         value = metaObject.getValue(propertyName);
107                     }
108                     sql = replacePlaceholder(sql, value);
109                 }
110             }
111         }
112         return sql;
113     }
114
115     private String replacePlaceholder(String sql, Object propertyValue) {
116         String result;
117         if (propertyValue != null) {
118             if (propertyValue instanceof String) {
119                 result = "'" + propertyValue + "'";
120             } else if (propertyValue instanceof Date) {
121                 result = "'" + DATE_FORMAT.format(propertyValue) + "'";
122             } else {
123                 result = propertyValue.toString();
124             }
125         } else {
126             result = "null";
127         }
128         return sql.replaceFirst("\\?", Matcher.quoteReplacement(result));
129     }
130 }