<iterate
/* ??????,??????????????????????????????????????????????????????,?????????????????????List??????,???????????????OutofRangeException, ?????????????????????java.util.Map????????????,
????????????????????????????????????java.util.List, ????????????????????????.
?????????????????????: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html#id386679
???????????????????????????, ??????????????????????????????????????????????????????, ????????????????????????, ????????????.
*/
property=""
/* ??????,iterate???????????????????????????,?????????????????????????????????????????????????????????,????????????????????????OR???, ????????????????????????OR,???????????????union all???, ????????????????????????union all*/
conjunction=""
/* ??????, ?????????????????????*/
open=""
/* ??????, ?????????????????????*/
close=""
/* ??????, ??????open??????????????????????????????*/
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???????????????????????? ?????????
=================================================
<!--??????????????????-->
<insert id="BarkInsertUsers" parameterClass="java.util.List">
insert
<iterate conjunction="">
into users(user_id,user_name,password,address,telephone,email,create_date)
values(#list[].user_id#,#list[].user_name#,#list[].password#,
#list[].address#,#list[].telephone#,#list[].email#,#list[].create_date#)
</iterate>
<!--??????????????? -->
select * from dual
</insert>
//??????????????????
<insert id="BarkInsertUsers" parameterClass="java.util.List">
insert into users(user_id,user_name,password,address,telephone,email,create_date)
values(
<iterate conjunction="union all">select #list[].user_id#,#list[].user_name#,#list[].password#,
#list[].address#,#list[].telephone#,#list[].email#,#list[].create_date#)
from dual
</iterate>
</insert>
//???????????????????????????????????????????????????????????????hibernate_sequence.NEXTVAL?????????
//???????????????????????? (SCOTT.SYS_C0010057)????????????????????????
==================================================
?????????????????????SQL?????????
//?????????????????????
public int deleteStudyBook(List<Integer> bookList){
try {
return this.client.delete("delStudybook",bookList);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//?????????????????????
public int updateUsers(Map<String,Object> map){
try {
return this.client.delete("updateUsersIterate",map);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//??????Iterate????????????????????????
public void barkInsert(List<Users> list){
try {
this.client.insert("BarkInsertUsers",list);
} catch (SQLException e) {
e.printStackTrace();
}
}
=========================
//??????iterate????????????in???????????????
public void queryByIterate(List<Integer> ids){
try {
List<Users> list=this.client.queryForList("selectByIterate",ids);
for (Users user : list) {
System.out.println("user====I"+user.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
=========================
Test???????????????????????????
//???????????????
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
//????????????
dao.deleteStudyBook(list);
----------------------------------------------------
//????????????????????????
Map<String,Object> map=new HashMap<String,Object>();
//?????????????????????
Users user=new Users();
user.setUser_name("aaaaaaaaaaaaaaaaa");
List<Integer> list1=new ArrayList<Integer>();
list1.add(1);
list1.add(2);
map.put("userInfo",user);
map.put("list",list1);
dao.updateUsers(map);
----------------------------------------------------------
//?????????????????????
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
dao.queryByIterate(list);
---------------------------------------------------------
//????????????????????????
List<Users> userList=new ArrayList<Users>();
//????????????????????????????????????(id????????????#list[].user_id#)
Users user1=new Users(10,"bbbb","bbbb","bbbb","bbbb","bbbb","bbbb");
Users user2=new Users(11,"cccc","cccc","cccc","cccc","cccc","cccc");
dao.barkInsert(userList);