好得很程序员自学网

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

springboot+mybatis+枚举处理器的实现

背景

在Spring boot项目开发中经常遇到需要使用枚举的场景,比如描述状态、性别、类型等相关字段。通常这些字段在数据库会以 tinyint 类型存在,比如:0:女性,1:男性;或者,0:设备掉线,1:设备在线。如果在系统中我们也以int的方式到处使用,后期在维护的时候对数字的理解会非常困难,所以通常这种字段我们一般采用枚举的方式在系统的流转,而在存储的时候我们使用数字的方式存储。

以上,因为数据库存储字段的类型和我们在系统中流转的类型不同,我们需要实现一个两者自动转换的功能,也就是标题中提到的枚举处理器。

现状

如果我们采用全数字的方式在系统中流转,最终系统代码就会变成这样:

?

1

2

3

4

5

6

if (status == 1 ){ // 时间一长,谁来告诉我1代表什么状态

     ...

} else if (status == 0 ){ // 0又代表什么状态

     ...

}

...

以上这种操作会造成代码的可读性非常低,非常需要依靠对其中数字的注释进行编码。

期望

希望类型性别、状态这类字段可以全部使用枚举类型,提高代码的可维护性。考虑到大部分我们使用枚举的时候都可以使用 ONLINE(1, "在线"), OFFLINE(0, "掉线") 这样的方式,可以总结出一个通用的枚举接口,后续类似的枚举类都实现这个接口。

实现

为了满足期望的功能,通过一个通用的枚举接口和mybatis的枚举处理器来实现业务系统中使用枚举类型和mysql中使用数值类型的功能。

通用的枚举接口

简单粗暴的命名这个通用枚举接口为 KeyValueEnum :

?

1

2

3

4

5

6

7

public interface KeyValueEnum {

 

     int getKey();

 

     String getValue();

 

}

基于该接口实现一个状态枚举类:

?

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

public enum StatusEnum implements KeyValueEnum {

 

     ONLINE( 1 , "在线" ),

    

     OFFLINE( 0 , "掉线" );

    

     private int key;

 

     private String value;

 

     StatusEnum( int key, String value){

         this .key = key;

         this .value = value;

     }

 

     @Override

     public int getKey() {

         return key;

     }

 

     @Override

     public String getValue() {

         return value;

     }

}

由此,所有类似的接口都可以用过 getKey() 和 getValue() 方法来获取相关数值。

mybaits枚举处理器

?

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

@MappedTypes (value = {Status. class , Sex. class }) // 每增加一种枚举类型,就在此添加。

public class EnumTypeHandler<E extends KeyValueEnum> extends BaseTypeHandler<E> {

 

     private Class<E> type;

     private E[] enums;

 

     public EnumTypeHandler(Class<E> type){

         this .type = type;

         this .enums = type.getEnumConstants();

     }

 

     @Override

     public void setNonNullParameter(PreparedStatement preparedStatement, int i, KeyValueEnum keyValueEnum, JdbcType jdbcType) throws SQLException {

         preparedStatement.setInt(i, keyValueEnum.getKey());

     }

 

     @Override

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

         int key = resultSet.getInt(i);

         return getEnum(key);

     }

 

     @Override

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

         int key = resultSet.getInt(s);

         return getEnum(key);

     }

 

     @Override

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

         int key = callableStatement.getInt(i);

         return getEnum(key);

     }

 

     private E getEnum( int key){

         for (E keyValue:

              enums) {

             if (keyValue.getKey() == key){

                 return keyValue;

             }

         }

         return null ;

     }

}

枚举处理器是通过继承mybatis自带的BaseTypeHandler抽象类实现的,其中包含的具体方法参考mybatis的BaseTypeHandler文档即可。

需要注意的是,如果每增加一种需要存储到数据库的枚举类型就需要在@MappedTypes(value = {Status.class, Sex.class})注解中添加一个对应的枚举类。

配置枚举处理器

?

1

2

3

4

5

# application.yaml

mybatis:

   configuration:

     map-underscore-to-camel-case: true # 下划线转驼峰

   type-handlers-package: xxx.xxx.xxx.handler.mybatisTypeHandler # 配置刚才编辑的枚举处理器

包含枚举类型字段的实体类

?

1

2

3

4

5

6

7

@Data

public class device {

 

     private long id;

 

     private Status status;

}

期望实现的目标是直接通过数据库查询出对应的枚举类型。

查询mapper

?

1

2

3

4

5

6

7

8

9

public interface deviceMapper {

 

     @Results (id= "aaa" , value = {

             @Result (id = true , column = "id" , property = "id" ),

             @Result (column = "status" , property = "status" )

     })

     @Select ( "select * from device where id = #{id}" )

     Device getDeviceById( long id);

}

通过mapper查询最终可以实现数据库 tinyint 类型查询后转换为实体类字段对应的枚举类型。

到此这篇关于springboot+mybatis+枚举处理器的实现的文章就介绍到这了,更多相关springboot mybatis 枚举处理器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7069567717962940424

查看更多关于springboot+mybatis+枚举处理器的实现的详细内容...

  阅读:20次