mybatis批量新增,存在就更新(mysql數據庫)

只需要把要批量新增的實體類放到集合中,作爲參數傳給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了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章