In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how to achieve bulk data insertion in MyBatis, multiple forEach cycle related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Bulk insert data, multiple forEach loops
In the process of business development, multiple forEach loops are needed when batch inserts are encountered.
The following is a practical application public class SysRoleData extends DataEntity {private static final long serialVersionUID = 1L; private String kind; / / category (1: by department 2: by role) private String roleId; / / role_id private String roleName; / / role name private String officeId; / / office_id private String officeName; / / department name private String type; / / 1. Brand 2. Category 3. Brand & Category private String dataId; / / data ID Brand private String dataName; / / data name Brand private String dataIds; / / data ID Category private String dataNames; / / data name Category private String groupNo; / / Group ID private String useable; / / available (1: available, 0: unavailable) private String remarks; / / remarks private List officeIdList = Lists.newArrayList () Private List roleIdList = Lists.newArrayList (); private List 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 length must be between 0 and 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 length must be between 0 and 45") public String getOfficeId () {return officeId;} public void setOfficeId (String officeId) {this.officeId = officeId;} @ Length (min=0, max=4, message= "category length must be between 0 and 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 getDataList () {return dataList;} public void setDataList (List dataList) {this.dataList = dataList;} public List getOfficeIdList () {return officeIdList;} public void setOfficeIdList (List officeIdList) {this.officeIdList = officeIdList;} public List getRoleIdList () {return roleIdList } public void setRoleIdList (List roleIdList) {this.roleIdList = roleIdList;} public String getGroupNo () {return groupNo;} public void setGroupNo (String groupNo) {this.groupNo = groupNo;}}
As shown above as an entity class, there will be dataList and roleIdList or officeIdList, which forms multiple loops when batch inserts.
The picture above is the list page.
The image above shows the add page. Department name and brand name, category name support multiple selection, and when saving, it needs to be split and saved. When querying, aggregating through the group_concat function is displayed on the list page.
Therefore, when inserting data in bulk:
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 (# {kind}, null, # {officeId}, # {type} # {data.id}, # {data.name}, # {groupNo}, # {useable}, # {remarks}, # {createDate}, # {createBy.id}, # {updateDate} # {updateBy.id}) (# {kind}, # {roleId}, null, # {type}, # {data.id}, # {data.name}, # {groupNo}, # {useable} # {remarks}, # {createDate}, # {createBy.id}, # {updateDate}, # {updateBy.id})
As can be seen from the above sql, according to the difference of kind, the corresponding double forEach loop is used to insert data.
Mybatis insert foreach project scenario
Error reported, parameter not found
Org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'statusInfoId' not found. Available parameters are [collection, list]
Mapperpublic interface PatrolRecordMapper extends BaseMapper {int insertList (@ Param (value = "list") List list);}
Mapper has changed many ways of writing.
Insert into patrol_record (status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class, `status`) VALUES (patrolRecord.# {statusInfoId}, patrolRecord.# {routeId}, patrolRecord.# {placeName}, patrolRecord.# {patrolTime}, patrolRecord.# {patrolUser}, patrolRecord.# {patrolClass}) PatrolRecord.# {status})
Mapper
Insert into patrol_record (status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class, `status`) VALUES list.# {statusInfoId}, list.# {routeId}, list.# {placeName}, list.# {patrolTime}, list.# {patrolUser}, list.# {patrolClass} List.# {status}
In the end, it should be written like this: mapper
Insert into patrol_record (status_info_id,route_id,place_name,patrol_time, patrol_user,patrol_class, `status`) VALUES (# {patrolRecord.statusInfoId}, # {patrolRecord.routeId}, # {patrolRecord.placeName}, # {patrolRecord.patrolTime}, # {patrolRecord.patrolUser}, # {patrolRecord.patrolClass} # {patrolRecord.status}) these are all the contents of the article "how to insert data in batches in MyBatis, multiple forEach loops" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.