zoukankan      html  css  js  c++  java
  • mybatis 多条数据插入,判断表中是否含有将插入的数据,插入没有的数据。

    多条数据,需要条件筛选之后插入到数据表:

    <insert id="insertExpectCardLabelInfo" parameterType="java.util.List">
    //插入表字段 

     INSERT INTO expect_know_label (
        expect_know_label_id,
        user_id,
        expect_know_label_data_id,
        del_flag,
        create_time,
        create_user,
        update_time,
        update_user)


    //满足条件的数据表 重命名 
      select
        expect_know_label_id,
        user_id,
        expect_know_label_data_id,
        del_flag,
        create_time,
        create_user,
        update_time,
        update_user
      from (

    //满足条件的数据,传入集合bean,遍历数据

    <foreach collection="list" item="item" index="index" separator="UNION ALL">
      select
        #{item.expectId:VARCHAR} as expect_know_label_id,
        #{item.userId:VARCHAR} as user_id,
        #{item.expectDataId:VARCHAR} as expect_know_label_data_id,
        0 as del_flag,
        now() as create_time,
        #{item.userId:VARCHAR} as create_user,
        now() as update_time,
        #{item.userId:VARCHAR} as update_user
      from dual
    </foreach>
      ) A

    //根据需求筛选出需要的数据

    where A.user_id=#{userId:VARCHAR} and A.del_flag=0
    and A.expect_know_label_data_id not in
    (select expect_know_label_data_id from expect_know_label where user_id=#{userId:VARCHAR} and del_flag=0)

    </insert>

    //数据插入成功。

    如何只是需要插入数据,而不对数据进行筛选的,可以直接进行<foreach>遍历,例如

    select 
        expect_know_label_id,
        user_id,
        expect_know_label_data_id,
        del_flag,
        create_time,
        create_user,
        update_time,
        update_user

    <foreach collection="list" item="item" index="index" separator="UNION ALL">
        #{item.expectId:VARCHAR},
        #{item.userId:VARCHAR},
        #{item.expectDataId:VARCHAR},
        0,
        now(),
        #{item.userId:VARCHAR},
        now(),
        #{item.userId:VARCHAR} 
    </foreach>

    /////

  • 相关阅读:
    OPC-UA和IEC 62541协议
    excel多级部门字符串截取其中一端的公式
    mac 显示音频文件 速率
    解决:Mac安装HOME brew 拒绝了我们的连接请求解决方案
    ss自定义规则
    macos关闭更新功能
    mac 使用触摸板左键长按选择是,总是弹出系统自带词典的问题
    固定区域截图快速粘贴到表格
    Outlook 2016 for Mac 更改邮件存放路径
    微信发送高清视频(避免被微信压缩变模糊),100M以内
  • 原文地址:https://www.cnblogs.com/mustanglqt/p/10594211.html
Copyright © 2011-2022 走看看