当前位置:   article > 正文

mybatis批量插入时判断是否存在 如果存在就更新或忽略_mybatis批量插入时校验

mybatis批量插入时校验

mybatis批量插入时判断是否存在 如果存在就更新

 建立了一个唯一的约束 payroll_number,来判断是否重复 

  1. <insert id="insertStaffSalaryList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
  2. insert into staff_salary
  3. (user_id, group_id, payroll_number, approval_date, user_name, id_card, group_name, join_date, leave_date, online_days, work_months,
  4. tb_sumpremium, xb_sumpremium, xb_sumnetpremium, zb_sumpremium, zb_sumnetpremium, incentive_coefficient, basic_subsidy,
  5. is_probation, harvest_allowance, basic_salary, talking_time_reward, personal_task, sumpremium, complex_coefficient,
  6. late_Leave_early, take_leave, sick_leave, absenteeism, full_attendance, total_attendance, recommend_will,
  7. business_violation, lmdi_performance, coulmn_id, coulmn1, coulmn2, coulmn3, coulmn4, coulmn5, coulmn6, coulmn7,
  8. coulmn8, coulmn9, oth_reward, performance, kpi, oth_bonus, total_wages, created_name, created_date, created_id, profit_insurance_coefficient)
  9. values
  10. <foreach collection="list" item="item" index="index" separator=",">
  11. (
  12. #{item.userId,jdbcType=INTEGER}, #{item.groupId,jdbcType=INTEGER}, #{item.payrollNumber,jdbcType=VARCHAR},
  13. #{item.approvalDate,jdbcType=TIMESTAMP}, #{item.userName,jdbcType=VARCHAR},
  14. #{item.idCard,jdbcType=VARCHAR}, #{item.groupName,jdbcType=VARCHAR}, #{item.joinDate,jdbcType=TIMESTAMP},
  15. #{item.leaveDate,jdbcType=TIMESTAMP}, #{item.onlineDays,jdbcType=DECIMAL}, #{item.workMonths,jdbcType=INTEGER}, #{item.tbSumpremium,jdbcType=DECIMAL},
  16. #{item.xbSumpremium,jdbcType=DECIMAL}, #{item.xbSumnetpremium,jdbcType=DECIMAL}, #{item.zbSumpremium,jdbcType=DECIMAL},
  17. #{item.zbSumnetpremium,jdbcType=DECIMAL}, #{item.incentiveCoefficient,jdbcType=DECIMAL}, #{item.basicSubsidy,jdbcType=INTEGER},
  18. #{item.isProbation,jdbcType=VARCHAR}, #{item.harvestAllowance,jdbcType=INTEGER}, #{item.basicSalary,jdbcType=DECIMAL},
  19. #{item.talkingTimeReward,jdbcType=INTEGER}, #{item.personalTask,jdbcType=INTEGER}, #{item.sumpremium,jdbcType=INTEGER},
  20. #{item.complexCoefficient,jdbcType=INTEGER}, #{item.lateLeaveEarly,jdbcType=INTEGER}, #{item.takeLeave,jdbcType=INTEGER},
  21. #{item.sickLeave,jdbcType=INTEGER}, #{item.absenteeism,jdbcType=INTEGER}, #{item.fullAttendance,jdbcType=INTEGER},
  22. #{item.totalAttendance,jdbcType=INTEGER}, #{item.recommendWill,jdbcType=INTEGER},#{item.businessViolation,jdbcType=INTEGER},
  23. #{item.lmdiPerformance,jdbcType=INTEGER}, #{item.coulmnId,jdbcType=INTEGER}, #{item.coulmn1,jdbcType=VARCHAR},
  24. #{item.coulmn2,jdbcType=VARCHAR}, #{item.coulmn3,jdbcType=VARCHAR}, #{item.coulmn4,jdbcType=VARCHAR},
  25. #{item.coulmn5,jdbcType=VARCHAR}, #{item.coulmn6,jdbcType=VARCHAR}, #{item.coulmn7,jdbcType=VARCHAR},
  26. #{item.coulmn8,jdbcType=VARCHAR}, #{item.coulmn9,jdbcType=VARCHAR}, #{item.othReward,jdbcType=DECIMAL},
  27. #{item.performance,jdbcType=DECIMAL}, #{item.kpi,jdbcType=DECIMAL}, #{item.othBonus,jdbcType=DECIMAL}, #{item.totalWages,jdbcType=DECIMAL},
  28. #{item.createdName,jdbcType=VARCHAR}, #{item.createdDate,jdbcType=TIMESTAMP}, #{item.createdId,jdbcType=INTEGER}, #{item.profitInsuranceCoefficient,jdbcType=DECIMAL}
  29. )
  30. </foreach>
  31. on duplicate key update
  32. approval_date = values(approval_date), user_name = values(user_name), work_months = values(work_months), tb_sumpremium = values(tb_sumpremium) ,
  33. xb_sumpremium = values(xb_sumpremium), xb_sumnetpremium = values(xb_sumnetpremium), zb_sumpremium = values(zb_sumpremium), zb_sumnetpremium = values(zb_sumnetpremium) ,
  34. incentive_coefficient = values(incentive_coefficient), basic_subsidy = values(basic_subsidy), is_probation = values(is_probation), harvest_allowance = values(harvest_allowance) ,
  35. basic_salary = values(basic_salary), talking_time_reward = values(talking_time_reward) , personal_task = values(personal_task) , sumpremium = values(sumpremium),
  36. complex_coefficient = values(complex_coefficient), late_Leave_early = values(late_Leave_early), take_leave = values(take_leave), sick_leave = values(sick_leave),
  37. absenteeism = values(absenteeism), full_attendance = values(full_attendance), total_attendance = values(total_attendance), recommend_will = values(recommend_will),
  38. business_violation = values(business_violation), lmdi_performance = values(lmdi_performance), coulmn_id = values(coulmn_id), coulmn1 = values(coulmn1),
  39. coulmn2 = values(coulmn2), coulmn3 = values(coulmn3), coulmn4 = values(coulmn4), coulmn5 = values(coulmn5), coulmn6 = values(coulmn6),
  40. coulmn7 = values(coulmn7), coulmn8 = values(coulmn8), coulmn9 = values(coulmn9), oth_reward = values(oth_reward),
  41. performance = values(performance), kpi = values(kpi), oth_bonus = values(oth_bonus), total_wages = values(total_wages),
  42. created_name = values(created_name), created_date = values(created_date), created_id = values(created_id),
  43. profit_insurance_coefficient = values(profit_insurance_coefficient)
  44. </insert>

