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 ...
随机推荐
- Linux编写Shell脚本
——<Linux就该这么学>笔记Shell脚本命令的工作方式有两种 交互式: 用户每输入一条命令就立即执行 批处理: 由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中 ...
- php性能调试工具介绍
php版本:php7 xhprof: xhprof是php5.*下很好的性能测试工具,配合xhprof_html能够图形显示测试结果,基本够用, 但已经没人维护了. tideways: 在php7下, ...
- Mac-WIFI总是断网
$ cd /Library/Preferences/SystemConfiguration/ 删除如下文件 com.apple.airport.preferences.plist com.apple. ...
- mysql分页查询语法
一.limit语法 SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 SELECT 语句返回指 ...
- echarts 图表建立步骤
需要引用的文件 <script src="web/mobile/js/jquery-1.8.3.min.js"></script> <script s ...
- Django remedy a security issue refer dos attack
Today the Django team is issuing multiple releases -- Django 1.4.8, Django 1.5.4, and Django 1.6 bet ...
- ubantu启动盘制作
转载自http://jingyan.baidu.com/article/b24f6c82cf50e086bfe5dae9.html 1 首先打开UltraISO软件,没有的请百度搜索,下载安装,尽量下 ...
- HDU 2521 反素数(数论,比较)
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...
- 见微知著(三):解析ctf中的pwn--Fastbin和bins的溢出
1月1号写博客,也是不容易呀!大家新年快乐呀! 先从Fastbin看起,是2015年RCTF的一道pwn题,shaxian.先看看代码的大致流程,随便输入一下: 这个题目关键之处在于堆溢出,对于堆种类 ...
- 闪迪U3利用工具U3-Pwn
闪迪U3利用工具U3-Pwn 闪迪U3是闪迪公司为Sandisk Cruzer系列U盘提供的一个功能.该模块支持数据加密和CD启动功能.U3-Pwn就是针对U3的一个利用工具.渗透测试人员可以通过 ...