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

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

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

  insert into counterparty()
  values 
 
  (
  #{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}
  )
 

  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"

 


这样就OK了。

你可能感兴趣的:(java,mybatis,mysql)