“mybatis retrieves the automatically generated primary key”

In MyBatis, you can use the parameter useGeneratedKeys to fetch the automatically generated primary key.

Firstly, set useGeneratedKeys to true in the insert statement, and specify keyProperty to indicate the attribute name that will receive the primary key. For example:

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
  INSERT INTO user (username, password) VALUES (#{username}, #{password})
</insert>

Next, in the corresponding Mapper interface method, define a parameter containing the primary key attribute, and MyBatis will automatically assign the generated primary key value to that parameter. For example:

public interface UserMapper {
  void insertUser(User user);
}
User user = new User();
user.setUsername("test");
user.setPassword("123456");
userMapper.insertUser(user);

// 获取自动生成的主键值
Long id = user.getId();

With the above configuration and code, MyBatis will automatically assign the generated primary key value to the id attribute, which can be retrieved using user.getId().

bannerAds