Mybatis-Plus 新增获取自增列id方式
作者:编程课堂
这篇文章主要介绍了Mybatis-Plus 新增获取自增列id方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
新增获取自增列id
1、实体类定义
注意:@TableId(value = “id”, type = IdType.AUTO)注解中的 type = IdType.AUTO 属性标注主键为自增策略。
import lombok.Data;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableField;
@Data
@TableName("users")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("`name`")
private String name;
}
2、解决办法
方法一:
使用框架自带的insert方法。
int insert(T entity);
方法二:
@Insert("insert into users(`name`) values(#{user.name})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
Integer add(@Param("user") User user);方法三:
@InsertProvider(type = UserMapperProvider.class, method = "add")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
Integer add(@Param("user") User user);UserMapperProvider类
public class UserMapperProvider {
public String add(User user) {
return "insert into users(id, `name`) values(#{user.id},#{user.name})";
}
}3、调用方法获取id说明
方法调用前:

方法调用后:

解决id自增方法
在pojo文件中id加入
@TableId(value = “id”,type = IdType.AUTO)

application.yml中加入:
global-config: db-config: id-type: auto
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
