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

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. swagger2 注解说明文档

    @Api:用于类上,说明该类的作用.可以标记一个Controller类做为swagger 文档资源 @Api(value = "xxx", description = " ...

  2. 同步(synchronized)

    转载至:https://www.cnblogs.com/dolphin0520/p/3923737.html 一.什么时候会出现线程安全问题? 在单线程中不会出现线程安全问题,而在多线程编程中,有可能 ...

  3. PsSetCreateProcessNotifyRoutineEx 创建回调函数

    转载自http://blog.csdn.net/yushiqiang1688/article/details/5209597 最近要做一个进程监控的程序,功能很简单,就是创建和退出进程的时候,能触发我 ...

  4. roslaunch 启动时修改参数

    启动命令: roslaunch beginner_tutorials turtlemimic.launch arg1:=3.0 查询命令: rosparam get /param1 可以看到param ...

  5. 本地yum源搭建

    2021/07/15 1.挂载 # 创建挂载目录 mkdir /mnt/cdrom # 挂载 mount -t iso9660 /dev/cdrom /mnt/cdrom 2.修改 yum 源配置# ...

  6. vue 手机号码隐藏中间

    template {{ phone | mobileFilter }}   data export default { filters:{         mobileFilter(val){     ...

  7. Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.

    背景 有一个 Flask 项目,然后有一个路由返回的是 dict 通过浏览器访问,结果报错 关键报错信息 TypeError: 'dict' object is not callable The vi ...

  8. 三剑客之sed编辑器 基操

    目录: 一.sed编辑器 二.打印内容 三.使用地址 四.删除行 五.替换 六.插入 一.sed编辑器 sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流. sed ...

  9. Java入门准备:Java开发环境的安装与卸载

    Java的三大版本 JavaSE:标准版 JavaME:嵌入式开发 JavaEE:企业级开发 JDK(Java Development Kit):Java开发者工具包 JRE(Java Runtime ...

  10. CodeForce-803C Maximal GCD(贪心数学)

    Maximal GCD CodeForces - 803C 现在给定一个正整数 n.你需要找到 k 个严格递增的正整数 a1, a2, ..., ak,满足他们的和等于 n 并且他们的最大公因数尽量大 ...