深入理解@InsertProvider执行原理

1.首先要拼接处insert语句,其中包含入参,与数据库表字段的映射字段。

在执行Provider类里面的动态插入sql的时候,程序会调用 AbstractSQL这个抽象类,执行里面的两个拼接字符串的方法

    public T INSERT_INTO(String tableName) {
        this.sql().statementType = AbstractSQL.SQLStatement.StatementType.INSERT;
        this.sql().tables.add(tableName);
        return this.getSelf();
    }

    public T VALUES(String columns, String values) {
        this.sql().columns.add(columns);
        this.sql().values.add(values);
        return this.getSelf();
    }

这个抽象class里面有一个私有的静态类SQLStatement和私有静态SalfAppendable类。SQLStatement类里面有一个共有静态枚举类,里面有它的一个私有构造函数,枚举被设计成是单例模式,即枚举类型会由JVM在加载的时候,实例化枚举对象,你在枚举类中定义了多少个就会实例化多少个,JVM为了保证每一个枚举类元素的唯一实例,是不会允许外部进行new的,所以会把构造函数设计成private,防止用户生成实例,破坏唯一性。枚举类里面含有增删查改四种。私有的静态类SQLStatement有sql语句的各种关键字list,distinct是boolean类型,例如:

   List<String> sets = new ArrayList();
List<String> columns = new ArrayList();

子类ArrayList实例化List,因为List是一个接口,所以只能依靠其“子类”(在这里是List的实现类)来进行实例化,这里的对象是List的对象。

  private void sqlClause(AbstractSQL.SafeAppendable builder, String keyword, List<String> parts, String open, String close, String conjunction) {
            if(!parts.isEmpty()) {
                if(!builder.isEmpty()) {
                    builder.append("\n");
                }

                builder.append(keyword);
                builder.append(" ");
                builder.append(open);
                String last = "________";
                int i = 0;

                for(int n = parts.size(); i < n; ++i) {
                    String part = (String)parts.get(i);
                    if(i > 0 && !part.equals(") \nAND (") && !part.equals(") \nOR (") && !last.equals(") \nAND (") && !last.equals(") \nOR (")) {
                        builder.append(conjunction);
                    }

                    builder.append(part);
                    last = part;
                }

                builder.append(close);
            }

        }

上面这个函数就是增删查改通用的sql解析函数。下面是增调用的函数

  private String insertSQL(AbstractSQL.SafeAppendable builder) {
            this.sqlClause(builder, "INSERT INTO", this.tables, "", "", "");
            this.sqlClause(builder, "", this.columns, "(", ")", ", ");
            this.sqlClause(builder, "VALUES", this.values, "(", ")", ", ");
            return builder.toString();
        }

通过下面的函数确定增删查改对应调用的函数

 public String sql(Appendable a) {
            AbstractSQL.SafeAppendable builder = new AbstractSQL.SafeAppendable(a);
            if(this.statementType == null) {
                return null;
            } else {
                String answer;
                switch(null.$SwitchMap$org$apache$ibatis$jdbc$AbstractSQL$SQLStatement$StatementType[this.statementType.ordinal()]) {
                case 1:
                    answer = this.deleteSQL(builder);
                    break;
                case 2:
                    answer = this.insertSQL(builder);
                    break;
                case 3:
                    answer = this.selectSQL(builder);
                    break;
                case 4:
                    answer = this.updateSQL(builder);
                    break;
                default:
                    answer = null;
                }

                return answer;
            }
        }
私有静态SalfAppendable类是进行sql字符串的拼接。里面有是一个私有不可以改变的Appendable对象。


 private static class SafeAppendable {
        private final Appendable a;
        private boolean empty = true;

        public SafeAppendable(Appendable a) {
            this.a = a;
        }

        public AbstractSQL.SafeAppendable append(CharSequence s) {
            try {
                if(this.empty && s.length() > 0) {
                    this.empty = false;
                }

                this.a.append(s);
                return this;
            } catch (IOException var3) {
                throw new RuntimeException(var3);
            }
        }

        public boolean isEmpty() {
            return this.empty;
        }
    }



然后调用ProviderSqlSource类,接着调用不可变类MappedStatement类,再调用BaseStatementHandler类,--->PreparedStatementHandler等。

2.ProviderSqlSource实现了sqlSource接口,代表从注解中读取相关的映射语句的内容,它创建的sql会被传到数据库。

根据SQL语句的类型不同,mybatis提供了多种SqlSource的具体实现:

1)StaticSqlSource:最终静态SQL语句的封装,其他类型的SqlSource最终都委托给StaticSqlSource。
2)RawSqlSource:原始静态SQL语句的封装,在加载时就已经确定了SQL语句,没有、等动态标签和${} SQL拼接,比动态SQL语句要快,因为不需要运行时解析SQL节点。
3)DynamicSqlSource:动态SQL语句的封装,在运行时需要根据参数处理、等标签或者${} SQL拼接之后才能生成最后要执行的静态SQL语句。
4)ProviderSqlSource:当SQL语句通过指定的类和方法获取时(使用@XXXProvider注解),需要使用本类,它会通过反射调用相应的方法得到SQL语句





猜你喜欢

转载自blog.csdn.net/zhangludcsdn/article/details/80064114