Some special symbols in XML need to be escaped as content information, otherwise it will affect the legality and use of the file.
HTML code
- < <
- > >
- & &
- ' ‘
- " "
When writing SQL statements in the mapper file, it is recommended to use <! to avoid unnecessary hassles (such as <, etc.) [cdata[]]> to mark text data that should not be parsed by the XML parser, by <! [cdata[]]> all contents of the package will be ignored by the parser <! [cdata[SQL statement]]>
XML code
- <select id="Getaccountsbybranch" resulttype="account" parametertype="string" >
- <! [Cdata[select * from t_acctreg_accounts where Acctno < #{acctno}]]>
- </Select>
Use <! for the entire SQL statement [cdata[]]> tags to avoid collisions is generally possible, but if you write
XML code
- <select id= "getaccounterrorcount" resulttype="int" parametertype="map">
- <! [Cdata[
- Select COUNT (*) from T_acctreg_accounterror
- <where>
- <if test= "EndDate! = null and EndDate! =" ">
- CreateDate <= #{enddate}
- </if>
- <if test= "Acctno! = null and Acctno! =" ">
- And acctno like '% ' | | #{acctno}| | ' %‘
- </if>
- </where>
- ]]>
- </Select>
You will receive an error message:
Org.springframework.jdbc.UncategorizedSQLException:Error setting null parameter. Most JDBC drivers require, the Jdbctype must is specified for all nullable parameters. Cause:java.sql.SQLException: Invalid column type: 1111; Uncategorized SQLException for SQL []; SQL state [99999]; Error code [17004]; Invalid column type: 1111; Nested exception is java.sql.SQLException: Invalid column type: 1111
This is because there is a dynamic statement (WHERE,IF) in the SQL configuration and the where,if condition cannot be placed in the <! [Cdata[]]>, otherwise, will cause the dynamic Judgment section to be unrecognized, causing the entire SQL statement to be illegal. You should narrow it down and only make legitimate adjustments to a character-conflicting part.
XML code
- <select id= "getaccounterrorcount" resulttype="int" parametertype="map">
- Select COUNT (*) from T_acctreg_accounterror
- <where>
- <if test="EndDate! = null and EndDate! =" ">
- <! [Cdata[createdate <= #{enddate}]]>
- </If>
- <if test="Acctno! = null and Acctno! =" ">
- <! [Cdata[and acctno like '% ' | | #{acctno}| | ' % ']]>
- </If>
- </where>
- </Select>
There is also the MYBATIS3 error setting null parameter when inserting data to Oracle. Most of the JDBC drivers require that the Jdbctype must is specified for all nullable parameters is due to the null value of the parameter, for MyBatis, if the operation The Jdbctype type parameter is not specified, mybatis default jdbctype.other cause, parameter plus jdbctype can be resolved (note case)
Http://code.google.com/p/mybatis/issues/detail?id=224&q=Error%20setting%20null%20parameter&colspec=ID
XML code
- <insert id="Insertaccounterror" statementtype="PREPARED "
- parametertype="Accounterror">
- INSERT into T_acctreg_accounterror (Createdate,acctno, ErrorInfo)
- VALUES (#{createdate,jdbctype=date},#{acctno,jdbctype=varchar},#{errorinfo,jdbctype= VARCHAR})
- </Insert>
MyBatis Write Mapper file considerations