Mybatis insert into 参数类型parameterType为hashmap,hashmap里面含有List

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值