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

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. SpringCloud商品服务调用方式之Ribbon

    1.创建order_service项目 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  2. 一. Go微服务--隔离设计

    1. 前言 隔离设计源于船舶行业,一般而言无论大船还是小船,都会有一些隔板,将船分为不同的空间,这样如果有船舱漏水一般只会影响这一小块空间,不至于把整个船都给搞沉了. 同样我们的软件服务也是一个道理, ...

  3. 面试官问我MySQL索引,我

    面试官:我看你简历上写了MySQL,对MySQL InnoDB引擎的索引了解吗? 候选者:嗯啊,使用索引可以加快查询速度,其实上就是将无序的数据变成有序(有序就能加快检索速度) 候选者:在InnoDB ...

  4. Linux centos7 nginx 平滑升级

    2021-08-19为了方便读者的阅读,该文通篇使用绝对路径,各位朋友们在实际上操作中可以根据实际情况编写路径(#^.^#)1. 当前环境 # system cat /etc/redhat-relea ...

  5. Redis++:Redis 内存爆满 之 淘汰策略

    前言: 我们的redis使用的是内存空间来存储数据的,但是内存空间毕竟有限,随着我们存储数据的不断增长,当超过了我们的内存大小时,即在redis中设置的缓存大小(maxmeory 4GB),redis ...

  6. 图像处理之Canny边缘检测(一)

    一:历史 Canny边缘检测算法是1986年有John F. Canny开发出来一种基于图像梯度计算的边缘 检测算法,同时Canny本人对计算图像边缘提取学科的发展也是做出了很多的贡献.尽 管至今已经 ...

  7. Vue Abp vNext获取当前登录用户

    系统默认提供了获取当前用户的api方法 https://localhost:44364/api/identity/my-profile 手工实现方法:abp后台获取当前用户需要在AppService应 ...

  8. JS013. 重写toFixed( )方法,toFixed()原理 - 四舍五入?银行家舍入法?No!六舍七允许四舍五入√!

    以下为场景实测与原理分析,需要重写函数请直接滚动至页尾!!! 语法 - Number.prototype.toFixed( ) // toFixed()方法 使用定点表示法来格式化一个数值. numO ...

  9. Hibernate持久层ORM框架

    一.概念 hibernate交互数据库时,对象的属性转成sql,mybatis直接写sql,性能更高: 二.

  10. vue.js框架图片上传组件

    html: <div id="app"> <div class="hello"> <div class="upload& ...