Mybatis - 存在即更新、存在即不更新

  1. Mysql表中列设置unique属性
  2. xml中的写法:

例子1:存在即更新为新数据(除了UserId 自增加1)


    insert into yjj_ExpressInfo(UserId, OrderId, OrderItemGroupId, ExpressSupplier, ExpressOrderNo, ExpressUpdateTime, CreateTime, UpdateTime, OrderNo)
    values(#{userId}, #{orderId}, #{orderItemGroupId}, #{expressSupplier}, #{expressOrderNo}, #{expressUpdateTime}, #{createTime}, #{updateTime}, #{orderNo})

    ON DUPLICATE KEY UPDATE UserId = UserId + 1, Status = 0, OrderItemGroupId = VALUES(OrderItemGroupId), ExpressOrderNo = VALUES(ExpressOrderNo), ExpressSupplier = VALUES(ExpressSupplier), UpdateTime=UNIX_TIMESTAMP(now())*1000

例子二:存在即不更新

    "addVisitHistory" parameterType="map">
        insert ignore into yjj_UserVisitHistory (UserId, RelatedId, CreateTime)
        values
            "ids" item="id" open="(" close=")" separator="), (">
                #{userId}, #{id}, #{time}
            
    

你可能感兴趣的:(mybatis)