参考文章Mybatis插件及示例—-打印每条Sql语句及其执行时间

mybatis有基本的sql日志,但是看起来很不舒服,sql一行参数在另一行,查询多的时候sql行和参数行分开很远。下面就是相同颜色的是一组:

看了大哥的文章决定试试弄个美化日志的mybatis插件,其中还增加了对通用mapper Example查询、日期参数和自定义String[] TypeHandle参数的处理,废话不多说上代码:


@Component @Profile({"dev", "test"}) @Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}), @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}), @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})}) @Slf4j public class SqlCastInInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object target = invocation.getTarget(); long startTime = System.currentTimeMillis(); StatementHandler statementHandler = (StatementHandler) target; int size = 0; try { Object o = invocation.proceed(); if (isList(o.getClass())) { size = ((List) o).size(); } return o; } finally { long endTime = System.currentTimeMillis(); long sqlCost = endTime - startTime; BoundSql boundSql = statementHandler.getBoundSql(); String sql = boundSql.getSql(); Object parameterObject = boundSql.getParameterObject(); List<ParameterMapping> parameterMappingList = boundSql.getParameterMappings(); // 格式化Sql语句,去除换行符,替换参数 sql = formatSql(sql, parameterObject, parameterMappingList); log.debug("SQL:[" + sql + "]执行耗时[" + sqlCost + "ms]结果集[" + size + "]"); } } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { log.info("set properties"); } /** * @param sql 没花钱sql * @param parameterObject 参数 * @param parameterMappingList 参数map * @return 美化后sql * 增加代码可以处理通用mapper的Example参数美化sql */ @SuppressWarnings("unchecked") private String formatSql(String sql, Object parameterObject, List<ParameterMapping> parameterMappingList) { // 输入sql字符串空判断 if (sql == null || sql.length() == 0) { return ""; } // 美化sql sql = beautifySql(sql); // 不传参数的场景,直接把Sql美化一下返回出去 if (parameterObject == null || CollectionUtils.isEmpty(parameterMappingList)) { return sql; } // 定义一个没有替换过占位符的sql,用于出异常时返回 String sqlWithoutReplacePlaceholder = sql; final PageExample pageExample = tryPageExample(parameterObject); try { Class<?> parameterObjectClass = parameterObject.getClass(); // 如果参数是StrictMap且Value类型为Collection,获取key="list"的属性,这里主要是为了处理<foreach>循环时传入List这种参数的占位符替换 // 例如select * from xxx where id in <foreach collection="list">...</foreach> if (Objects.nonNull(pageExample)) { sql = this.handlePageExampleParameter(sql, pageExample, parameterMappingList); } else if (isStrictMap(parameterObjectClass)) { DefaultSqlSession.StrictMap<Collection<?>> strictMap = (DefaultSqlSession.StrictMap<Collection<?>>) parameterObject; if (isList(strictMap.get("list").getClass())) { sql = handleListParameter(sql, strictMap.get("list"), parameterMappingList); } } else if (isMap(parameterObjectClass)) { // 如果参数是Map则直接强转,通过map.get(key)方法获取真正的属性值 // 这里主要是为了处理<insert>、<delete>、<update>、<select>时传入parameterType为map的场景 Map<?, ?> paramMap = (Map<?, ?>) parameterObject; sql = handleMapParameter(sql, paramMap, parameterMappingList); } else if (parameterObject instanceof Example) { sql = handleExampleParameter(sql, (Example) parameterObject, parameterMappingList); } else { // 通用场景,比如传的是一个自定义的对象或者八种基本数据类型之一或者String sql = handleCommonParameter(sql, parameterMappingList, parameterObjectClass, parameterObject); } } catch (Exception e) { log.error(e.getMessage(), e); // 占位符替换过程中出现异常,则返回没有替换过占位符但是格式美化过的sql,这样至少保证sql语句比BoundSql中的sql更好看 return sqlWithoutReplacePlaceholder; } return sql; } /** * 美化Sql */ private String beautifySql(String sql) { sql = sql.replaceAll("[\\s\n ]+", " "); return sql; } /** * @param sql 美化前sql * @param example 通用mapper tk Example * @param parameterMappingList 参数List * @return 美化含参数sql * @throws IllegalAccessException, IntrospectionException, InvocationTargetException * 处理tk Example 参数的sql */ private String handleExampleParameter(String sql, Example example, List<ParameterMapping> parameterMappingList) throws IllegalAccessException, IntrospectionException, InvocationTargetException { final List<Example.Criteria> oredCriteria = example.getOredCriteria(); sql = handleCriteria(sql, parameterMappingList, oredCriteria); return sql; } private String handlePageExampleParameter(String sql, PageExample example, List<ParameterMapping> parameterMappingList) throws IllegalAccessException, IntrospectionException, InvocationTargetException { final List<Example.Criteria> oredCriteria = example.getOredCriteria(); sql = handleCriteria(sql, parameterMappingList, oredCriteria); sql = sql.replaceFirst("\\?", example.getFirstPageHelper().toString() + "," + example.getSecondPageHelper().toString()); return sql; } private String handleCriteria(String sql, List<ParameterMapping> parameterMappingList, List<Example.Criteria> oredCriteria) throws IllegalAccessException, IntrospectionException, InvocationTargetException { for (Example.Criteria criteria : oredCriteria) { for (Example.Criterion criterion : criteria.getCriteria()) { final Object value = criterion.getValue(); if (Objects.isNull(value)) { continue; } if (isList(value.getClass())) { sql = handleListParameter(sql, (List) criterion.getValue(), parameterMappingList); } else { sql = handleCommonParameter(sql, parameterMappingList, value.getClass(), value); } } } return sql; } /** * 处理参数为List的场景 */ private String handleListParameter(String sql, Collection<?> col, List<ParameterMapping> parameterMappingList) { final Object[] objects = col.toArray(); final int groupSize = parameterMappingList.size() / objects.length; for (int i = 0; i < objects.length; i++) { sql = this.formatSql(sql, objects[i], parameterMappingList.subList(i * groupSize, (i + 1) * groupSize - 1)); } return sql; } /** * 处理参数为Map的场景 */ private String handleMapParameter(String sql, Map<?, ?> paramMap, List<ParameterMapping> parameterMappingList) { List<ParameterMapping> current = Lists.newArrayList(); final HashMap<Object, Object> currentMap = Maps.newHashMap(); currentMap.putAll(paramMap); for (ParameterMapping parameterMapping : parameterMappingList) { final String property = parameterMapping.getProperty(); final String fieldName = property.startsWith("__frch_record_") ? property.substring(property.indexOf(".") + 1) : property; Object propertyValue = currentMap.get(fieldName); current.add(parameterMapping); sql = this.formatSql(sql, propertyValue, current); current.clear(); } return sql; } /** * 处理通用的场景 */ private String handleCommonParameter(String sql, List<ParameterMapping> parameterMappingList, Class<?> parameterObjectClass, Object parameterObject) throws IntrospectionException, InvocationTargetException, IllegalAccessException { String propertyValue = "null"; // 基本数据类型或者基本数据类型的包装类,直接toString即可获取其真正的参数值,其余直接取paramterMapping中的property属性即可 if (isPrimitiveOrPrimitiveWrapper(parameterObjectClass)) { propertyValue = parameterObject.toString(); } else if (String.class.isAssignableFrom(parameterObjectClass)) { propertyValue = "\"" + parameterObject + "\""; } else if (Enum.class.isAssignableFrom(parameterObjectClass)) { //枚举存int值 propertyValue = String.valueOf(((Enum) parameterObject).ordinal()); } else if (parameterObject instanceof String[]) { //自定义的TypeHandler处理数组 propertyValue = String.join(",", ((String[]) parameterObject)); } else if (parameterObject instanceof Date || Temporal.class.isAssignableFrom(parameterObjectClass)) { //日期处理 propertyValue = "\"" + parameterObject.toString() + "\""; } else if (!CollectionUtils.isEmpty(parameterMappingList)) { for (ParameterMapping parameterMapping : parameterMappingList) { final String property = parameterMapping.getProperty(); final String fieldName = property.startsWith("__frch_record_") ? property.substring(property.indexOf(".") + 1) : property; final PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, parameterObjectClass); final Method readMethod = propertyDescriptor.getReadMethod(); final Object invoke = readMethod.invoke(parameterObject); if (Objects.isNull(invoke)) { sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(propertyValue)); } else { sql = this.formatSql(sql, ImmutableMap.of(fieldName, invoke), parameterMappingList); } } } if (!StringUtils.isEmpty(propertyValue)) { sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(propertyValue)); } return sql; } /** * 是否基本数据类型或者基本数据类型的包装类 */ private boolean isPrimitiveOrPrimitiveWrapper(Class<?> parameterObjectClass) { return parameterObjectClass.isPrimitive() || (parameterObjectClass.isAssignableFrom(Byte.class) || parameterObjectClass.isAssignableFrom(Short.class) || parameterObjectClass.isAssignableFrom(Integer.class) || parameterObjectClass.isAssignableFrom(Long.class) || parameterObjectClass.isAssignableFrom(Double.class) || parameterObjectClass.isAssignableFrom(Float.class) || parameterObjectClass.isAssignableFrom(Character.class) || parameterObjectClass.isAssignableFrom(Boolean.class)); } /** * 是否DefaultSqlSession的内部类StrictMap */ private boolean isStrictMap(Class<?> parameterObjectClass) { return DefaultSqlSession.StrictMap.class.isAssignableFrom(parameterObjectClass); } /** * 是否List的实现类 */ private boolean isList(Class<?> clazz) { return Collection.class.isAssignableFrom(clazz); } /** * 是否Map的实现类 */ private boolean isMap(Class<?> parameterObjectClass) { return Map.class.isAssignableFrom(parameterObjectClass); } @SuppressWarnings("unchecked") private PageExample tryPageExample(Object o) { try { if (!isMap(o.getClass())) { return null; } final Map map = (Map) o; final Integer firstPageHelper = (Integer) map.get("First_PageHelper"); final Integer secondPageHelper = (Integer) map.get("Second_PageHelper"); final List<Example.Criteria> oredCriteria = (List<Example.Criteria>) map.get("oredCriteria"); if (Objects.isNull(firstPageHelper) || Objects.isNull(secondPageHelper) || Objects.isNull(oredCriteria)) { return null; } return PageExample.builder() .firstPageHelper(firstPageHelper) .oredCriteria(oredCriteria) .secondPageHelper(secondPageHelper).build(); } catch (Exception e) { log.info("Not PageExample"); } return null; } @Data @Builder private static class PageExample { private List<Example.Criteria> oredCriteria; private Integer firstPageHelper; private Integer secondPageHelper; } }

最后看看结果吧,如图还是比较完美的:smile::smile::smile:


2018-09-06 11:42:02,810:DEBUG reactor-http-server-epoll-5 (SqlCastInInterceptor.java:56) - SQL:[select cmv.city as city, wcu.id as id, wcu.wx_cp_user_id as wx_cp_user_id, wcu.name as name, wcu.position as position, wcu.mobile as mobile, wcu.gender as gender, wcu.enable as enable, wcu.attr_counter_id as attr_counter_id, wcu.attr_counter_name as attr_counter_name, wcu.created_at as created_at from wechat_cp_user as wcu left join counter_maintenance_vendor cmv on cmv.counter_id = wcu.attr_counter_id where wx_cp_user_id = "wangjungang"]执行耗时[1ms] 2018-09-06 11:42:02,811:DEBUG reactor-http-server-epoll-5 (SqlCastInInterceptor.java:56) - SQL:[select id, name, deleted, created_at from maintenance_type where id =1]执行耗时[0ms] 2018-09-06 11:42:02,813:DEBUG reactor-http-server-epoll-5 (SqlCastInInterceptor.java:56) - SQL:[select id, name, telephone, address, deleted, created_at, project_leader, leader_mobile, leader_wechat, repairman, repairman_mobile, repairman_wechat from maintenance_vendor where id = 4]执行耗时[1ms]

1 对 “Mybatis插件-sql日志美化输出”的想法;

发表评论

电子邮件地址不会被公开。 必填项已用*标注