JdbcType类型和Java类型的对应关系:
JDBC Type Java Type
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER INTEGER
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
DISTINCT mapping of underlying type
STRUCT Struct
REF Ref
DATALINK java.net.URL[color=red][/color]
xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响
< | < | 小于 |
> | > | 大于 |
& | & | 和 |
' | ' | 单引号 |
" | " | 双引号 |
在mapper文件中写sql语句时,为避免不必要的麻烦(如<等),建议使用<![CDATA[ ]]>来标记不应由xml解析器进行解析的文本数据,由<![CDATA[ ]]>包裹的所有的内容都会被解析器忽略
范例:
<select id="selectByParams" parameterType="map" resultType="map" >
select
role_id as roleId,
role_code as roleCode,
role_name as roleName,
status,
sys_create_user as sysCreateUser,
sys_create_time as sysCreateTime,
sys_update_user as sysUpdateUser,
sys_update_time as sysUpdateTime,
remarks
from
<include refid="Base_Table_Name" />
<where>
<trim prefix="(" suffix=")" suffixOverrides="and|or">
<if test="roleName != null and roleName != '' ">
<![CDATA[ role_name like CONCAT('%', #{roleName,jdbcType=VARCHAR}, '%') and ]]>
</if>
<if test="roleCode != null and roleCode != '' ">
<![CDATA[ role_code like CONCAT('%', #{roleCode,jdbcType=VARCHAR}, '%') and ]]>
</if>
<if test="status != null and status != '' ">
<![CDATA[ status = #{status,jdbcType=VARCHAR} and ]]>
</if>
</trim>
</where>
</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可解决(注意大小写)
范例:
<insert id="insertAccountError" statementType="PREPARED"
parameterType="AccountError">
INSERT INTO t_acctreg_accounterror(createdate,acctno, errorinfo)
VALUES(#{createdate,jdbcType=DATE},#{acctno,jdbcType=VARCHAR},#{errorinfo,jdbcType=VARCHAR})
</insert>