Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. package com.mobigen.demo.web.interceptors;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.sql.Statement;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Properties;
  8.  
  9. import org.apache.ibatis.executor.statement.StatementHandler;
  10. import org.apache.ibatis.mapping.BoundSql;
  11. import org.apache.ibatis.mapping.ParameterMapping;
  12. import org.apache.ibatis.plugin.Interceptor;
  13. import org.apache.ibatis.plugin.Intercepts;
  14. import org.apache.ibatis.plugin.Invocation;
  15. import org.apache.ibatis.plugin.Plugin;
  16. import org.apache.ibatis.plugin.Signature;
  17. import org.apache.ibatis.session.ResultHandler;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20.  
  21.  
  22. @Intercepts({ @Signature(type = StatementHandler.class, method = "update", args = { Statement.class }),
  23. @Signature(type = StatementHandler.class, method = "query", args = { Statement.class, ResultHandler.class }) })
  24.  
  25. public class MybatisSqlLogInterceptor implements Interceptor{
  26. private static Logger logger = LoggerFactory.getLogger("MYBATIS_SQL_LOG");
  27.  
  28. @Override
  29. public Object intercept(Invocation invocation) throws Throwable {
  30. StatementHandler handler = (StatementHandler) invocation.getTarget();
  31. String sql = bindSql(handler); // SQL 추출
  32.  
  33. logger.info("=====================================================================");
  34. logger.info("{} ", sql);
  35. logger.info("=====================================================================");
  36. return invocation.proceed();
  37. }
  38.  
  39. /**
  40. * <pre>
  41. * bindSql
  42. *
  43. * <pre>
  44. *
  45. * @param boundSql
  46. * @param sql
  47. * @param param
  48. * @return
  49. * @throws NoSuchFieldException
  50. * @throws IllegalAccessException
  51. */
  52. @SuppressWarnings("rawtypes")
  53. private String bindSql(StatementHandler handler) throws NoSuchFieldException, IllegalAccessException {
  54. BoundSql boundSql = handler.getBoundSql();
  55.  
  56. // 쿼리실행시 맵핑되는 파라미터를 구한다
  57. Object param = handler.getParameterHandler().getParameterObject();
  58. // 쿼리문을 가져온다(이 상태에서의 쿼리는 값이 들어갈 부분에 ?가 있다)
  59. String sql = boundSql.getSql();
  60.  
  61. // 바인딩 파라미터가 없으면
  62. if (param == null) {
  63. sql = sql.replaceFirst("\\?", "''");
  64. return sql;
  65. }
  66.  
  67. // 해당 파라미터의 클래스가 Integer, Long, Float, Double 클래스일 경우
  68. if (param instanceof Integer || param instanceof Long || param instanceof Float || param instanceof Double) {
  69. sql = sql.replaceFirst("\\?", param.toString());
  70. }
  71. // 해당 파라미터의 클래스가 String인 경우
  72. else if (param instanceof String) {
  73. sql = sql.replaceFirst("\\?", "'" + param + "'");
  74. }
  75. // 해당 파라미터의 클래스가 Map인 경우
  76. else if (param instanceof Map) {
  77. List<ParameterMapping> paramMapping = boundSql.getParameterMappings();
  78. for (ParameterMapping mapping : paramMapping) {
  79. String propValue = mapping.getProperty();
  80. Object value = ((Map) param).get(propValue);
  81. if (value == null) {
  82. continue;
  83. }
  84.  
  85. if (value instanceof String) {
  86. sql = sql.replaceFirst("\\?", "'" + value + "'");
  87. } else {
  88. sql = sql.replaceFirst("\\?", value.toString());
  89. }
  90. }
  91. }
  92. // 해당 파라미터의 클래스가 사용자 정의 클래스인 경우
  93. else {
  94. List<ParameterMapping> paramMapping = boundSql.getParameterMappings();
  95. Class<? extends Object> paramClass = param.getClass();
  96.  
  97. for (ParameterMapping mapping : paramMapping) {
  98. String propValue = mapping.getProperty();
  99. Field field = paramClass.getDeclaredField(propValue);
  100. field.setAccessible(true);
  101. Class<?> javaType = mapping.getJavaType();
  102. if (String.class == javaType) {
  103. sql = sql.replaceFirst("\\?", "'" + field.get(param) + "'");
  104. } else {
  105. sql = sql.replaceFirst("\\?", field.get(param).toString());
  106. }
  107. }
  108. }
  109.  
  110. // return sql
  111. return sql;
  112. }
  113.  
  114. @Override
  115. public Object plugin(Object target) {
  116. return Plugin.wrap(target, this);
  117. }
  118.  
  119. @Override
  120. public void setProperties(Properties properties) {
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement