mybatis中的一个坑:预期:传入的Record中会生动填入在db中生成的id值

使用  insertUseGeneratedKeys插入数据时,如果id字段不是AUTO_INCREMENT,则不会生成新的id

package tk.mybatis.mapper.common.special;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.provider.SpecialProvider;

public interface InsertUseGeneratedKeysMapper<T> {
    @Options(
        useGeneratedKeys = true,
        keyProperty = "id"
    )
    @InsertProvider(
        type = SpecialProvider.class,
        method = "dynamicSQL"
    )
    int insertUseGeneratedKeys(T var1);
}
package tk.mybatis.mapper.common.special;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.provider.SpecialProvider;

/**
 * 通用Mapper接口,特殊方法,批量插入,支持批量插入的数据库都可以使用,例如mysql,h2等
 *
 * @param <T> 不能为空
 * @author liuzh
 */
public interface InsertUseGeneratedKeysMapper<T> {

    /**
     * 插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效
     *
     * @param record
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
    int insertUseGeneratedKeys(T record);

}

 

相关文章:

  • 2021-04-15
  • 2021-04-23
  • 2021-06-29
  • 2021-06-12
  • 2021-08-19
  • 2021-08-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
  • 2022-03-05
  • 2021-11-14
相关资源
相似解决方案