Mybatis insert into 参数类型parameterType为List

Mybatis 插入记录时,传入的参数类型是List (可以对比下一篇文章 传入的参数类型是hashmap, 而且hashmap里面包含List)

1. DAO

int insertListContacts(List<OrgContact> record);

2. Mapper.xml

注意:<foreach collection="list" item="item" separator=","> 里面不能有open="(" close=")"

<insert id="insertListContacts" parameterType="java.util.List">
        insert into t_org_contact ( Forg_id, Fcontact_type, Fcontact_name,Fcontact_mobile,Fcontact_email,
        Fproduct, Fcontract_num )
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.orgId,jdbcType=BIGINT},
            #{item.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<>();

        //业务联系人       
       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为hashmap,hashmap里面含有List: https://blog.csdn.net/qb170217/article/details/81315955

 

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中的INSERT操作可以通过mapper.xml文件来定义。在mapper.xml文件中,可以使用<insert>标签来定义INSERT操作。例如,可以在<insert>标签中指定插入数据的表名和字段,并使用#{参数名}来表示要插入的值。比如,下面是一个示例的mapper.xml文件中的INSERT操作的定义: <insert id="add" parameterType="EStudent"> insert into TStudent(name, age) values(#{name}, #{age}) </insert> 在上面的示例中,<insert>标签的id属性指定了方法名为"add",parameterType属性指定了传入的参数类型为EStudent。在INSERT语句中,使用了#{name}和#{age}来表示要插入的name和age字段的值。 在Mapper接口中,可以定义与mapper.xml文件中INSERT操作对应的方法。根据需要,可以选择不同的返回值类型。例如,可以定义返回类型为空void的方法、返回类型为布尔类型boolean的方法,或者返回类型为删除的条数long的方法。以下是一些定义INSERT操作的示例方法: // 返回值为空void void insert(Employee emp); // 返回值为布尔类型boolean boolean insert(Employee emp); // 返回值为删除的条数long long insert(Employee emp) 通过定义mapper.xml文件和Mapper接口中的方法,可以实现MyBatis中的INSERT操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [MyBatis魔法堂:Insert操作详解(返回主键、批量插入)](https://blog.csdn.net/weixin_33910759/article/details/85668982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [16. Mybatis 插入操作-insert](https://blog.csdn.net/zongf0504/article/details/100103946)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值