Mybatis批量添加,删除与修改

1、批量添加元素session.insert(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("浙江省" + 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>

 2、批量修改session. insert (String string,Object o)

 1 实例1:
 2 public void batchUpdateStudent(){
 3     List<Integer> ls = new ArrayList<Integer>();
 4     for(int i = 2;i < 8;i++){
 5         ls.add(i);
 6     }
 7     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
 8     session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);
 9     session.commit();
10     session.close();
11 }
12 <update id="batchUpdateStudent" parameterType="java.util.List">
13     UPDATE STUDENT SET name = "5566" WHERE id IN
14     <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
15         #{item}
16     </foreach>
17 </update>
18
19 实例2:
20 public void batchUpdateStudentWithMap(){
21     List<Integer> ls = new ArrayList<Integer>();
22     for(int i = 2;i < 8;i++){
23         ls.add(i);
24     }
25     Map<String,Object> map = new HashMap<String,Object>();
26     map.put("idList", ls);
27     map.put("name", "mmao789");
28     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
29     session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);
30     session.commit();
31     session.close();
32 }
33 <update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
34     UPDATE STUDENT SET name = #{name} WHERE id IN
35     <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
36         #{item}
37     </foreach>
38 </update>

3、批量删除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>
时间: 2024-10-06 03:23:04

Mybatis批量添加,删除与修改的相关文章

myBatis批量添加,修改和删除

摘自: http://blog.csdn.net/myjlvzlp/article/details/8434376 1.批量添加元素session.insert(String string,Object o) public void batchInsertStudent(){ List<Student> ls = new ArrayList<Student>(); for(int i = 5;i < 8;i++){ Student student = new Student(

&lt;linux小脚本&gt;批量添加/删除用户

批量添加/删除用户,当输入add时,判断用户是否存在,存在则显示存在,不存在则添加:当输入del时,判断用户是否存在,存在则删除用户,不存在则显示不存在. #!/bin/bash if [ $1 == "add" ];then for i in {1..10}; do if id user$i &> /dev/null;then echo "the user$i exists!" else useradd user$i &> /dev/n

EF6基础系列(12)--- EF进行批量添加/删除

EF6添加了批量添加/删除实体集合的方法,我们可以使用DbSet.AddRange()方法将实体集合添加到上下文,同时实体集合中的每一个实体的状态都标记为Added,在执行SaveChange()方法时为每个实体执行Insert操作:同样的我们使用DbSet.RemoveRange()方法将集合中的所有实体都标记为deleted状态,在执行SaveChange()方法时为每一条数据执行delete操作. 通过AddRange()和RemoveRange()方法可以有效提升性能,所以建议在进行不批

mybatis批量添加、批量删除

<!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.util.List" useGeneratedKeys="false"> insert into tb_namelist_syn_his (SERIAL, RECORDNUMBER, HASHTBNAME, CTLTYPE, SOURCE, CREATETIME, CKEY, C

批量添加删除用户

批量添加用户 #!/bin/bash read -p "Enter the User Password : " PASSWD for UNAME in `cat users.txt` do id $UNAME &> /dev/null if [ $? -eq 0 ];then echo "Already exists" else useradd $UNAME &> /dev/null echo "$PASSWD" |

Mybatis批量添加对象List

1.对应的xml文件: <!--批量添加--><insert id="insertStandardItemInfo" parameterType="hashmap"> insert into t_standard_item(standard_id,item_id) values <foreach collection="itemIdList" index="index" item="it

MyBatis批量添加和删除

p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; font-size: 10.5pt; font-family: 等线 } h1 { margin-right: 0cm; margin-left: 0cm; font-size: 24.0pt; font-family: 宋体; font-weight: bold } p { margin-right

KVM脚本批量添加删除虚拟机版本2

在原有的基础上,做些功能上的添加. 修改虚拟机的主机名 修改虚拟机的MAC 修改虚拟机的IP 虚拟机采用qcow2格式,使用qemu-img的backing_file技术,快速生成虚拟机 这样,虚拟机创建好后,便可以远程管理了. 第1版,请参考: http://5ydycm.blog.51cto.com/115934/1211630 第2版,create_delete_vm.py代码: #!/usr/bin/env python #coding:utf-8 ###################

&lt;linux小脚本&gt;case实现批量添加/删除用户

#!/bin/bash #输入add添加用户,输入del删除用户,-v选择是否显示结果信息 DEBUG=0 ADD=0 DEL=0 for i in `seq 0 $#`; do if [ $# -gt 0 ];then case $1 in -v) DEBUG=1 shift ;; -h) echo "please open the help page....." exit 0 ;; --add) ADD=1 ADDUSER=$2 shift 2 ;; --del) DEL=1 DE