mybatis iterate

<iterate  

 

    property="" /*可选,   

 

        从传入的参数集合中使用属性名去获取值,   


        这个必须是一个List类型,   


        否则会出现OutofRangeException,   


        通常是参数使用java.util.Map时才使用,   


        如果传入的参数本身是一个java.util.List, 不能只用这个属性.  

 

        不知道为啥官网: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html#id386679  

 

        说这个属性是必须的, 但是测试的时候是可以不设置这个属性的, 还望那位大虾知道, 讲解一下.  

 

        */  

 

    conjunction="" /*可选,   

 

        iterate可以看作是一个循环,   

 

        这个属性指定每一次循环结束后添加的符号,   

 

         比如使每次循环是OR的, 则设置这个属性为OR*/  

 

    open="" /*可选, 循环的开始符号*/  

 

    close="" /*可选, 循环的结束符号*/  

 

    prepend="" /*可选, 加在open指定的符号之前的符号*/  

 

>

 

</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 all

 <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>

 

注意:本打算使主键实现自动增长,但是使用了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);

MyBatis provides a useful feature called "foreach" that allows you to iterate over a collection and generate dynamic SQL statements. It is commonly used when you need to pass multiple values as parameters to an SQL query or statement. To use the foreach feature in MyBatis, you need to follow these steps: 1. Start by defining a parameter that holds the collection you want to iterate over. This can be a List, Set, or Array. 2. In your SQL statement or query, use the "foreach" element to iterate over the collection. You can specify the collection parameter, an item name for each iteration, and an index name if needed. 3. Within the "foreach" element, you can use the item and index names to reference the current item and its index in the collection. Here's an example of how to use foreach in MyBatis: ```xml <select id="getUsersByIdList" resultType="User"> SELECT * FROM users WHERE id IN <foreach item="id" collection="idList" open="(" separator="," close=")"> #{id} </foreach> </select> ``` In this example, we have a parameter called "idList" that holds a List of user IDs. The foreach element is used to iterate over this list and generate the "IN" clause for the SQL query. Note that the "item" attribute specifies the name of the variable that represents each item in the collection (in this case, "id"), and the "collection" attribute specifies the parameter name holding the collection ("idList" in this case). The "open", "separator", and "close" attributes define the opening, separator, and closing characters for the generated SQL statement. By using foreach in MyBatis, you can dynamically generate SQL statements based on the values in your collection, which provides flexibility and reusability in your queries.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值