xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响
Html代码
- < <
- > >
- & &
- ' '
- " "
在mapper文件中写sql语句时,为避免不必要的麻烦(如<等),建议使用<![CDATA[ ]]>来标记不应由xml解析器进行解析的文本数据,由<![CDATA[ ]]>包裹的所有的内容都会被解析器忽略 <![CDATA[ sql语句 ]]>
Xml代码
- < select id = "getAccountsByBranch" resultType = "Account" parameterType = "string" >
- <![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>
- </ select >
将整个sql语句用<![CDATA[ ]]>标记来避免冲突,在一般情况下都是可行的,但是如果这样写
Xml代码
- < select id = "getAccountErrorCount" resultType = "int" parameterType = "map" >
- <![CDATA[
- select count(*) from t_acctreg_accounterror
- <where>
- <if test="enddate != null and enddate != ''">
- createdate <= #{enddate}
- </if>
- <if test="acctno != null and acctno != ''">
- AND acctno LIKE '%'||#{acctno}||'%'
- </if>
- </where>
- ]]>
- </ 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代码
- < 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 >
还有在向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代码
- < 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 >
JAVA SpringMVC+mybatis(oracle 和 mysql) HTML5 全新高大尚后台框架 bootstrap