MyBatis实现批量添加
在进行后端的操作时,批量添加总是少不了,话不多说,下面贴上代码
Mybatis代码:
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO
tb_product_category(product_category_name, priority,
create_time, shop_id)
VALUES
<foreach collection="list" item="productCategory" index="index"
separator=",">
(
#{productCategory.productCategoryName},
#{productCategory.priority},
#{productCategory.createTime},
#{productCategory.shopId}
)
</foreach>
</insert>
使用动态SQL,foreach标签进行遍历
collection定义为 list ,以列表的形式添加进去
item:可自定义,最好与实体类相关
index:计数器
separator:分隔符,比如这种( ?, ?, ?, ? ) , ( ?, ?, ?, ? )
Junit测试类代码
@Test
public void testABatchInsertProductCategory(){
ProductCategory productCategory = new ProductCategory();
productCategory.setProductCategoryName("商品类别1");
productCategory.setPriority(1);
productCategory.setCreateTime(new Date());
productCategory.setShopId(1L);
ProductCategory productCategory2 = new ProductCategory();
productCategory2.setProductCategoryName("商品类别2");
productCategory2.setPriority(2);
productCategory2.setCreateTime(new Date());
productCategory2.setShopId(1L);
List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();
productCategoryList.add(productCategory);
productCategoryList.add(productCategory2);
int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
assertEquals(2, effectedNum);
}
结果展示

MyBatis实现批量添加的更多相关文章
- Mybatis实现批量添加操作
Mybatis实现批量添加操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...
- mybatis之批量查询
关于MyBatis批量更新和添加,参考我的如下文章即可:MyBatis的批量更新实例 MyBatis的批量添加实例 另外不管是批量的新增.删除.修改.查询也好,还是单个新增.删除.修改查询也罢.都会用 ...
- 【mybatis】之批量添加
mybatis批量添加xml <insert id="batchCreate"> INSERT INTO `roomer` (`order`,name,idCard,m ...
- Mybatis 批量添加,批量更新
此篇适合有一定的mybatis使用经验的人阅读. 一.批量更新 为了提升操作数据的效率,第一想到的是做批量操作,直接上批量更新代码: <update id="updateBatchMe ...
- mybatis 框架 的应用之二(批量添加、实现分页查询)
lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...
- Sql批量添加,批量查询,批量删除,批量修改。mybatis都有对应标签
Sql批量添加,批量查询,批量删除,批量修改.mybatis都有对应标签
- MyBatis通过注解方式批量添加、修改、删除
唯能极于情,故能极于剑 注: 本文转载于:CodeCow · 程序牛 的个人博客:http://www.codecow.cn/ 一.数据库实体DO public class User implemen ...
- Mybatis批量添加对象List
1.对应的xml文件: <!--批量添加--><insert id="insertStandardItemInfo" parameterType="ha ...
- mybatis批量添加、批量删除
<!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...
随机推荐
- SpringMVC之@ControllerAdvice
@ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...
- JS对象创建的几种方法
最近一直在看JS高级程序设计这本书,有空来梳理一下几种创建对象的方式.话不多说,直接步入正题. 第一种:Object构造函数创建 var Person = new Object(); Person.n ...
- mzy对于反射的复习
反射其实就是指在超脱规则的束缚,从强引用到弱相关,在上帝视角做事情,对于写配置文件,和一些框架的源码,得到调用上至关重要,java带有解释器的语法特性,使得泛型一类的语法糖和反射配合之后更如鱼得水! ...
- 【C语言】第4章 选择结构程序设计
第4章 选择结构程序设计 C语言有两种选择语句: if 语句,实现两个分支的选择结构 switch 语句,实现多分支的选择结构 输入3个数a,b,c,要求按由小到大的顺序输出. 可以先用伪代码写出算法 ...
- JavaScript高级程序设计学习笔记之事件
1.事件流 事件流描述的是从页面中接收事件的顺序. 事件冒泡 IE的事件流叫做事件冒泡(event bubbling),即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播 ...
- docker-harbor私有仓库使用笔记
1. 登录harbor管理页面,创建项目,比如yuqx_test 2. admin登录,此处免密登录,正常情况下会输入账号密码 [root@k8s-rancher2 ~]# docker login ...
- SpringBoot 属性配置文件数据注入配置和yml与properties区别
前言 我们知道SpringBoot 通过配置类来解放一堆的xml文件配置,通属性配置文件,来进行,系统全局属性配置,这样极大的简化了我们开发过程,java web 也可以甜甜的从此 快速配置 Spri ...
- Java HashMap工作原理:不仅仅是HashMap
前言: 几乎所有java程序员都用过hashMap,但会用不一定会说. 近年来hashMap是非常常见的面试题,如何为自己的回答加分?需要从理解开始. "你用过hashMap吗?" ...
- Django项目中的模板继承
1. 定义一个基础的页面HTML文件base.html <!DOCTYPE html> <html lang="en"> <head> < ...
- Hibernate持久层ORM框架
一.概念 hibernate交互数据库时,对象的属性转成sql,mybatis直接写sql,性能更高: 二.