mybatis深刻理解(一)之 # 与 $ 区别以及 sql 预编译

mybatis 中使用 sqlMap 进行 sql 查询时,常常须要动态传递参数,例如咱们须要根据用户的姓名来筛选用户时,sql 以下:java

select * from user where name = "ruhua";

上述 sql 中,咱们但愿 name 后的参数 "ruhua" 是动态可变的,即不一样的时刻根据不一样的姓名来查询用户。在 sqlMap 的 xml 文件中使用以下的 sql 能够实现动态传递参数 name:mysql

select * from user where name = #{name};

或者sql

select * from user where name = '${name}';

对于上述这种查询状况来讲,使用 #{ } 和 ${ } 的结果是相同的,可是在某些状况下,咱们只能使用两者其一。数据库

'#' 与 '$'

区别

动态 SQL 是 mybatis 的强大特性之一,也是它优于其余 ORM 框架的一个重要缘由。mybatis 在对 sql 语句进行预编译以前,会对 sql 进行动态解析,解析为一个 BoundSql 对象,也是在此处对动态 SQL 进行处理的。缓存

在动态 SQL 解析阶段, #{ } 和 ${ } 会有不一样的表现:服务器

#{ } 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符。mybatis

例如,sqlMap 中以下的 sql 语句并发

select * from user where name = #{name};

解析为:框架

select * from user where name = ?;

一个 #{ } 被解析为一个参数占位符 ?ide

而,

${ } 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换

例如,sqlMap 中以下的 sql

select * from user where name = '${name}';

当咱们传递的参数为 "ruhua" 时,上述 sql 的解析为:

select * from user where name = "ruhua";

预编译以前的 SQL 语句已经不包含变量 name 了。

综上所得, ${ } 的变量的替换阶段是在动态 SQL 解析阶段,而 #{ }的变量的替换是在 DBMS 中。

用法 tips

一、能使用 #{ } 的地方就用 #{ }

首先这是为了性能考虑的,相同的预编译 sql 能够重复利用。

其次,${ } 在预编译以前已经被变量替换了,这会存在 sql 注入问题。例如,以下的 sql,

select * from ${tableName} where name = #{name}

假如,咱们的参数 tableName 为 user; delete user; --,那么 SQL 动态解析阶段以后,预编译以前的 sql 将变为

select * from user; delete user; -- where name = ?;

-- 以后的语句将做为注释,不起做用,所以原本的一条查询语句偷偷的包含了一个删除表数据的 SQL!

二、表名做为变量时,必须使用 ${ }

这是由于,表名是字符串,使用 sql 占位符替换字符串时会带上单引号 '',这会致使 sql 语法错误,例如:

select * from #{tableName} where name = #{name};

预编译以后的sql 变为:

select * from ? where name = ?;

假设咱们传入的参数为 tableName = "user" , name = "ruhua",那么在占位符进行变量替换后,sql 语句变为

select * from 'user' where name='ruhua';

上述 sql 语句是存在语法错误的,表名不能加单引号 ''(注意,反引号 ``是能够的)。

sql预编译

定义

sql 预编译指的是数据库驱动在发送 sql 语句和参数给 DBMS 以前对 sql 语句进行编译,这样 DBMS 执行 sql 时,就不须要从新编译。

为何须要预编译

JDBC 中使用对象 PreparedStatement 来抽象预编译语句,使用预编译

  1. 预编译阶段能够优化 sql 的执行
    预编译以后的 sql 多数状况下能够直接执行,DBMS 不须要再次编译,越复杂的sql,编译的复杂度将越大,预编译阶段能够合并屡次操做为一个操做。

  2. 预编译语句对象能够重复利用
    把一个 sql 预编译后产生的 PreparedStatement 对象缓存下来,下次对于同一个sql,能够直接使用这个缓存的 PreparedState 对象。

mybatis 默认状况下,将对全部的 sql 进行预编译。

mysql预编译源码解析

mysql 的预编译源码在 com.mysql.jdbc.ConnectionImpl 类中,以下:

