您好,登錄后才能下訂單哦!
今天小編給大家分享一下MyBatis怎么實現批量插入數據,多重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循環插入數據。
報錯 ,找不到參數
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>
以上就是“MyBatis怎么實現批量插入數據,多重forEach循環”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。