1、导入包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
版本3.3.1
2、Mapper.java文件
int insertBatch(List<SubWarn> list);
不需要@Param
3、Mapper.xml文件
<insert id="insertBatch" parameterType="java.util.List" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
insert into sub_warn(id,subject_name, warn_type, warn_levels, warn_title, warn_content,
town_name, town_code, town_uid,
county_name, county_code, county_uid,
city_name, city_code, city_uid,
province_name, province_code, province_id,
lon, lat, create_time, start_time, end_time, be_star, status, be_fake) values
<foreach collection="list" item="item" separator="," close=";">
(nextval('sub_warn_id_seq'),#{item.subjectName}, #{item.warnType}, #{item.warnLevels}, #{item.warnTitle}, #{item.warnContent},
#{item.townName}, #{item.townCode}, #{item.townUid},
#{item.countyName}, #{item.countyCode}, #{item.countyUid},
#{item.cityName}, #{item.cityCode}, #{item.cityUid},
#{item.provinceName}, #{item.provinceCode}, #{item.provinceId},
#{item.lon}, #{item.lat}, #{item.createTime}, #{item.startTime},#{item.endTime},
#{item.beStar},#{item.status},#{item.beFake})
</foreach>
</insert>
注意:数据设置好id自增,并在插入时也插入id,值为nextval(‘sub_warn_id_seq’)
4、调用
List<SubWarn> subWarns = new ArrayList<>();
SubWarn s1 = new SubWarn();
s1.setSubjectName("tt1");
SubWarn s2 = new SubWarn(s1);
s2.setSubjectName("tt2");
subWarns.add(s1);
subWarns.add(s2);
subWarnMapper.insertBatch(subWarns);
LOG.info("更新为:{}", JSONObject.toJSONString(subWarns));
返回结果打印id有值