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

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. ABC类IP地址

    A类IP地址一个A类IP地址由1字节的网络地址和3字节主机地址组成,网络地址的最高位 必须是"0", 地址范围从1.0.0.0 到126.0.0.0.可用的A类网络有126个,每个 ...

  2. 一些Java知识点

    1 import java.util.ArrayList; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 ...

  3. 使用Eclipse搭建SSM框架(Spring + Spring MVC + Mybatis)

    1.创建项目 1)打开Eclipse,点击File --> New --> Other 2)输入maven,找到Maven Project 3)然后一直按Next,直到出现一下界面: 4) ...

  4. MySQL-SQL基础-子查询

    #子查询-某些情况下,当进行查询的时候,需要的条件是另外一个select语句的结果,这个时候就要用到子查询.用于子查询的关键字主要包括: in.not in.=.!=.exists.not exist ...

  5. Nginx配置文件详解与优化建议

    1.概述 今天来详解一下Nginx的配置文件,以及给出一些配置建议,希望能对大家有所帮助. 2.nginx.conf 1)配置文件位置 nginx 安装目录的 conf 文件夹下,例如:/usr/lo ...

  6. eclipse建立c语言工程以及成功下载到FPGA芯片过程遇到的各种问题以及解决方法详解

    推荐大家预先建立好一个工程目录文件夹,确实挺好用,参考正点原子的pdf教程,如下图所示, 我们eclipse在software文件夹建立一个workspace即可 选择用helloworld模板建立工 ...

  7. Lambda@edge 实现负载均衡器功能

    一般的业务实现流程为CDN->ELB->EC2,但OTT业务往往会产生很高的流量费用,如果使用常规的架构,流量费用会成倍增加,为了降低费用,我们对架构做了一些优化. AWS Cloudfr ...

  8. 你的 JVM 基础“大厦”稳健吗?

    [从 1 开始学 JVM 系列] JVM 对于每位 Java 语言编程者来说无疑是"重中之重",尽管我们每天都在与它打交道,却很少来审视它.了解它,慢慢地,它成为了我们" ...

  9. Redis详解(二)——

    https://www.cnblogs.com/yeya/p/14274948.html https://www.cnblogs.com/liang24/tag/redis/

  10. Git - Mac 电脑使用 brew 更新 Git

    安装 Homebrew Homebrew 是一个软件包管理器.它的作用就是将软件包安装到自己的目录中,然后将其文件符号链接到 /usr/local.更多信息,请自行进入官网查看 https://bre ...