springboot + mybatis 自定義枚舉類型轉換


springboot+mybatis

https://www.cnblogs.com/jackspan/p/10582948.html

根據我的實際項目測試,與鏈接中的兩種方法為有小差異

我的配置為:

第一種方法:

a)在application.properties文件新增配置

mybatis.type-handlers-package=com.xiao.mywebproject.dao.typehandler

這是自己寫的handler包

 

b)在自己寫的handler類上面加注解

@MappedTypes(OrderStatusEnum.class)

OrderStatusEnum是自己寫的枚舉類

 

第二種方法:

刪除第一種方法的配置

a) mapper的xml文件里,resultMap相應的字段,增加

typeHandler= com.xiao.mywebproject.dao.typehandler. EnumTypeHandler

(這是主要用於select取值轉換)

inset和update語句同樣增加typehandler,如下

1 <insert id="insertTest" parameterType="Order" >
2     insert into order_info (id, order_status)
3     values (#{id, jdbcType=BIGINT}, #{orderStatus, jdbcType=TINYINT,typeHandler=com.xiao.mywebproject.dao.typehandler.EnumTypeHandler})
4 </insert>

 

代碼demo:

1、 handler類,繼承了BaseTypeHandler

 1 @MappedTypes(OrderStatusEnum.class)
 2 public class EnumTypeHandler<E extends BaseEnum<E>> extends BaseTypeHandler<E> {
 3 
 4     private final Map<Integer, E> enumMap;
 5     private Class<E> type;
 6 
 7     public EnumTypeHandler(Class<E> type) {
 8         if (type == null) {
 9             throw new IllegalArgumentException("Type argument cannot be null");
10         }
11         this.type = type;
12         this.enumMap = type.getEnumConstants()[0].getEnumMap();
13         if (this.enumMap == null) {
14             throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
15         }
16     }
17 
18     @Override
19     public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
20         ps.setInt(i, parameter.getCode());
21     }
22 
23     @Override
24     public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
25         int i = rs.getInt(columnName);
26         if (rs.wasNull()) {
27             return null;
28         } else {
29             return getEnum(i);
30         }
31     }
32 
33     @Override
34     public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
35         int i = rs.getInt(columnIndex);
36         if (rs.wasNull()) {
37             return null;
38         } else {
39             return getEnum(i);
40         }
41     }
42 
43     @Override
44     public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
45         int i = cs.getInt(columnIndex);
46         if (cs.wasNull()) {
47             return null;
48         } else {
49             return getEnum(i);
50         }
51     }
52 
53     private E getEnum(int i) {
54         if (enumMap.containsKey(i)) {
55             return enumMap.get(i);
56         } else {
57             throw new IllegalArgumentException("Cannot convertor " + i + " to " + type.getSimpleName() + " by value.");
58         }
59     }
60 
61 }

 

2、BaseEnum

1 public interface BaseEnum<E> {
2     int getCode();
3 
4     Map<Integer, E> getEnumMap()
5 }

 

3、 OrderStatusEnum

 1 public enum  OrderStatusEnum implements BaseEnum{
 2     WATI_PAY(1,"待付款"),
 3     PAYED(2,"付款成功"),
 4     WAIT_REFUND(3,"等待退款"),
 5     REFUNDING(4,"退款中"),
 6     REFUND_SUCCESS(5,"退款成功"),
 7     REFUND_FAIL(6,"退款失敗"),
 8     COMPLETED(7,"訂單完成");
 9 
10     private int code;
11     private String desc;
12     public static Map<Integer,OrderStatusEnum> enumMap = new HashMap<>();
13     static{
14         for(OrderStatusEnum temp : OrderStatusEnum.values()){
15             enumMap.put(temp.getCode(),temp); }
16     }
17 
18     OrderStatusEnum(int code, String desc) {
19         this.code = code;
20         this.desc = desc;
21     }
22 
23 
24     @Override
25     public int getCode() {
26         return code;
27     }
28 
29     public void setCode(int code) {
30         this.code = code;
31     }
32 
33     public String getDesc() {
34         return desc;
35     }
36 
37     public void setDesc(String desc) {
38         this.desc = desc;
39     }
40 
41     @Override
42     public Map getEnumMap() {
43         return enumMap;
44     }
45 }

 

配置:在application.properties

#數據庫連接
spring.datasource.url=jdbc:mysql://localhost:3306/myweb_project
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#掃描mapper xml
mybatis.mapper-locations=classpath:mapper/*.xml
#簡化mapper  xml文件中類的寫法,使在xml里不需要全限定名
mybatis.type-aliases-package=com.xiao.mywebproject.dao.domain
#typehandler配置,與實際handler類的@MappedTypes注解聯合使用,即指定了數據插入和查詢時的typehandler
mybatis.type-handlers-package=com.xiao.mywebproject.dao.typehandler

(這是第二種配置方法)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM