Notes on MyBatis writing mapper files

Source: Internet
Author: User
Tags cdata xml parser

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
    1. &lt; <    
    2. &gt; >    
    3. &amp; &
    4. &apos; ‘
    5. &quot; "

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
    1. < select   id = "Getaccountsbybranch "   resulttype = "account"   parametertype = "string" >   
    2.     <![ Cdata[select * from t_acctreg_accounts where acctno < #{acctno}]]>   
    3. </ Select >   

use <! for the entire SQL statement [cdata[]]> tags to avoid collisions is generally possible, but if you write

XML code
  1. < Select id="Getaccounterrorcount" resulttype="int" " parametertype=" map ">
  2. <! [Cdata[
  3. Select COUNT (*) from T_acctreg_accounterror
  4. <where>
  5. <if test= "EndDate! = null and EndDate! =" ">
  6. createdate <= #{enddate}
  7. </if>
  8. <if test= "Acctno! = null and Acctno! =" ">
  9. and acctno like '% ' | | #{acctno}| | ' % '
  10. </if>
  11. </where>
  12. ]]>
  13. </ 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
  1. < Select id="Getaccounterrorcount" resulttype="int" parametertype = "Map" >
  2. Select COUNT (*) from T_acctreg_accounterror
  3. < where >   
  4. < if Test="EndDate! = null and EndDate! =" ">
  5. <! [Cdata[createdate <= #{enddate}]]>
  6. </ if >   
  7. < if Test="Acctno! = null and Acctno! =" ">
  8. <! [Cdata[and acctno like '% ' | | #{acctno}| | ' % ']]>
  9. </ if >   
  10. </ where >   
  11. </ 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
  1. < Insert id="insertaccounterror" statementtype=" PREPARED "
  2. ParameterType = "Accounterror" >   
  3. INSERT into T_acctreg_accounterror (Createdate,acctno, ErrorInfo)
  4. VALUES (#{createdate,jdbctype=DATE},#{acctno,jdbctype= VARCHAR },#{errorinfo,jdbctype=VARCHAR})
  5. </ Insert >   

Notes on MyBatis writing mapper files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.