Ibatis Iterate use

Source: Internet
Author: User
Tags bulk insert

(turn) Ibatis to achieve iterate use

From: http://hongzhguan.iteye.com/blog/1222353

<iterate

Property= ""/* optional,

Use the property name in the passed-in parameter collection to get the value,

This must be a list type,

Otherwise there will be outofrangeexception,

Usually the parameters are used when using JAVA.UTIL.MAP,

If the passed in parameter itself is a java.util.List, it cannot be used only with this property.

I don't know why. Official website: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html#id386679

Say this property is necessary, but the test can not set this property, but also look at the shrimp know, explain.

*/

Conjunction= ""/* optional,

Iterate can be seen as a cycle,

This property specifies the symbols that are added after each loop,

For example, to make each loop an OR, set this property to or*/

Open= ""/* optional, looping start symbol */

Close= ""/* optional, Loop end symbol */

Prepend= ""/* optional, plus the symbol before the open specified symbol */

>

</iterate>

<!--Bulk Delete objects, iterate do not property properties--

<delete id= "Delstudybook" parameterclass= "Java.util.List" >

Delete from Studybook WHERE ID in

<iterate conjunction= "," property= "AAA"

Open= "(" close= ")" >

#aaa []#

</iterate>

</delete>

Pay attention to the property's error

caused By:com.ibatis.common.beans.ProbeException:Error getting ordinal list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException:String index out of range: 1

Also: If the parameterclass= "java.util.List" type does not match

Error caused by:java.sql.SQLException:Invalid Parameter object type. Expected ' Java.util.Map ' but found ' java.util.ArrayList '.

===============================

<!--bulk modification of objects, iterate must include property properties--

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

Note Do not make the property attribute error

Cause:com.ibatis.sqlmap.client.SqlMapException:ParameterObject or property is not a Collection, Array or Iterator.

===============================

<!--iterate, queries based on multiple matching criteria, like in (a,b,c)--

<select id= "Selectbyiterate" parameterclass= "java.util.List" resultclass= "User" >

SELECT * from the USERS WHERE user_id in

<iterate conjunction= "," open= "(" close= ")" >

#ids []#

</iterate>

</select>

Note: Do not attribute property, otherwise error. String index out of range: 1

2. However, if the parameter has multiple incoming one is list, the other is not, Parameterclass is a map, the property attribute is required to differentiate the collection to traverse.

=================================================

<!--Bulk Add objects--

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

<!--must be added-

SELECT * FROM dual

</insert>

Note: This is intended to allow the primary key to grow automatically, but hibernate_sequence is used. Nextval, error

Violates the unique constraint (SCOTT. sys_c0010057), please expert guidance under.

==================================================

Test the configuration SQL statement on top.

Delete a book's information

public int Deletestudybook (list<integer> booklist) {

try {

Return This.client.delete ("Delstudybook", Booklist);

} catch (SQLException e) {

E.printstacktrace ();

return 0;

}

}

Modify the user's information

public int updateusers (map<string,object> Map) {

try {

Return This.client.delete ("Updateusersiterate", map);

} catch (SQLException e) {

E.printstacktrace ();

return 0;

}

}

Using iterate to implement BULK insert data

public void Barkinsert (list<users> List) {

try {

This.client.insert ("barkinsertusers", list);

} catch (SQLException e) {

E.printstacktrace ();

}

}

=========================

Test iterate implement a function query similar to 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 procedures in the test class

Removal of tests

List<integer> list=new arraylist<integer> ();

List.add (1);

List.add (2);

Delete a book

Dao.deletestudybook (list);

----------------------------------------------------

Test Modify user Information

Map<string,object> map=new hashmap<string,object> ();

Set the Modified field

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

----------------------------------------------------------

Methods for testing queries

List<integer> list=new arraylist<integer> ();

List.add (1);

List.add (2);

Dao.querybyiterate (list);

---------------------------------------------------------

Test BULK INSERT Record

List<users> userlist=new arraylist<users> ();

Mode one: Not applicable to autogrow column (id setting value: #list [].user_id#]

Users user1=new users (Ten, "bbbb", "bbbb", "bbbb", "bbbb", "bbbb", "bbbb");

Users user2=new users (one, "CCCC", "CCCC", "CCCC", "CCCC", "CCCC", "CCCC");

Dao.barkinsert (userlist);

Ibatis Iterate use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.