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);

}