170515、mybatis批量操作
//Java代码
public void batchAdd(){
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
List<Student> list = new ArrayList<Student>();
Student student1 = new Student();
student1.setName("新增" + new Random().nextInt(10));
student1.setAge(16);
Student student2 = new Student();
student2.setName("新增" + new Random().nextInt(10));
student2.setAge(16);
list.add(student1);
list.add(student2);
int result = mapper.batchAdd(list);
sqlSession.commit();
if (result > 0) {
System.out.println("----------batchAdd success-----");
} else {
System.out.println("----------batchAdd fail---------");
}
}
<!-- 批量新增 -->
<insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" >
<selectKey resultType="int" keyProperty="id" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into t_student (name,age)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name},${item.age})
</foreach>
</insert>
//Java代码
public void batchDelList(){
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
List<Integer> list = new ArrayList<Integer>();
list.add(23);
list.add(24);
int result = mapper.batchDelStr(list);
sqlSession.commit();
if (result > 0) {
System.out.println("----------batchDel success-----");
} else {
System.out.println("----------batchDel fail---------");
}
}
<!-- 批量删除-list -->
<delete id="batchDelStr" parameterType="java.util.List">
delete from t_student
where id in
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
//Java代码
public void batchDelArray(){
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
int[] ids = new int[]{27,28};
int result = mapper.batchDelArrays(ids);
sqlSession.commit();
if (result > 0) {
System.out.println("----------batchDel success-----");
} else {
System.out.println("----------batchDel fail---------");
}
}
<!-- 批量删除-array -->
<delete id="batchDelArrays" parameterType="int">
delete from t_student
where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
170515、mybatis批量操作的更多相关文章
- mybatis批量操作-xml方式
在实际项目中,我们一般都会用到批量insert.delete.update等操作,由于使用频率还是蛮高的,这里就做个简单的记录,供以后学习和参考. 批量insert 在数据库中,批量插入可以是多条in ...
- Mybatis 批量操作-删除、修改和查询
批量操作的核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中一个,其它的就可以举一反三,触类旁通.它之所以执行效率高,是因为合并后日志量(MySQL的binlog和InnoDB的 ...
- MyBatis 批量操作、集合遍历-foreach
在使用mybatis操作数据库时,经常会使用到批量插入.IN条件查询的情况,这时就难免要使用到foreach元素.下面一段话摘自mybatis官网: foreach 元素的功能是非常强大的,它允许你指 ...
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
问题背景: 在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下: com.chenzhou.base.mybati ...
- mybatis 批量操作增删改查
在介绍批量操作之前,首先先介绍一个语法:foreach.可以说是,foreach是整个批量操作的灵魂. 属性 描述 item 循环体中的具体对象. 支持属性的点路径访问,如item.age,item. ...
- mybatis 批量操作 ------持续更新
mybatis 不存在则才进行添加 # 添加的 sql 语句insert into sys_link_post_user(post_id,user_id)# 进行批量添加 (若不需要可以取消 froe ...
- (七)mybatis批量操作,分页插件
首先使用方式很简单: SqlSession sqlSession = sessionFactory.openSession(ExecutorType.BATCH); 批量操作核心:改变执行sql的方式 ...
- on duplicate key update 的用法说明(解决批量操作数据,有就更新,没有就新增)mybatis批量操作数据更新和添加
项目用的ORM框架是用springdatajpa来做的,有些批量数据操作的话,用这个效率太低,所以用mybatis自己写sql优化一下. 一般情况,我们肯定是先查询,有就修改,没有就添加,这样的话,单 ...
- Mybatis批量操作
首先,mysql需要数据库连接配置&allowMultiQueries=true jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true& ...
随机推荐
- 如何对抗、预防 SQL注入 攻击
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...
- node.js执行shell命令
nodejs功能强大且多样,不只是可以实现 服务器端 与 客户端 的实时通讯,另一个功能是用来执行shell命令 首先,引入子进程模块 var process = require('child_pro ...
- URI、URL和URN之间的区别
URI标识一个事物,URL定位一个事物:然而,位置同样可以标识一个事物,所以,每个URL都是一个 URI,但一个URI并不一定是一个URL. 举例说明 罗杰·佩特 这是我的名字,这是一个标识.它就像一 ...
- c#调用cmd
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- PHP curl_setopt函数用法介绍补充篇
1.curl数据采集系列之单页面采集函数get_html 单页面采集在数据采集过程中是最常用的一个功能 有时在服务器访问限制的情况下 只能使用这种采集方式 慢 但是可以简单的控制 所以写好一个常用的c ...
- DWORD类型
INT 代表int,DWORD 代表 unsigned longint会随着机器位数的不同而发生变化,比如在16位机上为16为,在32位机上为32位,在64位机上为64位.看看最原始的定义就知道了. ...
- CSS3 经典教程系列:CSS3 圆角(border-radius)详解
http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-border-radius.html 特别好的一篇文章
- Spring------Spring boot data jpa的使用方法
1.DoMain.java import org.springframework.boot.SpringApplication; import org.springframework.boot.aut ...
- android webView不简单
手机屏幕大小非常伤程序猿 励志成为一名Javaproject师的我.真的被它伤到了,不仅由于webView的强大.并且这个内容适合各样屏幕大小问题. 想当年苹果project师嘲笑安卓project师 ...
- C语言函数參数传递原理
C语言中參数的传递方式一般存在两种方式:一种是通过栈的形式传递.还有一种是通过寄存器的方式传递的. 这次.我们仅仅是具体描写叙述一下第一种參数传递方式,第二种方式在这里不做具体介绍. 首先,我们看一下 ...