今天在用
<select id="aaa" parameterClass="java.util.List" resultMap="siteMap">
select * from table where a in
<iterate open="(" close=")" conjunction=",">
#idList[]#
</iterate>
时一直报错,找了很长时间,看到一篇文章才知道原来参数是list 在iterate里面不能加property,去掉property就正常了。
////////////////////////////////////////////////////////////////////////
写道
<iterate property="" conjunction="" open="" close="" prepend="">
</iterate>
<!-- 批量删除对象的时候,iterate不要property属性 -->
<delete id="delStudybook" parameterClass="java.util.List">
delete FROM STUDYBOOK WHERE ID IN
<iterate conjunction="," open="(" close=")">
#bookList[]#
</iterate>
</delete>
注意要property的错误
Caused by: com.ibatis.common.beans.ProbeException: Error getting ordinal list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out of range: -1
//另外:如果parameterClass="java.util.List"类型不匹配的话
报错Caused by: java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.util.ArrayList'.
===============================
<!--批量修改对象,iterate必须包括property属性-->
<update id="updateUsersIterate" parameterClass="java.util.Map">
update users set user_name=#userInfo.user_name# where user_id in
<iterate property="list" conjunction="," open="(" close=")">
#list[]#
</iterate>
</update>
注意不要property属性的错误
Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
===============================
<!-- Iterate的使用,根据多个匹配条件查询,类似in(a,b,c)-->
<select id="selectByIterate" parameterClass="java.util.List" resultClass="user">
SELECT * FROM USERS WHERE USER_ID IN
<iterate conjunction="," open="(" close=")">
#ids[]#
</iterate>
</select>
注意:不要property属性,否则报错。String index out of range: -1
2. 但是,若参数有多个传入的一个是List,另一个不是, parameterClass为map时,需要property属性区分要遍历的 集合。
</iterate>
<!-- 批量删除对象的时候,iterate不要property属性 -->
<delete id="delStudybook" parameterClass="java.util.List">
delete FROM STUDYBOOK WHERE ID IN
<iterate conjunction="," open="(" close=")">
#bookList[]#
</iterate>
</delete>
注意要property的错误
Caused by: com.ibatis.common.beans.ProbeException: Error getting ordinal list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out of range: -1
//另外:如果parameterClass="java.util.List"类型不匹配的话
报错Caused by: java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.util.ArrayList'.
===============================
<!--批量修改对象,iterate必须包括property属性-->
<update id="updateUsersIterate" parameterClass="java.util.Map">
update users set user_name=#userInfo.user_name# where user_id in
<iterate property="list" conjunction="," open="(" close=")">
#list[]#
</iterate>
</update>
注意不要property属性的错误
Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
===============================
<!-- Iterate的使用,根据多个匹配条件查询,类似in(a,b,c)-->
<select id="selectByIterate" parameterClass="java.util.List" resultClass="user">
SELECT * FROM USERS WHERE USER_ID IN
<iterate conjunction="," open="(" close=")">
#ids[]#
</iterate>
</select>
注意:不要property属性,否则报错。String index out of range: -1
2. 但是,若参数有多个传入的一个是List,另一个不是, parameterClass为map时,需要property属性区分要遍历的 集合。
/////ibtais一对多查询用法,和一对多插入方法
写道
<resultMap id="voteMap" class="Vote">
<result property="voteId" column="vote_id" />
<result property="siteId" column="site_id" />
</resultMap>
<resultMap id="picVoteMap" class="PicVote" extends="voteMap" >
<result property="picOptions" column="vote_id" select="getVotePicOptionByVoteId" />
</resultMap>
<select id="getVotePicOptionByVoteId" parameterClass="int" resultMap="votePicOptionMap" >
select *
from OBS.FACE_EXT_VOTE_PIC_OPTION where vote_id = #value#
</select>
<select id="getPicVoteById" parameterClass="int" resultMap="picVoteMap" >
select *
from OBS.FACE_EXT_VOTE where vote_id = #value#
</select>
<result property="voteId" column="vote_id" />
<result property="siteId" column="site_id" />
</resultMap>
<resultMap id="picVoteMap" class="PicVote" extends="voteMap" >
<result property="picOptions" column="vote_id" select="getVotePicOptionByVoteId" />
</resultMap>
<select id="getVotePicOptionByVoteId" parameterClass="int" resultMap="votePicOptionMap" >
select *
from OBS.FACE_EXT_VOTE_PIC_OPTION where vote_id = #value#
</select>
<select id="getPicVoteById" parameterClass="int" resultMap="picVoteMap" >
select *
from OBS.FACE_EXT_VOTE where vote_id = #value#
</select>
用这种查询方式即可只调用getPicVoteById,就能将一对多对象进行查询操作。
一对多插入:
先插入主表,然后获取主表id,然后批量插入副表即可完成一对多的插入。
写道
<selectKey keyProperty="voteId" resultClass="int">
VALUES IDENTITY_VAL_LOCAL()
</selectKey>
VALUES IDENTITY_VAL_LOCAL()
</selectKey>
插入主表末尾插入这么一段就可返回插入主键。
相关推荐
ibatise中关于iterate的用法的例子
NULL 博文链接:https://duqiangcise.iteye.com/blog/286764
ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 ibatis的的增删改查和一对一、一对多查询 完成的项目
ibatis配置多表关联(一对一、一对多、多对多
ibatis 一对多关系映射 ibatis 一对多关系映射ibatis 一对多关系映射
Ibatis多表查询
NULL 博文链接:https://keer2345.iteye.com/blog/673391
NULL 博文链接:https://self4j.iteye.com/blog/906319
Ibatis多表查询,一个小小的多表查询实例教你如何用ibatis进行多表查询
操作数据库 iBATIS查询,java 和spring的配置方法
利用ibatis实现一对一、多对一的示例代码。每种关系用两种方式实现,具体的讲解可看我的博客http://blog.csdn.net/duwenchao1986/article/details/8565386
ibatis实战之一对多关联源代码 文章地址:http://blog.csdn.net/itmyhome1990/article/details/30050183
一份非常详细的讲解文档,从建表到Ibatis配置文件,再到dao代码全部都有
ibatis学习 ibatis总结 ibatis ibatis ibatis
iBatis Web用法实例工程
Ibatis查询Id列表.doc
ibatis入门与ibatis迭代的用法
ibatis动态多条件组合查询 实例 说明
Ibatis一对一映射提示,需要学习的同学请关注,谢谢。