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>

    /////

  • 相关阅读:
    struts2 批量上传.
    jsoup html解析器 实现对博客园博文标题链接抓取
    赶鸭子上架的cdq分治
    RE:从零开始的莫比乌斯反演
    我永远无法学会的dp
    gym 101915
    2017-2018 ACM-ICPC Latin American Regional Programming Contest GYM101889
    网络流24T
    re:从零开始的数位dp
    Codeforces Round 504
  • 原文地址:https://www.cnblogs.com/mustanglqt/p/10594211.html
Copyright © 2011-2022 走看看