public synchronized java.sql.PreparedStatement prepareStatement(String sql,
            int resultSetType, int resultSetConcurrency) throws SQLException {
        checkClosed();

        //
        // FIXME: Create warnings if can't create results of the given
        // type or concurrency
        //
        PreparedStatement pStmt = null;
        
        boolean canServerPrepare = true;
        
        // 不一样的数据库系统对sql进行语法转换
        String nativeSql = getProcessEscapeCodesForPrepStmts() ? nativeSQL(sql): sql;
        
        // 判断是否能够进行服务器端预编译
        if (this.useServerPreparedStmts && getEmulateUnsupportedPstmts()) {
            canServerPrepare = canHandleAsServerPreparedStatement(nativeSql);
        }
        
        // 若是能够进行服务器端预编译
        if (this.useServerPreparedStmts && canServerPrepare) {

            // 是否缓存了PreparedStatement对象
            if (this.getCachePreparedStatements()) {
                synchronized (this.serverSideStatementCache) {
                    
                    // 从缓存中获取缓存的PreparedStatement对象
                    pStmt = (com.mysql.jdbc.ServerPreparedStatement)this.serverSideStatementCache.remove(sql);
                    
                    if (pStmt != null) {
                        // 缓存中存在对象时对原 sqlStatement 进行参数清空等
                        ((com.mysql.jdbc.ServerPreparedStatement)pStmt).setClosed(false);
                        pStmt.clearParameters();
                    }

                    if (pStmt == null) {
                        try {
                            // 若是缓存中不存在,则调用服务器端(数据库)进行预编译
                            pStmt = ServerPreparedStatement.getInstance(getLoadBalanceSafeProxy(), nativeSql,
                                    this.database, resultSetType, resultSetConcurrency);
                            if (sql.length() < getPreparedStatementCacheSqlLimit()) {
                                ((com.mysql.jdbc.ServerPreparedStatement)pStmt).isCached = true;
                            }
                            
                            // 设置返回类型以及并发类型
                            pStmt.setResultSetType(resultSetType);
                            pStmt.setResultSetConcurrency(resultSetConcurrency);
                        } catch (SQLException sqlEx) {
                            // Punt, if necessary
                            if (getEmulateUnsupportedPstmts()) {
                                pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
                                
                                if (sql.length() < getPreparedStatementCacheSqlLimit()) {
                                    this.serverSideStatementCheckCache.put(sql, Boolean.FALSE);
                                }
                            } else {
                                throw sqlEx;
                            }
                        }
                    }
                }
            } else {

                // 未启用缓存时,直接调用服务器端进行预编译
                try {
                    pStmt = ServerPreparedStatement.getInstance(getLoadBalanceSafeProxy(), nativeSql,
                            this.database, resultSetType, resultSetConcurrency);
                    
                    pStmt.setResultSetType(resultSetType);
                    pStmt.setResultSetConcurrency(resultSetConcurrency);
                } catch (SQLException sqlEx) {
                    // Punt, if necessary
                    if (getEmulateUnsupportedPstmts()) {
                        pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
                    } else {
                        throw sqlEx;
                    }
                }
            }
        } else {
            // 不支持服务器端预编译时调用客户端预编译(不须要数据库 connection )
            pStmt = (PreparedStatement) clientPrepareStatement(nativeSql, resultSetType, resultSetConcurrency, false);
        }
        
        return pStmt;
    }

流程图以下所示:

图片描述

mybatis之sql动态解析以及预编译源码

mybatis sql 动态解析

mybatis 在调用 connection 进行 sql 预编译以前,会对sql语句进行动态解析,动态解析主要包含以下的功能:

  • 占位符的处理

  • 动态sql的处理

  • 参数类型校验

mybatis强大的动态SQL功能的具体实现就在此。动态解析涉及的东西太多,之后再讨论。

总结

本文主要深刻探究了 mybatis 对 #{ } 和 ${ }的不一样处理方式,并了解了 sql 预编译。

相关文章
相关标签/搜索