SoFunction
Updated on 2025-03-09

How to get the self-increment id in mysql

When JAVA inserts a piece of data of the self-increment primary key, how can I get the self-increment primary key id of the self-increment primary key at the same time?

Business scenarios

Development business scenario: In the student management system business scenario, for example, Xiao Wu, a new student transferred to the class, participated in the school's basic examination, then the system administrator needs to update his information to the student table and also needs to bind a relationship with the course table (a relationship table needs to be established in many-to-many relationships).

The grade table (main fields: student_id, source_id, score…) determines the uniqueness of a data through the student id and the course id.

Therefore, when adding a new student, you also need to add the relevant relationship table (which courses did Xiao Wu learn).

Examples of actual development

@Service
public class GroupServiceImpl implements IGroupService
{
    private static final Logger log = ();

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private IGroupUserService groupUserService;
/*
 Business scenarios: group (group table), user (user table), group user relationship table (group_user)
 Add a new group. There can be multiple members under the group. One member can create multiple groups. There is a many-to-many relationship between them. When adding a new group, the relationship table must also bind this add record.  So there are two insert operations here.  A new group must be created by the current user.  Therefore, when creating a new group, you have to fill in the default record in the group_user relationship.
 */
    @Override
    public int insertGroupInfo(Group group) {
        (group);//Initialize some fields        (group);//Add grouping        
        .  (newGroupUser(0,(),().getUser().getUserId().intValue(),().getUser().getRoleId().intValue(),Constants.TEAM_ROLE.LEADER));//A record of the group membership table is added by default (create the grouper)        return 1;
    }
}

Code implementation

Here we will use mysql technology, in the file,

Add the following properties in the insert code location: useGeneratedKeys="true" and keyProperty="id", the id here is the self-incremented primary key field you set

<insert  parameterType="" useGeneratedKeys="true" keyProperty="id">
    insert into student (id, name,sex,age)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{age,jdbcType=VARCHAR}
  </insert>

After adding attributes to the mapper mapping file, you can directly obtain the id in the service layer

Student student=new Student();
("Xiao Wu");
(student);//Add students(new Score((),,0));//Add grade entity,The default score is0point

Summarize

Get the value of the currently added data field's self-increment id,

Using mysql technology, add key attributes when insert operation:

  • 1. useGeneratedKeys=“true”
  • 2. keyProperty=“id”
  • 3. Get the id of the currently added entity directly at the service layer.
  • 4. mybatisplus integrates the useGeneratedKeys="true" and keyProperty="id" properties by default, which can be obtained directly through getId().

The above is personal experience. I hope you can give you a reference and I hope you can support me more.