在进行后端的操作时,批量添加总是少不了,话不多说,下面贴上代码

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实现批量添加的更多相关文章

  1. Mybatis实现批量添加操作

    Mybatis实现批量添加操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...

  2. mybatis之批量查询

    关于MyBatis批量更新和添加,参考我的如下文章即可:MyBatis的批量更新实例 MyBatis的批量添加实例 另外不管是批量的新增.删除.修改.查询也好,还是单个新增.删除.修改查询也罢.都会用 ...

  3. 【mybatis】之批量添加

    mybatis批量添加xml <insert id="batchCreate"> INSERT INTO `roomer` (`order`,name,idCard,m ...

  4. Mybatis 批量添加,批量更新

    此篇适合有一定的mybatis使用经验的人阅读. 一.批量更新 为了提升操作数据的效率,第一想到的是做批量操作,直接上批量更新代码: <update id="updateBatchMe ...

  5. mybatis 框架 的应用之二(批量添加、实现分页查询)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...

  6. Sql批量添加,批量查询,批量删除,批量修改。mybatis都有对应标签

    Sql批量添加,批量查询,批量删除,批量修改.mybatis都有对应标签

  7. MyBatis通过注解方式批量添加、修改、删除

    唯能极于情,故能极于剑 注: 本文转载于:CodeCow · 程序牛 的个人博客:http://www.codecow.cn/ 一.数据库实体DO public class User implemen ...

  8. Mybatis批量添加对象List

    1.对应的xml文件: <!--批量添加--><insert id="insertStandardItemInfo" parameterType="ha ...

  9. mybatis批量添加、批量删除

    <!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...

随机推荐

  1. 解析和遍历一个HTML文档

    如何解析一个HTML文档: String html = "<html><head><title>First parse</title>< ...

  2. C#多线程---Monitor实现线程同步

    一.简介 Monitor.Enter和Monitor.Exit方法来实现线程同步,这个属于排他锁,即每次只有一个线程可以访问共享数据. C#中通过lock关键字来提供简化的语法,lock可以理解为Mo ...

  3. 十二:Servlet3.0的注解

    1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener> <listener-class>ListenerClass</listene ...

  4. C# 对SQlServer访问的完整类

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  5. Python打印图片

    准备好图片:(我女票) python代码: # by gubin 6.20 from dyy from PIL import Image import sys import os def _main( ...

  6. (二)js基础。。。freecodecamp笔记

    个人需要注意的点 当 JavaScript 中的变量被声明的时候,程序内部会给它一个初始值undefined.当你对一个值为undefined的变量进行运算操作的时候,算出来的结果将会是NaN,NaN ...

  7. DFS常规解题套路

    本文为xdfApp团队成员文章,原文链接:https://blog.csdn.net/sinat_37380158/article/details/106866970 作者介绍:韩沛沛, 北京邮电大学 ...

  8. 你的域名是如何变成 IP 地址的?

    我的 个人网站 上线了,上面可以更好的检索历史文章,并且可以对文章进行留言,欢迎大家访问 可能大家都知道或者被问过一个问题,那就是很经典的「从浏览器输入 URL 再到页面展示,都发生了什么」.这个问题 ...

  9. 记录Mac下使用Charles抓包

    抓包 简述 在网络应用如后端系统,app,小程序等的开发过程中,免不了接口可能会报错,但是一般在app中或者小程序中没有便捷的console控制台,而且线上环境也不会开启调试模式,所以想看一下接口的响 ...

  10. RabbitMQ-初见

    目录 什么是中间件 消息队列协议 AMQP协议 MQTT协议 OpenMessage协议 Kafka协议 消息队列持久化 消息的分发策略 消息队列高可用和高可靠 什么是高可用机制 集群模式1 - Ma ...