MyBatis实现批量插入数据,多重forEach循环
程序员文章站
2022-06-10 07:58:01
目录批量插入数据,多重foreach循环下面是一个实际应用mybatis insert foreach项目场景批量插入数据,多重foreach循环在业务开发过程中,遇到批量插入时,需要进行多重fore...
批量插入数据,多重foreach循环
在业务开发过程中,遇到批量插入时,需要进行多重foreach循环的情况。
下面是一个实际应用
public class sysroledata extends dataentity<sysroledata> { private static final long serialversionuid = 1l; private string kind; //类别(1:按部门2:按角色) private string roleid; // role_id private string rolename; //角色名称 private string officeid; //office_id private string officename; //部门名称 private string type; // 1.品牌 2.品类 3.品牌&品类 private string dataid; // 数据id品牌 private string dataname; //数据名称品牌 private string dataids; // 数据id品类 private string datanames; //数据名称品类 private string groupno; //分组标识 private string useable; //是否可用(1:可用,0:不可用) private string remarks; //备注 private list<string> officeidlist = lists.newarraylist(); private list<string> roleidlist = lists.newarraylist(); private list<brandcategoryvo> datalist = lists.newarraylist(); public sysroledata() { super(); } public sysroledata(string id){ super(id); } public string getkind() { return kind; } public void setkind(string kind) { this.kind = kind; } @length(min=0, max=45, message="role_id长度必须介于 0 和 45 之间") public string getroleid() { return roleid; } public void setroleid(string roleid) { this.roleid = roleid; } public string getrolename() { return rolename; } public void setrolename(string rolename) { this.rolename = rolename; } public string getofficename() { return officename; } public void setofficename(string officename) { this.officename = officename; } @length(min=0, max=45, message="office_id长度必须介于 0 和 45 之间") public string getofficeid() { return officeid; } public void setofficeid(string officeid) { this.officeid = officeid; } @length(min=0, max=4, message="品类长度必须介于 0 和 45 之间") public string gettype() { return type; } public void settype(string type) { this.type = type; } @notnull public string getdataid() { return dataid; } public void setdataid(string dataid) { this.dataid = dataid; } public string getdataname() { return dataname; } public void setdataname(string dataname) { this.dataname = dataname; } public string getdataids() { return dataids; } public void setdataids(string dataids) { this.dataids = dataids; } public string getdatanames() { return datanames; } public void setdatanames(string datanames) { this.datanames = datanames; } public string getuseable() { return useable; } public void setuseable(string useable) { this.useable = useable; } public string getremarks() { return remarks; } public void setremarks(string remarks) { this.remarks = remarks; } public list<brandcategoryvo> getdatalist() { return datalist; } public void setdatalist(list<brandcategoryvo> datalist) { this.datalist = datalist; } public list<string> getofficeidlist() { return officeidlist; } public void setofficeidlist(list<string> officeidlist) { this.officeidlist = officeidlist; } public list<string> getroleidlist() { return roleidlist; } public void setroleidlist(list<string> roleidlist) { this.roleidlist = roleidlist; } public string getgroupno() { return groupno; } public void setgroupno(string groupno) { this.groupno = groupno; } }
如上所示为一个实体类,会有datalist和roleidlist或officeidlist,在批量插入时从而形成多重循环。
上图为列表页面,
上图为添加页面。部门名称和品牌,品类名称支持多选,而在保存时,需要将其拆分保存。在查询时通过group_concat函数进行聚合展示在列表页面。
故在批量插入数据时:
<insert id="insert"> insert into sys_role_data( kind, role_id, office_id, type, data_id, data_name, group_no, useable, remarks, create_date, create_by, update_date, update_by )values <if test="kind != null and kind == 0"> <foreach collection="officeidlist" item="officeid" separator=","> <foreach collection="datalist" item="data" separator=","> ( #{kind}, null, #{officeid}, #{type}, #{data.id}, #{data.name}, #{groupno}, #{useable}, #{remarks}, #{createdate}, #{createby.id}, #{updatedate}, #{updateby.id} ) </foreach> </foreach> </if> <if test="kind != null and kind == 1"> <foreach collection="roleidlist" item="roleid" separator=","> <foreach collection="datalist" item="data" separator=","> ( #{kind}, #{roleid}, null, #{type}, #{data.id}, #{data.name}, #{groupno}, #{useable}, #{remarks}, #{createdate}, #{createby.id}, #{updatedate}, #{updateby.id} ) </foreach> </foreach> </if> </insert>
由上面sql可以看出,根据kind不同,进行相应的双重foreach循环插入数据。
mybatis insert foreach
项目场景
报错 ,找不到参数
org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.binding.bindingexception: parameter ‘statusinfoid’ not found. available parameters are [collection, list]
@mapper public interface patrolrecordmapper extends basemapper<patrolrecord> { int insertlist(@param(value = "list") list<patrolrecord> list); }
mapper 换了很多种写法
<insert id="insertlist" parametertype="com.iricto.soft.patrol.entity.patrolrecord"> insert into patrol_record(status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class,`status`) values <foreach collection="list" item="patrolrecord" separator=","> (patrolrecord.#{statusinfoid}, patrolrecord.#{routeid}, patrolrecord.#{placename}, patrolrecord.#{patroltime}, patrolrecord.#{patroluser}, patrolrecord.#{patrolclass}, patrolrecord.#{status}) </foreach> </insert>
mapper
<insert id="insertlist" parametertype="com.iricto.soft.patrol.entity.patrolrecord"> insert into patrol_record(status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class,`status`) values <foreach collection="list" item="list" open="(" separator="," close=")"> list.#{statusinfoid}, list.#{routeid}, list.#{placename}, list.#{patroltime}, list.#{patroluser}, list.#{patrolclass}, list.#{status} </foreach> </insert>
最后应该这么写才对 : mapper
<insert id="insertlist" parametertype="com.iricto.soft.patrol.entity.patrolrecord"> insert into patrol_record(status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class,`status`) values <foreach collection="list" item="patrolrecord" separator=","> ( #{patrolrecord.statusinfoid}, #{patrolrecord.routeid}, #{patrolrecord.placename}, #{patrolrecord.patroltime}, #{patrolrecord.patroluser}, #{patrolrecord.patrolclass}, #{patrolrecord.status}) </foreach> </insert>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: WINDOWS特有的消息常量标识符(vb,vbs常用)
下一篇: vbs ping实现的两种方式
推荐阅读
-
Mybatis数据批量插入如何实现
-
Mybatis批量插入数据返回主键的实现
-
MyBatis 实现批量插入和删除中双层循环的写法案例
-
mybatis foreach批量插入数据:Oracle与MySQL区别
-
mybatis foreach批量插入数据:Oracle与MySQL的区别
-
想实现批量开会员卡,我的思路是用循环向数据库里插入数据,有更好的方法吗?
-
想实现批量开会员卡,我的思路是用循环向数据库里插入数据,有更好的方法吗?
-
MyBatis实现批量插入数据,多重forEach循环
-
sqlite循环批量插入数据采用批处理文件实现
-
Oracle + Mybatis 实现数据库批量插入、修改、删除