iBatis prepend iterate 标签,in字句的一点总结
来源:互联网 发布:mac git工具 编辑:程序博客网 时间:2024/06/05 09:15
先看一段代码:
<select id="select" resultMap="HotelinfoResult" parameterClass="java.util.Map"> select id, HotelId as hotelId, Selectflag as selectflag from Hotelinfokuxun <dynamic prepend=" WHERE "> <isPropertyAvailable property="idList"> <isNotNull property="idList" prepend=" and id in "> <iterate property="idList" conjunction="," close=")" open="("> #idList[]# </iterate> </isNotNull> </isPropertyAvailable> </dynamic> </select>预计预计产生的SQL是:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE id in (?,?,?,?)需要注意的是:iBatis对于第一个prepend=" and " 是要移除的,如果不移除,则SQL 语句就是:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE and id in (?,?,?,?) //显然这样的SQL是错误的,iBatis移除了字句中第一个and显然and是不能存在的,iBatis对于动态SQL移除第一个and,但是本例中in字句还包含” and id in",也同时被移除了,所以程序产生报错:
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred while applying a parameter map. --- Check the Hotelinfokuxun.select-InlineParameterMap. --- Check the statement (query failed). --- Cause: java.sql.SQLException: Operand should contain 1 column(s)
当前面的<isPropertyAvilable>都没有匹配上, prepend="and id in "子句作为第一个prepend时,就会被移除,程序就报错
实际产生的SQL语句是:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE (?,?,?,?) //少了应该存在的" id in "
解决方法:
一:增加一个无效的1='1'
<isPropertyAvailable property="idList"><isNotNull prepend=" and ">1='1'</isNotNull><isNotNull property="idList" prepend=" and id in "><iterate property="idList" conjunction="," close=")" open="(">#idList[]#</iterate></isNotNull></isPropertyAvailable>
产生的SQL语句:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE 1='1'and id in (?,?,?,?)
二:放弃使用In子句
<isPropertyAvailable property="idList"><isNotNull property="idList" prepend=" and "><iterate property="idList" conjunction="OR" close=")" open="(">id=#idList[]#</iterate></isNotNull> </isPropertyAvailable>产生的SQL语句是:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE ( id=? OR id=? OR id=? OR id=?)
- iBatis prepend iterate 标签,in字句的一点总结
- ibatis中动态语句的iterate标签
- iBatis iterate标签
- iBatis iterate标签
- ibatis iterate标签
- ibatis <iterate>标签
- ibatis标签之iterate
- ibatis <iterate>标签
- iBatis iterate标签
- ibatis实现Iterate的使用 ibatis里面in的实现
- ibatis Iterate in 使用数组
- ibatis的iterate使用
- ibatis的iterate使用
- Ibatis Iterate的使用
- Struts logic:iterate标签的总结
- ibatis批量插入-iterate标签应用
- ibatis中iterate的用法
- ibatis中iterate的使用
- linux学习笔记---putty乱码,查询软件包信息
- Resharper进阶一:简单介绍
- log4j配置 使用总结
- 常用网站
- LINUX驱动分析之RTC(四)
- iBatis prepend iterate 标签,in字句的一点总结
- 60款很酷的 jQuery 幻灯片演示和下载
- 构造函数初始化列表相关知识点集合
- hdu 2601
- gtest的单独编译和使用
- 一个用于生成图表的gem: lazy_high_charts
- EAS BOS进度条(针对耗时长的操作)
- 如何调整launcher2屏幕的布局
- 酿酒的方法与心得