mybatis批量插入时判断是否存在 如果存在就忽略  

  1. INSERT INTO staff_salary(user_id, approval_date, user_name, id_card)
  2. SELECT '491', '2010-01-10', '高某' , '6404251111112227'
  3. FROM DUAL
  4. WHERE NOT EXISTS(SELECT id FROM staff_salary WHERE approval_date = '2020-01-10' AND id_card ='6404251111112227' )

 先试了试单个数据的增加判断 关键词

insert into 表名 (列名) 

      (注意这里没有value) select 插入的值  

             from dual

                 where not exists(查询的数据用来判断唯一的条件)

 

      放上mybatis代码   注意 separator="union all"

     

  1. <insert id="insertStaffSalaryList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
  2. insert into staff_salary (user_id, approval_date, user_name, id_card, group_name, join_date, leave_date, online_days,
  3. tb_sumpremium, xb_sumpremium, xb_sumnetpremium, zb_sumpremium, zb_sumnetpremium, incentive_coefficient, basic_subsidy,
  4. is_probation, harvest_allowance, basic_salary, talking_time_reward, personal_task, sumpremium, complex_coefficient,
  5. late_Leave_early, take_leave, sick_leave, absenteeism, full_attendance, total_attendance, recommend_will,
  6. business_violation, lmdi_performance, coulmn_id, coulmn1, coulmn2, coulmn3, coulmn4, coulmn5, coulmn6, coulmn7,
  7. coulmn8, coulmn9, oth_reward, performance, kpi, oth_bonus, total_wages, created_name, created_date, created_id)
  8. <foreach collection="list" item="item" index="index" separator="union all">
  9. ( select
  10. #{item.userId,jdbcType=INTEGER}, #{item.approvalDate,jdbcType=TIMESTAMP}, #{item.userName,jdbcType=VARCHAR},
  11. #{item.idCard,jdbcType=VARCHAR}, #{item.groupName,jdbcType=VARCHAR}, #{item.joinDate,jdbcType=TIMESTAMP},
  12. #{item.leaveDate,jdbcType=TIMESTAMP}, #{item.onlineDays,jdbcType=INTEGER} ,#{item.tbSumpremium,jdbcType=DECIMAL},
  13. #{item.xbSumpremium,jdbcType=DECIMAL}, #{item.xbSumnetpremium,jdbcType=DECIMAL}, #{item.zbSumpremium,jdbcType=DECIMAL},
  14. #{item.zbSumnetpremium,jdbcType=DECIMAL}, #{item.incentiveCoefficient,jdbcType=DECIMAL}, #{item.basicSubsidy,jdbcType=INTEGER},
  15. #{item.isProbation,jdbcType=VARCHAR}, #{item.harvestAllowance,jdbcType=INTEGER}, #{item.basicSalary,jdbcType=DECIMAL},
  16. #{item.talkingTimeReward,jdbcType=INTEGER}, #{item.personalTask,jdbcType=INTEGER}, #{item.sumpremium,jdbcType=INTEGER},
  17. #{item.complexCoefficient,jdbcType=INTEGER}, #{item.lateLeaveEarly,jdbcType=INTEGER}, #{item.takeLeave,jdbcType=INTEGER},
  18. #{item.sickLeave,jdbcType=INTEGER}, #{item.absenteeism,jdbcType=INTEGER}, #{item.fullAttendance,jdbcType=INTEGER},
  19. #{item.totalAttendance,jdbcType=INTEGER}, #{item.recommendWill,jdbcType=INTEGER},#{item.businessViolation,jdbcType=INTEGER},
  20. #{item.lmdiPerformance,jdbcType=INTEGER}, #{item.coulmnId,jdbcType=INTEGER}, #{item.coulmn1,jdbcType=VARCHAR},
  21. #{item.coulmn2,jdbcType=VARCHAR}, #{item.coulmn3,jdbcType=VARCHAR}, #{item.coulmn4,jdbcType=VARCHAR},
  22. #{item.coulmn5,jdbcType=VARCHAR}, #{item.coulmn6,jdbcType=VARCHAR}, #{item.coulmn7,jdbcType=VARCHAR},
  23. #{item.coulmn8,jdbcType=VARCHAR}, #{item.coulmn9,jdbcType=VARCHAR}, #{item.othReward,jdbcType=DECIMAL},
  24. #{item.performance,jdbcType=DECIMAL}, #{item.kpi,jdbcType=DECIMAL}, #{item.othBonus,jdbcType=DECIMAL}, #{item.totalWages,jdbcType=DECIMAL},
  25. #{item.createdName,jdbcType=VARCHAR}, #{item.createdDate,jdbcType=TIMESTAMP}, #{item.createdId,jdbcType=INTEGER}
  26. from DUAL where not exists (select id from staff_salary where approval_date = #{item.approvalDate,jdbcType=TIMESTAMP}
  27. and id_card = #{item.idCard,jdbcType=VARCHAR} )
  28. )
  29. </foreach>

        如果帮助到你了,帮忙点个赞吧~ 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/569353
推荐阅读
相关标签
  

闽ICP备14008679号