MybatisSqlIntercept.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.dk.common.infrastructure.util;
  2. import com.alibaba.fastjson.JSON;
  3. import org.apache.ibatis.builder.StaticSqlSource;
  4. import org.apache.ibatis.executor.Executor;
  5. import org.apache.ibatis.mapping.MappedStatement;
  6. import org.apache.ibatis.mapping.SqlSource;
  7. import org.apache.ibatis.plugin.Interceptor;
  8. import org.apache.ibatis.plugin.Intercepts;
  9. import org.apache.ibatis.plugin.Invocation;
  10. import org.apache.ibatis.plugin.Signature;
  11. import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
  12. import org.apache.ibatis.scripting.defaults.RawSqlSource;
  13. import org.apache.ibatis.session.ResultHandler;
  14. import org.apache.ibatis.session.RowBounds;
  15. import org.springframework.stereotype.Component;
  16. import java.beans.BeanInfo;
  17. import java.beans.Introspector;
  18. import java.beans.PropertyDescriptor;
  19. import java.lang.reflect.Field;
  20. import java.lang.reflect.Method;
  21. import java.lang.reflect.Proxy;
  22. import java.util.HashMap;
  23. import java.util.LinkedHashMap;
  24. import java.util.Map;
  25. /**
  26. * @author 沈博
  27. * @date_time 2022年3月11日14:10:19
  28. * @description 拦截方法添加国际化参数 (目前有点小问题 url拼参数的无法添加)
  29. * TODO selectById的非Map类型参数的暂不支持 待修改
  30. */
  31. @Component
  32. @Intercepts({
  33. @Signature(type = Executor.class, method = "query", args = {MappedStatement.class,
  34. Object.class, RowBounds.class, ResultHandler.class})})
  35. public class MybatisSqlIntercept implements Interceptor {
  36. public static String I18N = "";
  37. public static Integer cpId = 0;
  38. @Override
  39. public Object intercept(Invocation invocation) throws Throwable {
  40. Object[] args = invocation.getArgs();
  41. Object[] args2 = new Object[4];
  42. args2[3] = args[3];
  43. args2[2] = args[2];
  44. if ((args[1] instanceof Map || args[1] instanceof Object) && checkType(args)) {
  45. Object argsNo2 = args[1];
  46. //map类型,对于数字的转换,如果直接转,Long给转成了Integer
  47. Map<String, Object> argsNo2Map = null;
  48. if (args[1] instanceof Map) {
  49. argsNo2Map = new HashMap<>();
  50. for (String key : ((Map<String, Object>) args[1]).keySet()) {
  51. argsNo2Map.put(key, ((Map<String, Object>) args[1]).get(key));
  52. }
  53. } else {
  54. try {
  55. argsNo2Map = convertBeanToMap(args[1]);
  56. } catch (Exception ignored) {
  57. }
  58. }
  59. if (argsNo2Map != null) {
  60. argsNo2Map.put("i18n", I18N);
  61. if(cpId != 0){
  62. argsNo2Map.put("cpId", cpId);
  63. }
  64. args2[1] = argsNo2Map;
  65. }
  66. } else {
  67. args2[1] = args[1];
  68. //TODO 在此添加单参数的逻辑 做法是获取父类的方法的参数名 然后把单参数转成Map传给xml
  69. }
  70. args2[0] = args[0];
  71. Invocation newInvocation = new Invocation(
  72. invocation.getTarget(), invocation.getMethod(), args2
  73. );
  74. // MappedStatement mappedStatement = (MappedStatement) args[0];
  75. // String cmdType = mappedStatement.getSqlCommandType().name();
  76. // if ("update".equalsIgnoreCase(cmdType)) {
  77. // List<Map<String,Object>> objList = SpringUtils.getBean(PlugMapper.class)
  78. // }
  79. return newInvocation.proceed();
  80. }
  81. private boolean checkType(Object[] args) {
  82. return !(args[1] instanceof Long) && !(args[1] instanceof Integer) && !(args[1] instanceof String);
  83. }
  84. /**
  85. * @desc : 实体转Map
  86. * @author : 洪旭东
  87. * @date : 2023-03-27 11:18
  88. */
  89. public Map<String, Object> convertBeanToMap(Object obj) {
  90. if (obj == null) {
  91. return null;
  92. }
  93. Map<String, Object> map = new HashMap<>(16);
  94. try {
  95. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  96. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  97. for (PropertyDescriptor property : propertyDescriptors) {
  98. String key = property.getName();
  99. // 过滤class属性
  100. if (!key.equals("class")) {
  101. // 得到property对应的getter方法
  102. Method getter = property.getReadMethod();
  103. Object value = getter.invoke(obj);
  104. if (null != value) {
  105. map.put(key, value);
  106. }
  107. }
  108. }
  109. } catch (Exception ignore) {
  110. return null;
  111. }
  112. return map;
  113. }
  114. }