Mybatis 插入记录时,传入的参数类型是hashmap,hashmap里面含有List(对比Mybatis insert into 参数类型parameterType为List),个人理解,这样更简洁, 大神如有更好的建议和示例,欢迎指正和分享。
1. DAO
int insertListContacts2(@Param("orgId") Long orgId, @Param("contactType") int contactType,@Param("contactInfo") List<OrganizationContactInfoParam> contactInfo);
2. Mapper.xml
注意:<foreach collection="list" item="item" separator=","> 里面不能有open="(", close=")",当然为空是可以的 open="" close=""
<insert id="insertListContacts2" parameterType="hashmap">
insert into t_org_contact ( Forg_id, Fcontact_type, Fcontact_name,Fcontact_mobile,Fcontact_email,
Fproduct, Fcontract_num )
values
<foreach collection="contactInfo" item="item" separator="," >
(#{orgId,jdbcType=BIGINT},
#{contactType,jdbcType=BIGINT},
#{item.contactName,jdbcType=VARCHAR},
#{item.contactMobile,jdbcType=VARCHAR},
#{item.contactEmail,jdbcType=VARCHAR},
#{item.product,jdbcType=VARCHAR},
#{item.contractNum,jdbcType=VARCHAR} )
</foreach>
</insert>
3. ServiceImpl
private void insertOrgContact(Long orgId, AbstractOrganizationInfoParam param) {
List<OrgContact> listBuz = new ArrayList<>();
List<OrgContact> listOpe = new ArrayList<>();
//业务联系人
if (CollectionUtils.isNotEmpty(param.getBuzContactInfo())) {
orgContactMapper.insertListContacts2(orgId, ContactTypeEnum.businiss.getCode(), param.getBuzContactInfo());
}
if (CollectionUtils.isNotEmpty(param.getOpeContactInfo())) {
orgContactMapper.insertListContacts2(orgId, ContactTypeEnum.operation.getCode(), param.getOpeContactInfo());
}
/* for (OrganizationContactInfoParam organizationContactInfoParam : param.getBuzContactInfo()) {
OrgContact orgContact = new OrgContact(organizationContactInfoParam);
orgContact.setOrgId(orgId);
orgContact.setContactType(ContactTypeEnum.businiss.getCode());
listBuz.add(orgContact);
}
if (CollectionUtils.isNotEmpty(listBuz)) {
orgContactMapper.insertListContacts(listBuz);
}
//运营联系人
for (OrganizationContactInfoParam organizationContactInfoParam : param.getOpeContactInfo()) {
OrgContact orgContact = new OrgContact(organizationContactInfoParam);
orgContact.setOrgId(orgId);
orgContact.setContactType(ContactTypeEnum.operation.getCode());
listOpe.add(orgContact);
}
if (CollectionUtils.isNotEmpty(listOpe)) {
orgContactMapper.insertListContacts(listOpe);
}*/
}
Mybatis insert into 参数类型parameterType为List: https://blog.csdn.net/qb170217/article/details/81316177