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
大致是相对应地改为全部大写。