MyBatis注解插入数据,返回自增主键

使用Mybatis框架操作数据库时,可以使用注解的方式,也可以使用XML文件配置,两种写法各有千秋。
在使用注解进行save操作时,如果我想获取插入数据后的自增主键,那么可以使用如下注释:

@Insert("insert into `product_module_tree` (`product_id`, `module_name`, `level`, `parent_id`, `status`, `sort`) " +
        "values(#{productId},#{moduleName},#{level},#{parentId},#{status},#{sort}")
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
void saveModel(ProductModuleTree model);

这样在实体中就会自动插入自增ID。

posted @ 2021-10-28 16:19  梨猫南北  阅读(541)  评论(0编辑  收藏  举报