Mybatis dynamic incoming tableName-- non-precompiled (STATEMENT)

In use Mybatis process, you can feel its powerful and flexible place, heartfelt is the father of 999 points Mybatis praise! In the course often encounter such a situation, when I query the data table name is dynamic incoming from the program, such as we write all look like when sql query through the xml file mybatis:
1, normal query

1
2
3
<select id="activityEnrollModelTableName" parameterType="java.util.HashMap" resultType="java.util.HashMap">
SELECT * FROM user WHERE userid = #
</select>

When the above query execution MyBatis, in fact, is automatically executed in accordance with the embodiment JDBC prepared statement is equivalent to the execution of code in the following paragraph JDBC

1
2
3
4
5
6
7
8
9
Class.forName( "com.mysql.jdbc.Driver");
Connection conn = DriverManage.getConnection( "jdbc:mysql://localhost:3306/dbname","root","112233");
PreparedStatement preState = conn.prepareStatement( "SELECT * FROM user WHERE userid = ?");
preState.setString ( 1 '96');
ResultSet result = preState.executeQuery();
while(result.next()){
result.getString(columnname);
..........
}

Here we can not help wondering, mybatis default are executed in accordance with precompiled sql statements do way? In fact, that's it. By looking at the official website mybatis document can see that there is such a parameter, statementType = [STATEMENT | PREPARED | CALLABLE]; there are three optional values, mybatis default is PREPARED;
this parameter is what role do:

  1. Sql execution mode setting of mybatis
  2. STATEMENT set to a non precompiled statement mode
  3. PREPARED set precompiled statement default mode -mybatis
  4. CALLABLE compatible mode is set, or the adaptive mode, such as after this value is set automatically according to the process #, $ mybatis to judgment processing during processing sql, said later about the difference between the $ and #.

In summary, mybatis default execute sql statements in accordance with pre-compiled statement way

2, dynamic table name passed

In fact, incoming tableName often encounter situations dynamic, meaning that the above sql statement "user" is a dynamic incoming, incoming dynamic table name is mybatis in a special case,

1
2
3
<select id="activityEnrollModelTableName" parameterType="java.util.HashMap" resultType="java.util.HashMap">
* The FROM the SELECT # # the WHERE userid = // wrong wording
</select>

For the above statement, if let mybatis still performed in a prepared statement the way, equivalent to JDBC code like the following:

1
2
3
4
5
6
7
8
9
10
Class.forName( "com.mysql.jdbc.Driver");
Connection conn = DriverManage.getConnection( "jdbc:mysql://localhost:3306/dbname","root","112233");
PreparedStatement preState = conn.prepareStatement( "SELECT * FROM ? WHERE userid = ?");
preState.setString( 1,"USER");
preState.setString ( 2 "96");
ResultSet result = preState.executeQuery();
while(result.next()){
result.getString(columnname);
..........
}

We put this section JDBC code by the java code execution, will be reported anomalies found:

1
the Java .sql .SQLException: ORA- 00903: table name is invalid

It is possible to explain the pre-compiled statement can not be used in the column name (column name queries nor can a prepared statement), table name; only the role and condition parameters where property! Since JDBC prepared statement will not be able to function and way above the table name, then mybatis it also does not work (because mybatis default precompiled statement mode). But mybatis have long been taken into account this situation, so for us to do a deal:

  1. Add select tag statement statementType = "STATEMENT" attribute configuration
  2. Sql statement within the tag will all be $ # {} {} replacement, i.e. further to $ #;
    1
    2
    3
    <select id= "activityEnrollModelTableName" statementType="STATEMENT" parameterType="java.util.HashMap" resultType="java.util.HashMap">
    * The FROM $ the WHERE userid the SELECT = $ / / correct wording
    </select>

The above statement by adding statementType = label after "STATEMENT" configuration, mybatis no longer execute sql statement using a prepared statement the way, that is, using direct sql statements operations; since then added statementType = "STATEMENT" non-pre-compiled after configuration, why also need to replace the $ # it? In fact, it is this:

    1. "#" Is prepared statement following default mode sign, that is encountered when mybatis # {}, # will replace the placeholder;? Is interpreted as a JDBC prepared statement, then the value itself # set in.
    2. “$” 是非预编译语句下面的匹配符,非预编译语句说白了就是你传入什么sql语句,就执行什么sql语句,mybatis不做任何处理操作,但是这里mybatis会将${}对应的值,当做一个字符串处理,也就是说你程序接口方法中传递过来参数值是什么,对应的sql填充就是什么!

Guess you like

Origin www.cnblogs.com/mxj961116/p/11907187.html