当前使用版本(3.2.0)

  • mysql saveOrUpdateBatch没效果,,我设置了某个字段为唯一索引,,插入不会走更新,,而是走插入,,导致直接报Duplicate entry

  • 我所知道的mysql insert or update语句是根据唯一索引或主键判断的。。但是这个根本不会,,如果根据主键值去判断,,意义不大。。我新入库的数据主键值都没有(但我设置了某个字段唯一索引),如果新生成主键,那怎么可能出现插入或更新。

Comment From: miemieYaho

就这样的

Comment From: 15klli

@miemieYaho 其实不是可以加多一个注解,注明某一列是 建有唯一索引的,然后在

public boolean saveOrUpdate(T entity) {
        if (null == entity) {
            return false;
        } else {
            Class<?> cls = entity.getClass();
            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
            Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
            return !StringUtils.checkValNull(idVal) && !Objects.isNull(this.getById((Serializable)idVal)) ? this.updateById(entity) : this.save(entity); //在这里加多一个判断不就可以实现了吗
        }
    }