SqlInterceptor.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.edu.mybatis;
  2. import com.edu.mybatis.dialect.Dialect;
  3. import com.edu.mybatis.dialect.MySql5Dialect;
  4. import com.edu.mybatis.dialect.OracleDialect;
  5. import org.apache.ibatis.executor.statement.StatementHandler;
  6. import org.apache.ibatis.plugin.Interceptor;
  7. import org.apache.ibatis.plugin.Intercepts;
  8. import org.apache.ibatis.plugin.Invocation;
  9. import org.apache.ibatis.plugin.Plugin;
  10. import org.apache.ibatis.reflection.DefaultReflectorFactory;
  11. import org.apache.ibatis.reflection.MetaObject;
  12. import org.apache.ibatis.reflection.SystemMetaObject;
  13. import org.apache.ibatis.session.Configuration;
  14. import org.apache.ibatis.session.RowBounds;
  15. import java.sql.Connection;
  16. import java.util.Properties;
  17. /**
  18. * Created by KangJellen on 2017/4/24.
  19. *
  20. */
  21. @Intercepts({@org.apache.ibatis.plugin.Signature(type=StatementHandler.class, method="prepare", args={Connection.class,Integer.class})})
  22. public class SqlInterceptor
  23. implements Interceptor
  24. {
  25. private static ThreadLocal<RowBounds> threadRowBounds = new ThreadLocal<RowBounds>();
  26. private static RowBounds getRowBounds() {
  27. RowBounds rowBounds = threadRowBounds.get();
  28. threadRowBounds.remove();
  29. return rowBounds;
  30. }
  31. public static void setRowBounds(RowBounds rowBounds) {
  32. threadRowBounds.set(rowBounds);
  33. }
  34. public Object intercept(Invocation invocation) throws Throwable
  35. {
  36. StatementHandler statementHandler = (StatementHandler)invocation.getTarget();
  37. // BoundSql boundSql = statementHandler.getBoundSql();
  38. MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
  39. RowBounds rowBounds = getRowBounds();
  40. if (rowBounds == null) {
  41. rowBounds = (RowBounds)metaStatementHandler.getValue("delegate.rowBounds");
  42. }
  43. Configuration configuration = (Configuration)metaStatementHandler.getValue("delegate.configuration");
  44. Dialect.Type databaseType;
  45. try {
  46. databaseType = Dialect.Type.valueOf(configuration.getVariables().getProperty("dialect").toUpperCase());
  47. }
  48. catch (Exception localException) {
  49. throw new RuntimeException(localException.getMessage());
  50. }
  51. if (databaseType == null) {
  52. throw new RuntimeException("the value of the dialect property in configuration.xml is not defined : " + configuration.getVariables().getProperty("dialect"));
  53. }
  54. Dialect dialect = null;
  55. switch (databaseType.ordinal()) {
  56. case 1:
  57. dialect = new MySql5Dialect();
  58. break;
  59. case 2:
  60. dialect = new OracleDialect();
  61. }
  62. String sql = (String)metaStatementHandler.getValue("delegate.boundSql.sql");
  63. if ((rowBounds != null) && (rowBounds != RowBounds.DEFAULT)) {
  64. sql = dialect.getLimitString(sql, rowBounds.getOffset(), rowBounds.getLimit());
  65. }
  66. if (sql == null) {
  67. throw new RuntimeException("the value of the dialect property in configuration.xml is not defined : " + configuration.getVariables().getProperty("dialect"));
  68. }
  69. metaStatementHandler.setValue("delegate.boundSql.sql", sql);
  70. metaStatementHandler.setValue("delegate.rowBounds.offset", 0);
  71. metaStatementHandler.setValue("delegate.rowBounds.limit", 2147483647);
  72. return invocation.proceed();
  73. }
  74. public Object plugin(Object target)
  75. {
  76. return Plugin.wrap(target, this);
  77. }
  78. public void setProperties(Properties properties)
  79. {
  80. }
  81. }