MyBatis實現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)
批量插入數(shù)據(jù),多重forEach循環(huán)
在業(yè)務(wù)開發(fā)過程中,遇到批量插入時,需要進行多重forEach循環(huán)的情況。
下面是一個實際應(yīng)用
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; ? ?// 數(shù)據(jù)ID品牌 ? ?private String dataName; ? ?//數(shù)據(jù)名稱品牌 ? ?private String dataIds; ? ? ? // 數(shù)據(jù)ID品類 ? ?private String dataNames; ? ?//數(shù)據(jù)名稱品類 ? ?private String groupNo; ?//分組標(biāo)識 ? ?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,在批量插入時從而形成多重循環(huán)。
上圖為列表頁面,
上圖為添加頁面。部門名稱和品牌,品類名稱支持多選,而在保存時,需要將其拆分保存。在查詢時通過group_concat函數(shù)進行聚合展示在列表頁面。
故在批量插入數(shù)據(jù)時:
<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可以看出,根據(jù)kind不同,進行相應(yīng)的雙重forEach循環(huán)插入數(shù)據(jù)。
mybatis insert foreach
項目場景
報錯 ,找不到參數(shù)
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>
最后應(yīng)該這么寫才對 : 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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Idea導(dǎo)入多個maven項目到同一目錄下的方法示例
這篇文章主要介紹了Idea導(dǎo)入多個maven項目到同一目錄下,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Day14基礎(chǔ)不牢地動山搖-Java基礎(chǔ)
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08Java?Chassis3熔斷機制的改進路程技術(shù)解密
這篇文章主要介紹了Java?Chassis?3技術(shù)解密之熔斷機制的改進路程實例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01解決在for循環(huán)中remove list報錯越界的問題
這篇文章主要介紹了解決在for循環(huán)中remove list報錯越界的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12SpringBoot集成內(nèi)存數(shù)據(jù)庫Sqlite的實踐
sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用,本文主要介紹了SpringBoot集成Sqlite,具有一定的參考價值,感興趣的可以了解一下2021-09-09java并發(fā)編程專題(四)----淺談(JUC)Lock鎖
這篇文章主要介紹了java并發(fā)編程(JUC)Lock鎖的相關(guān)內(nèi)容,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06