mybatis寫mapper檔案注意事項

來源:互聯網
上載者:User

標籤:att   prepare   ges   star   lex   操作   attribute   char   收藏   

xml中某些特殊符號作為內容資訊時需要做轉義,否則會對檔案的合法性和使用造成影響

Html代碼  
  1. &lt; <   
  2. &gt; >   
  3. &amp; &   
  4. &apos; ‘   
  5. &quot; "  

 

 

在mapper檔案中寫sql語句時,為避免不必要的麻煩(如<等),建議使用<![CDATA[ ]]>來標記不應由xml解析器進行解析的文本資料,由<![CDATA[  ]]>包裹的所有的內容都會被解析器忽略 <![CDATA[ sql語句 ]]> 

 

Xml代碼  
  1. <select id="getAccountsByBranch" resultType="Account" parameterType="string">  
  2.     <![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>  
  3. </select>  

 將整個sql語句用<![CDATA[   ]]>標記來避免衝突,在一般情況下都是可行的,但是如果這樣寫

 

Xml代碼  
  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>  

 就會收到錯誤資訊:

    org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: 無效的列類型: 1111 ; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 無效的列類型: 1111; nested exception is java.sql.SQLException: 無效的列類型: 1111

    這是由於該sql配置中有動態語句(where,if),where,if 條件不能放在<![CDATA[ ]]>中,否則將導致無法識別動態判斷部分,導致整個sql語句非法.應該縮小範圍,只對有字元衝突部分進行合法性調整

 

Xml代碼  
  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>  

 

還有在向oracle插入資料時,mybatis3報Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters,是由於參數出現了null值,對於Mybatis,如果進行操作的時候,沒有指定jdbcType類型的參數,mybatis預設jdbcType.OTHER導致,給參數加上jdbcType可解決(注意大小寫)

http://code.google.com/p/mybatis/issues/detail?id=224&q=Error%20setting%20null%20parameter&colspec=ID

 

Xml代碼  
  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>  

mybatis寫mapper檔案注意事項

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.