1.controller层创建一个List<>集合存放要批量插入的实体对象
List<GwyhNjh> gwyhNjhList = new ArrayList<GwyhNjh>();
for(int n = 0;n < 10;n++){
GwyhNjh gwyhNjh = new GwyhNjh();
gwyhNjh.setId(UUID.randomUUID().toString());
gwyhNjh.setYear(new Date());
gwyhNjh.setRemark('123');
gwyhNjh.setStatus("1");
gwyhNjhList.add(gwyhNjh);
}
//调用批量插入方法
int a = gwyhNjhService.insertBatch(gwyhNjhList);
2.编写批量插入方法接口
int insertBatch(List<GwyhNjh> gwyhNjhList);
3.编写批量插入方法接口实现
@Override
public int insertBatch(List<GwyhNjh> gwyhNjhList) {
return gwyhNjhMapper.insertBatch(gwyhNjhList);
}
4.编写dao接口
int insertBatch(@Param("gwyhNjhList") List<GwyhNjh> gwyhNjhList);
5.编写xml文件
<insert id="insertBatch">
insert into GWYH_NJH(ID,YEAR,REMARK,STATUS)
<foreach collection="gwyhNjhList" item="gwyhNjh" separator="union all" >
select #{gwyhNjh.id},#{gwyhNjh.year},#{gwyhNjh.remark},#{gwyhNjh.status} from dual
</foreach>
</insert>