Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the self-incrementing column id by Mybatis-Plus

Shulou Source: shulou.com Published: 2022-06-02 02:26:09 10月01日 Update

Today, I would like to share with you how Mybatis-Plus obtains the relevant knowledge points of self-adding id, with detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Added to get self-incrementing id1 and entity class definition

Note: the type = IdType.AUTO attribute in the @ TableId (value = "id", type = IdType.AUTO) annotation marks the primary key as a self-increment policy.

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, solution

Method 1:

Use the insert method that comes with the framework.

Int insert (T entity)

Method 2:

@ Insert ("insert into users (`name`) values (# {user.name})") @ Options (useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") Integer add (@ Param ("user") User user)

Method 3:

@ InsertProvider (type = UserMapperProvider.class, method = "add") @ Options (useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") Integer add (@ Param ("user") User user)

UserMapperProvider class

Public class UserMapperProvider {public String add (User user) {return "insert into users (id, `name`) values (# {user.id}, # {user.name})";}} 3. Call the method to get the id description

Before calling the method:

After calling the method:

The method of solving the self-increasing id

Join id in the pojo file

@ TableId (value = "id", type = IdType.AUTO)

Add to application.yml:

Global-config: db-config: id-type: auto above is all the content of the article "how to get self-adding id by Mybatis-Plus". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Methods knowledge articles content different big methods most entities that is attributes documents more frameworks annotations knowledge points strategies industries information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Shulou Information macOS Linux