• 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>

    /////

  • 相关阅读:
    ios UIWebView截获html并修改便签内容(转载)
    IOS获取系统时间 NSDate
    ios 把毫秒值转换成日期 NSDate
    iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用
    IOS开发 xcode报错之has been modified since the precompiled header was built
    iOS系统下 的手机屏幕尺寸 分辨率 及系统版本 总结
    iOS 切图使用 分辨率 使用 相关总结
    整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础
    Java面试之PO,VO,TO,QO,BO
    Notes模板说明
  • 原文地址:https://www.cnblogs.com/mustanglqt/p/10594211.html
Copyright © 2020-2023  润新知