Mybatis dynamically calls the table name and field name, And mybatis dynamically
I have been using the ORM framework of Mybatis, and all of them use some common functions in mybatis. Today, in project development, there is a business that requires various users to query fields in some tables and whether certain fields are displayed. For example, some fields in a table cannot be queried by users. In this case, you need to build an SQL statement to dynamically input the table name and field name. Now I will summarize the solution and hope to help partners who encounter the same problem.
Dynamic SQL is one of the powerful features of mybatis. Before pre-compiling SQL statements, mybatis dynamically parses the SQL statements into a BoundSql object, dynamic SQL is also processed here. Next let's familiarize ourselves with the usage of # {} and $ {} in mybatis:
In the dynamic SQL parsing process, the effects of # {} and $ {} are different:
# {} Is resolved to a parameter identifier of the JDBC precompiled statement (prepared statement.
See the following SQL statement
select * from user where name = #{name};
Will be parsed:
select * from user where name = ?;
We can see that # {} is resolved as a parameter placeholder ?.
$ {} Is only replaced by a broken string. The variable is replaced in the dynamic SQL parsing stage.
For example, the following SQL statement:
select * from user where name = ${name};
When the parameter "sprite" is passed, the SQL statement is parsed:
select * from user where name = "sprite";
The preceding SQL statement does not contain the variable name.
In summary, $ {}'s variable replacement stage is in the dynamic SQL parsing stage, while # {}'s variable replacement is in the DBMS.
The differences between # {} and $ {} are summarized as follows:
- # {} Treats the input parameter as a string and adds a double quotation mark to the input parameter
- $ {} Displays the input parameters directly in the SQL statement without quotation marks.
- # {} Can prevent SQL Injection in Chengdu, and $ {} cannot prevent SQL injection.
$ {} Has been replaced by a variable before pre-compilation, which poses the risk of SQL injection. The following SQL
select * from ${tableName} where name = ${name}
If the input parameter tableName is user; delete user; --, the SQL statement before pre-compilation is changed:
select * from user; delete user; -- where name = ?;
-- The subsequent statement will not work as a comment, and my friends and I are stunned !!! No. The original query statement secretly contains an SQL statement used to delete table data. It is Delete, delete, and delete !!! The important thing is three times. We can imagine how high the risk is.
- $ {} Is generally used to transmit database table names and field names.
- Use # {} Unless possible $ {}
Go to the topic. Through the above analysis, we believe you may have some ideas on how to dynamically call table names and field names. Example:
<select id="getUser" resultType="java.util.Map" parameterType="java.lang.String" statementType="STATEMENT"> select ${columns} from ${tableName} where COMPANY_REMARK = ${company} </select>
To dynamically call the table name and field name, you cannot use pre-compilation. You need to add statementType = "STATEMENT "".
StatementType: Any one of STATEMENT (non-precompiled), PREPARED (pre-compiled), or CALLABLE. This tells MyBatis to use Statement, PreparedStatement, or CallableStatement respectively. Default Value: PREPARED. Here, we obviously cannot use pre-compilation. We need to change it to non-Pre-compilation.
Second, the value of the variable in SQL is $ {xxx}, not # {xxx }.
$ {} Directly displays the input parameters to generate an SQL statement. For example, if $ {xxx} is input as a string, quotation marks must be placed before the input parameters, for example:
String name = "sprite"; name = "'" + name + "'";
Hope to help you! If you have any questions, leave a message below.