mybatis批量新增,存在就更新(mysql数据库)

来源:互联网 发布:python 给字典赋值 编辑:程序博客网 时间:2024/05/16 05:36

只需要把要批量新增的实体类放到集合中,作为参数传给dao,

最关键就是Mapper文件中,直接上代码:

<insert id="batchInsert" parameterType="java.util.List">

  insert into counterparty(<include refid="Base_Column_List"/>)
  values 
  <foreach collection="list" item="counterParty" index="index" separator=",">
  (
  #{counterParty.id,jdbcType=VARCHAR},
  #{counterParty.fullName,jdbcType=VARCHAR},
  #{counterParty.shortName,jdbcType=VARCHAR},
  #{counterParty.initialLimit,jdbcType=VARCHAR},
  #{counterParty.currencyName,jdbcType=VARCHAR},
  #{counterParty.initialLimitUsd,jdbcType=VARCHAR},
  #{counterParty.creditLimitAlertOne,jdbcType=VARCHAR},
  #{counterParty.creditLimitAlertTwo,jdbcType=VARCHAR},
  #{counterParty.creditLimitAlertThree,jdbcType=VARCHAR},
  #{counterParty.closingRunTime,jdbcType=VARCHAR},
  #{counterParty.operator,jdbcType=VARCHAR},
  #{counterParty.uptime,jdbcType=VARCHAR},
  #{counterParty.remark,jdbcType=VARCHAR}
  )
  </foreach>
  ON DUPLICATE KEY UPDATE 
  full_name = VALUES(full_name),
  short_name = VALUES(short_name),
  initial_limit = VALUES(initial_limit),
  currency = VALUES(currency),
  initial_limit_usd = VALUES(initial_limit_usd),
  credit_limit_alert_one = VALUES(credit_limit_alert_one),
  credit_limit_alert_two = VALUES(credit_limit_alert_two),
  credit_limit_alert_three = VALUES(credit_limit_alert_three),
  closing_run_time = VALUES(closing_run_time),
  operator = VALUES(operator),
  uptime = now(3),
  remark = "update"

  </insert>


这样就OK了。