mybatis转义问题
2015-07-23 15:38
323 查看
xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响
Html代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
< <
> >
& &
' '
" "
在mapper文件中写sql语句时,为避免不必要的麻烦(如<等),建议使用<![CDATA[ ]]>来标记不应由xml解析器进行解析的文本数据,由<![CDATA[ ]]>包裹的所有的内容都会被解析器忽略 <![CDATA[ sql语句 ]]>
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<select id="getAccountsByBranch" resultType="Account" parameterType="string">
<![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>
</select>
将整个sql语句用<![CDATA[ ]]>标记来避免冲突,在一般情况下都是可行的,但是如果这样写
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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>
Html代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
< <
> >
& &
' '
" "
在mapper文件中写sql语句时,为避免不必要的麻烦(如<等),建议使用<![CDATA[ ]]>来标记不应由xml解析器进行解析的文本数据,由<![CDATA[ ]]>包裹的所有的内容都会被解析器忽略 <![CDATA[ sql语句 ]]>
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<select id="getAccountsByBranch" resultType="Account" parameterType="string">
<![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>
</select>
将整个sql语句用<![CDATA[ ]]>标记来避免冲突,在一般情况下都是可行的,但是如果这样写
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/9b41f12ae6579ca0016cb180bf520fc7.png)
<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>
相关文章推荐
- 从Linux Deepin下小米2s刷机的“生”与“死”看Android手机的分区
- android 围绕中心旋转动画
- 华为6502做端口镜像
- 了解HTML5和“她”的 API (一)
- UI—代理简单使用
- 软件测试之性能测试:大促带来的灾难
- 文章标题
- redis安装与配置
- 保存文件到内存
- 麦子学院android开发教程:android手势翻页效果
- 揭秘百度核心技术:53位专家纯干货分享
- 端口镜像配置
- 基于CentOS的MySQL学习补充四--使用Shell批量从CSV文件中插入数据到数据表
- 嵌入式Linux开发环境的搭建
- rrayList,Vector, LinkedList的存储性能和特性
- Mysql(Windows)--- Access denied for user 'root'@'localhost'(using password:YES)
- Ubuntu安装SVN
- linux 出行日记(2)
- Java学习笔记--------Java的事件和事件处理接口
- MySQL Study之--MySQL用户及权限管理