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
(这是第二种配置方法)