0

我正在使用spring boot(带有捆绑的 jackson 和 lombok)、mybatismybatis-plus

我有一个User带有枚举列的模型status

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public enum UserStatusEnum {
  ACTIVE(1, "active"),
  INACTIVE(0, "inactive");

  @EnumValue
  private Integer dbValue;
  @JsonValue
  private String jsonValue;
}

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "users")
public class User {
  Integer id;
  UserStatusEnum status = UserStatusEnum.ACTIVE;
  String username;
  @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
  LocalDateTime createdAt = LocalDateTime.now();
}

数据库模式就像

CREATE TABLE IF NOT EXISTS users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    status TINYINT NOT NULL,
    username VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
)  ENGINE=INNODB;
CREATE UNIQUE INDEX users_username_unique ON users (username);
INSERT INTO users (status, username) VALUES (1, "test")

我想在json中获取一个字符串状态的用户,{"id":1,"status":"active","username":"test","createdAt":"2021-12-08T00:00:00.000Z"}并将status其存储在数据库中为TINYINT1。我怎样才能得到它?

我正在使用映射器:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {}

并拨打电话:

@Component
public class UserService {
  @Autowired
  UserMapper userMapper;

  public User findUser(Integer id) {
    return userMapper.selectById(id);
  }
}

现在我会有这样的错误:

Servlet.service() for servlet [dispatcherServlet] in context with path []
  threw exception [Request processing failed; nested exception is
  org.mybatis.spring.MyBatisSystemException: nested exception is
  org.apache.ibatis.executor.result.ResultMapException:
  Error attempting to get column 'status' from result set.
  Cause: java.lang.IllegalArgumentException:
  No enum constant app.model.user.UserStatusEnum.1] with root cause

java.lang.IllegalArgumentException: No enum constant app.model.user.UserStatusEnum.1
4

0 回答 0