文章目录

  • 一、List<String>
  • 二、List<IntEntity>
  • 三、再次修改

MyBatis使用foreach批量插入一个实体类数据,其中这个实体类包含一个List的成员变量。

即一个student类,里面含有一个hobby的成员变量,这个hobby由于可以有很多,所以用一个list来存。这个使用的数据可能是name=张三,hobby={跑步 ,唱歌,游泳},最终存到数据库里面就是三条数据,分别是张三-跑步,张三-唱歌,张三-游泳。

该测试背景是基于自己工作时代码,当时测试没有成功,于是被迫使用常规方式——根据list成员变量的长度去循环插入数据。(最终放弃使用foreach最重要的一个原因是,我报错然后询问有经验的前辈原因,他跟我说foreach不能这样用)

但是根据我自己搭建的Demo来看,其实是可行的。


废话少说,先上一个基础版代码

一、List<String>

成员变量list集合内只有一个变量,此处为一个String类型的变量

1、新建实体类POJO

对应的实体类成员变量

@Repository
public class OutEntity {private String name;private String age;private List<Integer> intEntities;
}

2、新建数据库表

第一阶段只需要使用name、age、intentity三个字段

CREATE TABLE `testmysql`.`Untitled`  (`name` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,`age` int(11) NULL DEFAULT NULL,`intentity` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,`intname` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,`intage` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

3、编写对应的SQL和mapper

@Repository
public interface MobianMapper {public int saveOutEntity(OutEntity outEntity);
}
<insert id="saveOutEntity" parameterType="pers.mobian.testtransactional.pojo.OutEntity">insert into outentity (name,age,intentity) values<foreach collection="intEntities" separator="," item="items">(#{name},#{age},#{items})</foreach>
</insert>

4、编写配置文件

# mysql基本配置
serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/testmysql?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver# 自己mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/**/*Mapper.xml

5、构建数据,开始测试

需要连接数据库测试,配置文件自行

@Test
public void test3() {OutEntity outEntity = new OutEntity();// 1、构建外层对象数据outEntity.setAge("2");outEntity.setName("mobian");// 2、构建list集合成员变量List<Integer> list =new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);outEntity.setIntEntities(list);mobianMapper.saveOutEntity(outEntity);
}

6、测试结果

数据插入测试成功,这也是我当时百度到的案例。

但是我自己业务场景,是下面这种。

二、List<IntEntity>

1、修改实体类

将放在List集合中Integer类型的参数修改为一个实体类型

@Repository
public class IntEntity {private String intName;private String intAge;
}
@Repository
public class OutEntity {private String name;private String age;private List<IntEntity> intEntities;
}

2、修改数据库表结构

还是之前的表,因为之前在新建表时把对应的字段已经添加好了,intname、intage

3、修改对应的SQL

这里的#{intName}、#{intAge}是有问题的,后面会再处理,注意!!!

<insert id="saveOutEntity" parameterType="pers.mobian.testtransactional.pojo.OutEntity">insert into outentity (name,age,intname,intage) values<foreach collection="intEntities" separator="," item="items">(#{name},#{age},#{intName},#{intAge})</foreach></insert>

4、修改测试数据,再次测试

设置的数据格式

OutEntity(name=mobian, age=3,
intEntities=[IntEntity(intName=5, intAge=7),
IntEntity(intName=9, intAge=8),
IntEntity(intName=0, intAge=0)])

@Test
public void test3() {OutEntity outEntity = new OutEntity();outEntity.setAge("3");outEntity.setName("mobian");// 组装list成员变量List<IntEntity> list =new ArrayList<IntEntity>();IntEntity intEntity = new IntEntity("5","7");IntEntity intEntity2 = new IntEntity("9","8");IntEntity intEntity3 = new IntEntity("0","0");list.add(intEntity);list.add(intEntity2);list.add(intEntity3);outEntity.setIntEntities(list);mobianMapper.saveOutEntity(outEntity);
}

很不幸,测试失败

// 报错信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'intName' in 'class pers.mobian.testtransactional.pojo.OutEntity'atorg.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy66.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy67.saveOutEntity(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

在公司代码编写的时候,我就因为这个错误,然后打住了,换一个方式处理了。

错误信息意思:intName参数在OutEntity实体类中没有get方法,可是我想我的intName参数在IntEntity中,OutEntity实体类里面肯定没有呀,然后我再一想,这里的错误应该是.xml文件中在识别这个#{intName}标志位的时候,使用反射的方式去调用OutEntity实体类中的方法getIntName,然后找不到对应的方法,然后报错。

但是我们指定的intName应该指定到IntEntity实体类中,然后我将对应的intName修改为item.intName


三、再次修改

于是我将#{intName}修改为#{items.intName},插入测试成功

<insert id="saveOutEntity" parameterType="pers.mobian.testtransactional.pojo.OutEntity">insert into outentity (name,age,intname,intage) values<foreach collection="intEntities" separator="," item="items">(#{name},#{age},#{items.intName},#{items.intAge})</foreach></insert>

最终插入数据库的数据效果:

查看日志中对应的SQL格式:

MyBatis使用foreach批量插入一个含List<实体>成员变量的实体类相关推荐

  1. MyBatis 使用 foreach 批量插入

    yml文件 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ ...

  2. Mybatis使用foreach批量插入Oracle提示命令未正确结束

    错误代码 <insert id="multiInsertTest" parameterType="java.util.List">INSERT IN ...

  3. mybatis多个foreach_使用 Mybatis 的 foreach 批量模糊 like 查询及批量插入

    使用 mybatis 平时都是用遍历集合 in ( ) ....,其实还可以多模糊查询和批量操作等其他操作,要明白 foreach 元素的属性主要意义,灵活使用,举例如下. 1.根据多个品牌名字分类, ...

  4. Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案

    Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 参考文章: (1)Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 (2)https://www.cnblogs. ...

  5. 如何定义一个布尔类型的成员变量

    一般情况下,我们可以有以下四种方式来定义一个布尔类型的成员变量: boolean success boolean isSuccess Boolean success Boolean isSuccess ...

  6. 创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 编程利用Rectangle输出一个矩形的周长和面积

    创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 编程利用Rectangle输出一个矩形的周长和面积 publi ...

  7. 90页第三题,创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 ,编程利用Rectangle输出一个矩形的

    //90页第三题,创建一个 Rectangle类,添加width和lenght两个成员变量 //在 Rectangle类中添加两种方法分别计算矩形的周长和面积 //编程利用Rectangle输出一个矩 ...

  8. Mybatis foreach 批量插入

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

  9. Java mybatis实现mysql批量插入

    记录下来方便自己,同时也希望能对比较迷惑的小盆友有所帮助 1.把批量插入的数据生成一个List集合 2.用java控制一次插入的条数和集合 // 每次插入10条int len = count, inc ...

  10. 16、mybatis动态sql 批量插入

    文章目录 1.EmployeeMapper 2.EmployeeMapper.xml(以逗号间隔执行一条语句)(推荐) 3.Test 4.以分号间隔执行每条语句(第二种方式) 5.Oracle下的批量 ...

最新文章

  1. SQLServer数据库如何收缩日志空间?
  2. ORACLE 格式VARCHAR2(n CHAR) 与VARCHAR2(n)的区别
  3. mysql 如何调用函数结果_MySQL自定义函数调用不出结果
  4. 行为设计模式 - 策略设计模式
  5. 机器学习十大经典算法之随机森林
  6. php srem,SREM命令_视频讲解_用法示例-redis编程词典-php中文网
  7. 震惊:2/3 被黑的网站隐藏着后门
  8. Android内容提供者(读取手机联系人信息)
  9. 片段中未调用onActivityResult
  10. Not enough information to list image symbols. Not enough information to list load addresses in ...
  11. 如何将windows桌面默认位置修改为D盘
  12. 专升本英语固定词组搭配500个
  13. pr如何跳到关键帧_【技能培训营】Pr基本操作(五)
  14. codewars 7×7 Skyscrapers 问题解决
  15. 配置Dot1q终结子接口实现跨设备VLAN间通信示例
  16. vue 实现文字转语音tts
  17. 利用Python做excel文本合并(根据左侧单元格,快速合并右侧单元格内容)
  18. python 源代码 macd双底 高 低_MACD双底背离_选股公式,炒股软件,炒股公式,股票软件,股票公式_指标编写互助答疑论坛_理想论坛 - 股票论坛...
  19. 15天基础爬虫、selenium、scrapy使用,附全程实现代码
  20. c语言合法标识符号大全,C语言合法标识符

热门文章

  1. 挑战性题目DSCT401:全源最短路径Floyd算法的并行实现
  2. LOJ2420「NOIP2015」神奇的幻方
  3. [2018.07.12 T3] B君的第三题
  4. js基础-8-浅拷贝和深拷贝
  5. 计算机专业面试 英文,计算机专业英文面试自我介绍.doc
  6. python股票回测_主流 Python 量化回测平台,回测速度客观评测
  7. python语言的核心理念是_Python编程语言的核心是什么?
  8. display函数怎么使用_使用Python写一个小游戏alien invasion!
  9. asp在线html编辑器,ASP下使用FCKeditor在线编辑器的方法
  10. mysql增删改查 表格_mysql 数据表 增删改查