MyBatis uses dynamic table or column code parsing

  • 2021-01-25 07:33:33
  • OfStack

Sometimes it is unavoidable to use dynamic tables or columns for business processing. Here's how to use dynamic tables/columns:

[1] Use precompilation

That is, the default value.


<select id="hisNumber" parameterType="hashmap" resultType="hashmap" >
   select number from ${oldTableName} 
<!-- Used here "$"!!!-->
where name=#{name} and date = #{date}
<!-- Used here "#"-->
<select>

select, number, from ? where name=? and date=? Use "?" like this. Statements as placeholders, and then parameter parsing.

[2] Use non-precompilation


<select id="hisNumber" parameterType="hashmap" resultType="hashmap" statementType="STATEMENT" >
   select number from ${oldTableName} 
<!-- Used here "$"!!!-->
where name='${name,jdbcType=VARCHAR}' and date = '${date,jdbcType=TIMESTAMP}'
<select>

Note the following values for name and date, using the '${name}' format, which will perform data type conversion on the parameters and help improve the performance of mysql queries.

[3] Unprecompiled is still used


<select id="hisNumber" parameterType="hashmap" resultType="hashmap" statementType="STATEMENT" >
   select number from ${oldTableName} 
<!-- Used here "$"!!!-->
where name=${name} and date = ${date}
<select>

Note that the values of name and date are in the format of ${name}, and will be taken directly without data type conversion. When the parameter is of numeric type and the format is "00124", the data will be misread (0124, 124, and so on will be read).

Therefore, it is recommended to use the first way!!

conclusion

This is the end of the article on MyBatis using dynamic table or column code parsing, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: