I hava a record class as java bean:
@With
@TableName(value = "user")
public record User(
@TableId(value = "uid", type = IdType.AUTO)
Integer id,
String username,
String password,
) {}
then I use mybatis-plus to manage it:
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public CommonResponse<Object> register(User user) {
String password = "9ABB136AA0B984E8";
User newUser = user.withPassword(password)
int rows = userMapper.insert(newUser); // got a MyBatisSystemException
if (rows == 0) {
return CommonResponse.createResponseForError();
}
return CommonResponse.createResponseForSuccess();
}
}
The UserMapper is the default implementation of mybatis-plus
public interface UserMapper extends BaseMapper<User> {}
When I execute the INSERT operation on it, the code reports an error:
Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'com.csu.mypetstore.api.domain.User'.
I know that the cause of this error is that the record class cannot perform setter operations, but I don't know how to get around this error and implement self-incrementing operations on the database.
I have tried adding the setId method to the User record class, but it doesn't work.
@With
@TableName(value = "user")
public record User(
@TableId(value = "uid", type = IdType.AUTO)
Integer id,
String username,
String password,
) {
public User setId(Integer id){
return new User(id, username, password); // No use
}
}
What can I do to accomplish mybatis-plus operations on the record class, such as completing an insert operation on a table with a self-incrementing primary key, or does the record class not apply to mybatis-plus?