Mysql migrates Kingbase and uses tk.mybatis to save data when encountering id insertion problems

Table of contents

foreword

Question 1: The annotation @GeneratedValue is used on the id field of the entity class, and a non-null exception is displayed when inserting data

Question 2: When inserting data, the id at the time of saving is not returned


foreword

When Mysql migrated Kingbase, I encountered some id problems when inserting data, and solved them

Question 1: The annotation @GeneratedValue is used on the id field of the entity class, and a non-null exception is displayed when inserting data

        The annotation @GeneratedValue on the id will automatically give the id a null value when inserting data. Mysql supports the id as a null value when inserting, but kingbase does not support it, so just delete this annotation, and the id will be automatically set when the database is migrated Primary key, no need to use this annotation

Question 2: When inserting data, the id at the time of saving is not returned

        When saving data, if you need to obtain the returned id value of this entity class when saving, you need to rewrite the method of inserting data in tk.mybatis in the inherited general mapper file, and add two new annotations @InsertProvider and @Options can

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

Guess you like

Origin blog.csdn.net/GuaGea/article/details/131307020