MyBatis批量添加、修改和删除
1、批量添加元素session.insert(String string,Object o)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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> |
实例2:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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></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>MyBatis批量添加、修改和删除的更多相关文章
- ASP.NET MVC用存储过程批量添加修改数据
用Entity Framework 进行数据库交互,在代码里直接用lamda表达式和linq对数据库操作,中间为程序员省去了数据库访问的代码时间,程序员直接可以专注业务逻辑层的编写.但是对于比较复杂的 ...
- EF 批量 添加 修改 删除
1批量添加 db.T_Investigator.AddRange(list) 2批量删除 db.T_Investigator.RemoveRange(list) 3批量修改 for 循 ...
- mybatis批量添加、批量删除
<!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...
- DNS添加/修改/查询/删除A记录
#查询DNS可用类 Get-WmiObject -Namespace root\MicrosoftDNS -List #查询所有资源记录 $mydns = [WMIClass]"ROOT\M ...
- Mybatis 批量添加,批量更新
此篇适合有一定的mybatis使用经验的人阅读. 一.批量更新 为了提升操作数据的效率,第一想到的是做批量操作,直接上批量更新代码: <update id="updateBatchMe ...
- Zabbix 4.0 API 实践,主机/主机群组 批量添加模板和删除模板
场景 我们日常在管理Zabbix 的时候,经常会需要批量添加模板和批量删除模板,Zabbix页面是提供的批量链接的功能,但是它链接的也只是当前页的主机,我们想扩展这个功能,在链接的时候,可以批量链接整 ...
- Mybatis批量添加、更新小结
虽然是很基础的东西,不过难免会忘记,所以写个笔记巩固一下,顺便分享. 实体类: @Data public class EventOrder { private Long id; private ...
- Mybatis批量添加对象List
1.对应的xml文件: <!--批量添加--><insert id="insertStandardItemInfo" parameterType="ha ...
- myBatis批量添加实例
<!-- 批量添加中转地数据 --> <insert id="addBatch" parameterType="com.isoftstone. ...
- myBatis批量添加,修改和删除
摘自: http://blog.csdn.net/myjlvzlp/article/details/8434376 1.批量添加元素session.insert(String string,Objec ...
随机推荐
- 输入法出现 footer被挤上去的问题
/** * 修改点击input输入框时的位置 *input框获取焦点footer隐藏,失去焦点时显示 */ $('.input-footer-none').on('focus',function(){ ...
- JavaScript ES6部分语法
ES6是JavaScript语言的新版本,它也可以叫做ES2015,之前学习的JavaScript属于ES5,ES6在它的基础上增加了一些语法,ES6是未来JavaScript的趋势,而且vue组件开 ...
- hdu 5178(二分-lower_bound,upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ubuntu、linux更换pip源
1.创建pip配置文件 vi ~/.pip/pip.conf 2.写入配置内容 [global] trusted-host = mirrors.aliyun.com index-url = http: ...
- HDU 2504.又见GCD-递归
又见GCD Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 10、Django实战第10天:找回密码
今天完成的功能是:用户忘记密码后,通过注册邮箱重置密码... 首先还是把前端页面准备好,把forgetpwd.html复制到templates目录下 编辑users.views.py,创建一个忘记密码 ...
- 对mysql数据库表的相关操作
虫师博客(Python使用MySQL数据库(新)): https://www.cnblogs.com/fnng/p/3565912.html 1.更改表的结构,增加一个字段放置新增的属性 alter ...
- [CF819B]Mister B and PR Shifts
题意:定义一个排列$p_{1\cdots n}$的“偏移量”$D=\sum _{i=1}^n\left|p_i-i\right|$ 求它所有的轮换排列中偏移量最小的是多少,要求输出轮换序数 暴力就是求 ...
- Java生成扫描可以生成手机号名片的二维码
(1)需求:用户通过扫描pc端网站后台管理系统的二维码获取对接人的相关信息,主要是是手机号信息,达到点击可以直接打电话或者将对接人的信息直接保存到通讯录 注:源码来源:https://blog.csd ...
- centos 7.3systemctl工具
http://www.cnblogs.com/tswcypy/p/4479153.html