自定义Mapper文件的几种写法

来源:互联网 发布:mac word 删除空白页 编辑:程序博客网 时间:2024/06/08 15:25

1.为xml中某些特殊符号作转义

  1.1不含有动态语句(where,if)

<select id="getAccountsByBranch" resultType="Account" parameterType="string">      <![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>  </select> 


  1.2含有动态语句(where,if)

  

<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>  

2.不指定jdbc类型的参数,MyBatis默认为Other类型。当不指定jdbc类型时,若参数为null,将报错Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters。

  下面是MyBatis中常用的javaType和jdbcType的对应关系

  

javaType   jdbcTypeString    CHARString    VARCHARBigDecimal   DECIMALBigDecimal  NUMERICboolean     BOOLEAN byte     TINYINT short     SMALLINTint      INTEGERlong     BIGINTfloat     FLOAT double    DOUBLEDate     DATETime     TIMETimestamp  TIMESTAMPClob     CLOBBlob     BLOB
  大致是相对应地改为全部大写。


0 0
原创粉丝点击