1. Batch Add
<span style= "FONT-SIZE:18PX;" ><span style= "Font-family:times New Roman;" >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>
</insert></ Span>
</span>
2. Batch Modification
<span style= "Font-family:times New roman;font-size:18px;"
> Instance 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 < ; foreach collection= "list" item= "item" index= "Index" open= "(" separator= "," close= ")" > #{item} </foreach> <
;/update> instance 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} </for
Each> </update></span>
3. Bulk Delete
<span style= "Font-family:times New roman;font-size:18px;" >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></span>