好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决MyBatis中Enum字段参数解析问题

基础Class和TypeHandler

MyBatis操作的基本User对象结构如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@Data

@Alias (value = "user" )

public class User implements Serializable {

     private static final long serialVersionUID = -4947062488310146862L;

 

     private Long id;

 

     @NotNull (message = "用户名不能为空" )

     private String userName;

 

     @NotNull (message = "备注不能为空" )

     private String note;

 

     @NotNull (message = "性别不能为空" )

     private SexEnum sex;

}

其中sex字段对应的类型为SexEnum枚举类型,因此同时设置了如下的TypeHandler,从而在前端传入参数和从数据库中取值时进行自动的名称转换。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

@MappedJdbcTypes (JdbcType.INTEGER)

@MappedTypes (value = SexEnum. class )

public class SexTypeHandler extends BaseTypeHandler<SexEnum> {

     /**

      * 设置非空性别参数

      */

     @Override

     public void setNonNullParameter(PreparedStatement preparedStatement, int i, SexEnum sexEnum, JdbcType jdbcType) throws SQLException {

         preparedStatement.setInt(i, sexEnum.getId());

     }

 

     /**

      * 通过列名读取性别

      */

     @Override

     public SexEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {

         int sex = resultSet.getInt(s);

         if (sex != 1 && sex != 2 ) {

             return null ;

         }

         return SexEnum.getEnumById(sex);

     }

 

     /**

      * 通过下标读取性别

      */

     @Override

     public SexEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {

         int sex = resultSet.getInt(i);

         if (sex != 1 && sex != 2 ) {

             return null ;

         }

         return SexEnum.getEnumById(sex);

     }

 

     /**

      * 通过存储过程读取性别

      */

     @Override

     public SexEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {

         int sex = callableStatement.getInt(i);

         if (sex != 1 && sex != 2 ) {

             return null ;

         }

         return SexEnum.getEnumById(sex);

     }

}

请求参数解析问题

下面在使用axios post请求来更新用户信息,请求的JSON参数如下:

?

1

2

3

4

5

6

{

       id: id,

       userName: username,

       sex: sex === 'MALE' ? 1 : 2, // 1: 男,2: 女

       note: note

}

其中由于sex字段的枚举类型,因此这里将sex根据select得到的option来转换为了枚举中的id对应的值。也就是:

z

1: MALE
2: FAMALE

但在发出请求之后,服务端日志中出现如下的问题:

2020-03-02 22:59:50.722  WARN 10864 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `cn.zyt.springbootlearning.domain.SexEnum` from number 2: index value outside legal index range [0..1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `cn.zyt.springbootlearning.domain.SexEnum` from number 2: index value outside legal index range [0..1]
 at [Source: (PushbackInputStream); line: 1, column: 40] (through reference chain: cn.zyt.springbootlearning.domain.User["sex"])]

问题解决

对于该问题,可以使用枚举类型的desc来作为参数传递。当使用如下desc属性映射时,将JSON请求参数改成如下就可以解析成功不报错:

?

1

2

3

4

5

6

{

       id: id,

       userName: username,

       sex: sex,

       note: note

}

此时对应的sex字段选择select标签如下:

?

1

2

3

4

5

6

7

< tr >

   < td >sex:</ td >

   < td >< select name = "sex" value={sex} onChange={this.handleChange}>

     < option value = "MALE" >MALE</ option >

     < option value = "FEMALE" >FEMALE</ option >

   </ select ></ td >

</ tr >

同时注意:enum字段sex对应的数据库列的设置中,该列的数据类型为int,而不能为tinyint。tinyint数据类型对应于java中的boolean,1表示true,0表示false。

到此这篇关于解决MyBatis中Enum字段参数解析问题的文章就介绍到这了,更多相关MyBatis Enum字段参数解析内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/yitian_z/article/details/104627772

查看更多关于解决MyBatis中Enum字段参数解析问题的详细内容...

  阅读:16次