springboot+mybatis
根据我的实际项目测试,与链接中的两种方法为有小差异
我的配置为:
第一种方法:
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
2 insert into order_info (id, order_status)3 values (#{id, jdbcType=BIGINT}, #{orderStatus, jdbcType=TINYINT,typeHandler=com.xiao.mywebproject.dao.typehandler.EnumTypeHandler})4
代码demo:
1、 handler类,继承了BaseTypeHandler
1 @MappedTypes(OrderStatusEnum.class)2 public class EnumTypeHandler> extends BaseTypeHandler{3
4 private final MapenumMap;5 private Classtype;6
7 public EnumTypeHandler(Classtype) {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 @Override19 public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throwsSQLException {20 ps.setInt(i, parameter.getCode());21 }22
23 @Override24 public E getNullableResult(ResultSet rs, String columnName) throwsSQLException {25 int i =rs.getInt(columnName);26 if(rs.wasNull()) {27 return null;28 } else{29 returngetEnum(i);30 }31 }32
33 @Override34 public E getNullableResult(ResultSet rs, int columnIndex) throwsSQLException {35 int i =rs.getInt(columnIndex);36 if(rs.wasNull()) {37 return null;38 } else{39 returngetEnum(i);40 }41 }42
43 @Override44 public E getNullableResult(CallableStatement cs, int columnIndex) throwsSQLException {45 int i =cs.getInt(columnIndex);46 if(cs.wasNull()) {47 return null;48 } else{49 returngetEnum(i);50 }51 }52
53 private E getEnum(inti) {54 if(enumMap.containsKey(i)) {55 returnenumMap.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{2 intgetCode();3
4 MapgetEnumMap()5 }
3、 OrderStatusEnum
1 public enum OrderStatusEnum implementsBaseEnum{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 intcode;11 privateString desc;12 public static Map enumMap = new HashMap<>();13 static{14 for(OrderStatusEnum temp : OrderStatusEnum.values()){15 enumMap.put(temp.getCode(),temp); }16 }17
18 OrderStatusEnum(intcode, String desc) {19 this.code =code;20 this.desc =desc;21 }22
23
24 @Override25 public intgetCode() {26 returncode;27 }28
29 public void setCode(intcode) {30 this.code =code;31 }32
33 publicString getDesc() {34 returndesc;35 }36
37 public voidsetDesc(String desc) {38 this.desc =desc;39 }40
41 @Override42 publicMap getEnumMap() {43 returnenumMap;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
(这是第二种配置方法)