MyBatis Bulk Add, modify, and delete _java

Source: Internet
Author: User
Tags commit

No more nonsense, straight to the point.

1. Add element Session.insert in bulk (String string,object o)

public void Batchinsertstudent () {
list<student> ls = new arraylist<student> ();
for (int i = 5;i < 8;i++) {
Student Student = new Student ();
Student.setid (i);
Student.setname ("Maoyuanjun" + i);
Student.setsex ("man" + i);
Student.settel ("tel" + i);
Student.setaddress ("Zhejiang province" + i);
Ls.add (student);
}
sqlsession session = Sessionfactoryutil.getsqlsessionfactory (). Opensession ();
Session.insert ("mybatisdemo.domain.Student.batchInsertStudent", ls);
Session.commit ();
Session.close ();
}
<insert id= "batchinsertstudent" parametertype= "java.util.List" >
insert INTO STUDENT (Id,name,sex,tel, Address)
VALUES 
<foreach collection= "list" item= "item" index= "index" separator= "," >
(#{ Item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
</foreach>

2, Batch modification session. Insert (String string,object o)

Example 1:

public void Batchupdatestudent () {
list<integer> ls = new arraylist<integer> ();
for (int i = 2;i < 8;i++) {
ls.add (i);
}
sqlsession session = Sessionfactoryutil.getsqlsessionfactory (). Opensession ();
Session.insert ("mybatisdemo.domain.Student.batchUpdateStudent", ls);
Session.commit ();
Session.close ();
}
<update id= "batchupdatestudent" parametertype= "java.util.List" >
update STUDENT SET name = "5566" WHERE ID in< C15/><foreach collection= "List" item= "item" index= "Index" open= "(" separator= "," close= ")" >
#{item}
" </foreach>
</update>

Example 2:

public void Batchupdatestudentwithmap () {
list<integer> ls = new arraylist<integer> ();
for (int i = 2;i < 8;i++) {
ls.add (i);
}
map<string,object> map = new hashmap<string,object> ();
Map.put ("idlist", ls);
Map.put ("name", "mmao789");
sqlsession session = Sessionfactoryutil.getsqlsessionfactory (). Opensession ();
Session.insert ("Mybatisdemo.domain.Student.batchUpdateStudentWithMap", map);
Session.commit ();
Session.close ();
}
<update id= "Batchupdatestudentwithmap" parametertype= "Java.util.Map" >
update STUDENT SET name = #{name} WHERE ID 
in <foreach collection= "idlist" index= "index" item= "Item" open= "(" separator= "," close= ")" > 
{Item} 
</foreach>

3, bulk Delete session.delete (String string,object o)

public void Batchdeletestudent () {
list<integer> ls = new arraylist<integer> ();
for (int i = 4;i < 8;i++) {
ls.add (i);
}
sqlsession session = Sessionfactoryutil.getsqlsessionfactory (). Opensession ();
Session.delete ("mybatisdemo.domain.Student.batchDeleteStudent", ls);
Session.commit ();
Session.close ();
}
<delete id= "batchdeletestudent" parametertype= "java.util.List" >
delete from STUDENT WHERE ID in
< foreach collection= "list" index= "index" item= "Item" open= "(" separator= "," close= ")" > 
#{item} 
</ Foreach>
</delete>

Well, this concludes the article, I hope to help you.

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.