MyBatis-Plus 枚举类型处理

在学习mybatis-plus的时候,学到插入枚举数据类型的时候,发现按照老师的要求写,在需要添加进数据库的枚举类属性上加上@EnumValue注解,然后进行测试,数据库资料如下:

这里的字段sex在实体类中对应的属性是SexEnum,字段sex在设计表时使用的是Integer

public enum SexEnum {
          
   
    MALE(1,"男"),
    FEMALE(2,"女");
    @EnumValue
    private Integer sex;
    private String sexName;

    SexEnum(Integer sex, String sexName) {
          
   
        this.sex = sex;
        this.sexName = sexName;
    }

    public Integer getSex() {
          
   
        return sex;
    }

    public String getSexName() {
          
   
        return sexName;
    }
}

这里可以看到用数字1和2来表示男女,在这里用@EnumValue注解在Integer sex属性表示要插入到数据值的值;当进行测试的时候,报了以下错误:

@Resource
    private StudentMapper studentMapper;
    @Test
    public void test01(){
          
   
        Student student = new Student();
        student.setName("王八");
        student.setAge(22);
        student.setSexEnum(SexEnum.FEMALE);
        int i = studentMapper.insert(student);
        System.out.println("受影响的行数"+i);
    }
JDBC Connection [HikariProxyConnection@1752402342 wrapping com.mysql.cj.jdbc.ConnectionImpl@8641b7d] will not be managed by Spring
==>  Preparing: INSERT INTO t_student ( name, age, sex ) VALUES ( ?, ?, ? )
==> Parameters: 王八(String), 22(Integer), FEMALE(String)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d464510]

org.springframework.jdbc.UncategorizedSQLException: 
### Error updating database.  Cause: java.sql.SQLException: Incorrect integer value: FEMALE for column sex at row 1
### The error may exist in com/aliang/mapper/StudentMapper.java (best guess)
### The error may involve com.aliang.mapper.StudentMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO t_student  ( name, age, sex )  VALUES  ( ?, ?, ? )
### Cause: java.sql.SQLException: Incorrect integer value: FEMALE for column sex at row 1
; uncategorized SQLException; SQL state [HY000]; error code [1366]; Incorrect integer value: FEMALE for column sex at row 1; nested exception is java.sql.SQLException: Incorrect integer value: FEMALE for column sex at row 1

按照报错信息可以知道,在插入枚举类SexEnum的时候出现了问题,已经使用了@EnumValue注解表示了要插入到数据,为什么还会报错呢?翻阅了以下mybatis-plus官方文档,需要在application.yml文件中进行设置

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
    username: root
    password: ln123456
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
  type-enums-package: com.aliang.enums

在配置了default-enum-type-handler和type-enums-package之后进行测试

==>  Preparing: INSERT INTO t_student ( name, age, sex ) VALUES ( ?, ?, ? )
==> Parameters: 王八(String), 22(Integer), 2(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4f5506]
受影响的行数1

查看一下数据库 插入成功。 在pom.xml文件中添加的mybatis-plus依赖为

<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.5.1</version>
</dependency>

在mybatis-plus提供的官方文档中可以看到在不同的版本中所需的操作是有所区别的 官方文档地址:

经验分享 程序员 微信小程序 职场和发展