Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report