要想批量插入,首先要知道forEach标签:

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。

foreach元素的属性主要有 item,index,collection,open,separator,close。

item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map

PersonDaoImpl

/*** Created by 李柏霖* 2020/10/15 11:13*/package com.lbl.dao;import com.lbl.domain.Person;import java.util.List;public interface IPersonDao {void savePersonList(List<Person> person);
}

IPersonService

/*** Created by 李柏霖* 2020/10/15 11:07*/package com.lbl.service;import com.lbl.domain.Person;import java.util.List;public interface IPersonService {void savePersonList(List<Person> person);
}

IPersonDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lbl.dao.IPersonDao"><insert id="savePersonList" parameterType="list">insert into person(name,money) values<foreach collection="list" index="index" item="Person" open="" close=""  separator=",">(#{Person.name},#{Person.money})</foreach></insert>
</mapper>

不写open="" close=""进而的参数。

在(#{Person.name},#{Person.money})参数外加上()

就可以达到(?,?),(?,?),(?,?)的效果

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--   设置扫描包,包下设置注解@Service @Repository @Component @AutoWried--><context:component-scan base-package="com.lbl"><!--    由于springmvc的controller是由springmvc来扫描,需要将controller排除在外--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 四大信息--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="jdbc:mysql://47.115.79.211:3308/ssm"></property><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="user" value="root"></property><property name="password" value="admin123"></property></bean><!-- session工厂--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><!-- com.wzx.domain.Person  person--><property name="typeAliasesPackage" value="com.lbl.domain"/></bean><!-- IPersonDao.xml  IPersonDao.java--><bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.lbl.dao"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean><!--配置Spring框架声明式事务管理--><!--配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="find*" read-only="true"/><tx:method name="*" isolation="DEFAULT"/></tx:attributes></tx:advice><!--配置AOP增强--><aop:config><aop:pointcut id="service" expression="execution(* com.lbl.service.impl.*ServiceImpl.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="service"/></aop:config></beans>

log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=F:/ssmTest/log4j/ssm2.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

transactionTest

/*** Created by 李柏霖* 2020/10/16 11:22*/package com.lbl.transaction;import com.lbl.domain.Person;
import com.lbl.service.IPersonService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.ArrayList;
import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class transactionTest {@AutowiredIPersonService service;@Testpublic void test03(){List<Person> personList = new ArrayList<>();personList.add(new Person("jack",100.00));personList.add(new Person("rose",200.00));personList.add(new Person("tony",300.00));service.savePersonList(personList);}}

sql执行结果:

使用批量插入执行的SQL语句应该等价于:

insert into person(name,money) values (?,?) , (?,?) , (?,?)

mybatis方法参数是list的批量插入相关推荐

  1. Mybatis 针对ORACLE和MYSQL的批量插入与多参数批量删除

    今天利用Mybatis的<for each>标签做oracle的批量插入数据时,发现和MySQL数据库有区别.在此记录下,以防之后再踩坑. 一.批量插入: 1.controller: /* ...

  2. MyBatis直接执行SQL查询及批量插入数据

    转:http://www.cnblogs.com/mabaishui/archive/2012/06/20/2556500.html 一.直接执行SQL查询: 1.mappers文件节选 <re ...

  3. vue excel导入mysql详细教程_Vue前端上传EXCEL文件,后端(springBoot+MyBatis+MySQL)解析EXCEL并批量插入/更新数据库...

    文章目录 Vue前端 后端 controller层 service层:如何解析Excel文件 MyBatis:实现批量插入 在mysql中设置唯一索引Unique Index MySQL中的inser ...

  4. Mybatis结合Oracle的foreach insert批量插入报错!

    2019独角兽企业重金招聘Python工程师标准>>> 最近做一个批量导入的需求,将多条记录批量插入数据库中.解决思路:在程序中封装一个List集合对象,然后把该集合中的实体插入到数 ...

  5. 【经典】Mybatis百万级高效批量插入

    先贴出最终解决办法 由于分析过程冗长,这里直接给出结论,直接copy去就可以用,若有时间和兴趣再继续往下看看具体解决办法的分析过程. public class MybatisBatchUtils {/ ...

  6. MyBatis批量插入大量数据

    1. 思路分析 批量插入这个问题,我们用 JDBC 操作,其实就是两种思路吧: 用一个 for 循环,把数据一条一条的插入(这种需要开启批处理). 生成一条插入 sql,类似这种 insert int ...

  7. Mybatis foreach 批量插入

    在mybatis中可以使用foreach标签做批量插入和更新操作,以批量插入为例: <insert id="insertMsg" parameterType="xz ...

  8. mybatisPlus批量插入性能优化

    背景:物联网平台背景,传感器采集频率干到了1000Hz,分了100多张表出来,还是把mysql干炸了.当前单表数据量在1000来w,从kafka上拉数据异步批量插入,每次插入数据量1500条,测试的时 ...

  9. 大数据写入到Oracle数据库(批量插入数据)

    开发中经常遇到批量插入数据的需求,为了提高开发效率大多会使用ORM架构,个别之处 才会手写SQL,我们使用C#.NET Core5.0开发,所以优先选择了微软的EF. 但是EF原生没有批量操作功能,需 ...

最新文章

  1. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 获取 linker 中的 dlopen 函数地址 并 通过 远程调用 执行该函数 )
  2. 用宏定义实现函数值互换
  3. 惊呆了!颜值爆表的20+位阿里技术女神同一时间向你发出共事邀请!
  4. python函数调用键盘热键_如何使用Python控制键盘和鼠标
  5. Python多线程编程基础1:为什么要使用线程
  6. 财务报表五力、五性分析雷达图
  7. 有时候,一个人也挺好
  8. 微信小程序 点击复制文本到剪贴板
  9. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-10.Springboot2.x用户登录拦截器开发实战...
  10. 10g添加用户 oracle_oracle10g下新建/删除用户
  11. 浅谈seo行业白菜价泛滥
  12. [Unity]技巧分享:更改Unity Asset Store 默认下载资源位置的方法
  13. ios开发-教程选择
  14. 基于人脸特征点实现疲劳检测
  15. 微信 支付 h5 开发 使用 best-pay-sdk
  16. 关于hibernate检索策略
  17. 【OpenCV 例程300篇】202. 查表快速替换(cv.LUT)
  18. java秒换算成时分秒的形式
  19. Flutter 热更新功能实现
  20. 联手腾讯八百客CRM实现“本土化”弯道超车

热门文章

  1. ne_comment 表
  2. An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means
  3. Linux网络技术学习(一)—— sk_buff数据结构解析
  4. c++使用vector求矩阵的A的逆
  5. (Samsung)Netsol SRAM,Novachips SATA3 SSD Controller,Zywyn 3V RS232,PLX
  6. 数据分析师的日常工作是什么?
  7. 基于UDP的可靠传输——QUIC 协议
  8. [转帖]改变无数人人生的32句实话[ChaseDream论坛]
  9. 联想计算机怎么添加打印机,电脑和联想打印机连接不上怎么办啊
  10. ORACLE应用产品和SAP、SSA、SYMIX产品的比较分析