您的位置:首页 > 移动开发

自定义Mapper文件的几种写法

2016-12-15 10:16 429 查看
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   jdbcType
String    CHAR
String    VARCHAR
BigDecimal  DECIMAL
BigDecimal  NUMERIC
boolean    BOOLEAN
byte     TINYINT
short     SMALLINT
int      INTEGER
long     BIGINT
float     FLOAT
double    DOUBLE
Date     DATE
Time     TIME
Timestamp  TIMESTAMP
Clob     CLOB
Blob     BLOB  大致是相对应地改为全部大写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: