Mysql迁移Kingbase,使用tk.mybatis进行保存数据时遇到的id插入问题

目录

前言

问题1:实体类id字段上使用了注解@GeneratedValue,插入数据时显示非空异常

问题2:在插入数据时,不返回保存时的id


前言

在Mysql迁移Kingbase时,再插入数据的时候遇到了一些关于id方面的问题,进行解决

问题1:实体类id字段上使用了注解@GeneratedValue,插入数据时显示非空异常

        id上的注解@GeneratedValue会在插入数据时,会自动赋予id null值,mysql支持插入时id为null值,kingbase不支持,所以只要删掉这个注解即可,在数据库迁移的时候会自动设置id为主键,不需要使用该注解

问题2:在插入数据时,不返回保存时的id

        在保存数据时,如果需要获取保存时的这个实体类的返回的id值,需要在继承的通用mapper文件中对tk.mybatis对插入数据的方法进行重写,添加两个新的注解@InsertProvider和@Options即可

public interface InsertDAO<T> extends Marker,
        InsertMapper<T>,
    InsertSelectiveMapper<T>,
    MySqlMapper<T> {
    @Override
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(T record);

    @Override
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertList(List<? extends T> recordList);

    @Override
    @InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertSelective(T record);
}

猜你喜欢

转载自blog.csdn.net/GuaGea/article/details/131307020