温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在mybatis中处理枚举类

发布时间:2021-05-19 16:01:00 来源:亿速云 阅读:433 作者:Leah 栏目:编程语言

这篇文章给大家介绍怎么在mybatis中处理枚举类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

mybatis自带对枚举的处理类

org.apache.ibatis.type.EnumOrdinalTypeHandler<E> :该类实现了枚举类型和Integer类型的相互转换。
但是给转换仅仅是将对应的枚举转换为其索引位置,也就是"ordinal()"方法获取到的值。对应自定义的int值,该类无能为力。

org.apache.ibatis.type.EnumTypeHandler<E> :该类实现了枚举类型和String类型的相互转换。
对于想将枚举在数据库中存储为对应的int值的情况,该类没办法实现。

基于以上mybatis提供的两个枚举处理类的能力有限,因此只能自己定义对枚举的转换了。

自定义mybatis的枚举处理类EnumValueTypeHandler

该类需要继承org.apache.ibatis.type.BaseTypeHandler<E> ,然后在重定义的方法中实现自有逻辑。

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.MappedTypes;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;


 //在 xml 中添加该 TypeHandler 时需要使用该注解
@MappedTypes(value = {
  QcListTypeEnum.class,
  SellingQcBizTypeEnum.class
})
public class EnumValueTypeHandler<E extends EsnBaseEnum> extends BaseTypeHandler<E> {

 private Class<E> type;

 private final E[] enums;

 public EnumValueTypeHandler(Class<E> type) {
  if (type == null) {
   throw new IllegalArgumentException("Type argument cannot be null");
  }
  this.type = type;
  this.enums = type.getEnumConstants();
  if (this.enums == null) {
   throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
  }
 }

 @Override
 public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
  //获取非空的枚举的int值并设置到statement中
  ps.setInt(i, parameter.getValue());
 }

 @Override
 public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
  int i = rs.getInt(columnName);
  if (rs.wasNull()) {
   return null;
  } else {
   try {
    return getEnumByValue(i);
   } catch (Exception ex) {
    throw new IllegalArgumentException(
      "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
   }
  }
 }

 /**
  * 通过枚举类型的int值,获取到对应的枚举类型
  * @author jingzz
  * @param i
  */
 protected E getEnumByValue(int i) {
  for (E e : enums) {
   if (e.getValue() == i) {
    return e;
   }
  }
  return null;
 }

 @Override
 public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  int i = rs.getInt(columnIndex);
  if (rs.wasNull()) {
   return null;
  } else {
   try {
    return getEnumByValue(i);
   } catch (Exception ex) {
    throw new IllegalArgumentException(
      "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
   }
  }
 }

 @Override
 public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  int i = cs.getInt(columnIndex);
  if (cs.wasNull()) {
   return null;
  } else {
   try {
    return getEnumByValue(i);
   } catch (Exception ex) {
    throw new IllegalArgumentException(
      "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
   }
  }
 }

}

该处理器是处理继承了EsnBaseEnum接口的枚举类,因为该接口中定义了获取自定义int值的方法。

如果在 mybatis 的 xml 中配置 该 typehandler,则需要添加注解@MappedTypes。在添加 typeHandler 注册时使用具体的实现类注册。

配置文件如下:

<typeAliases>
<!-- 为自定义的 TypeHandler 指定别名,在 sql 的 xml 中即可使用别名访问了 -->
  <typeAlias type="cn.followtry.mybatis.EnumValueTypeHandler" alias="enumValueTypeHandler"/>
</typeAliases>
<typeHandlers>
  <!--处理枚举的值转换-->
  <typeHandler handler="cn.followtry.mybatis.EnumValueTypeHandler"/>
</typeHandlers>

自定义的 Enum 需要实现的接口

public interface EsnBaseEnum {
  
  public int getValue();
}

而实现了EsnBaseEnum的枚举示例如下:

public enum SyncType implements EsnBaseEnum {
  
  DEPT(3),//部门
  PERSON(1);//人员
  
  private int value;
  
  private SyncType(int value) {
    
    this.value = value;
  }
  
  @Override
  public int getValue(){
    return this.value;
  }
}

只有在实现了EsnBaseEnum的接口,EnumValueTypeHandler才能通过接口的getValue方法获取到对应枚举的值。

到此,对于枚举的简单处理逻辑已经实现完成了,接下来就是如何配置来使用该自定义枚举处理逻辑

配置对枚举的处理

首先,mybatis中对于处理逻辑的设置是在sql的映射文件中,如EsnSyncLogMapper.xml。

关键的设置枚举处理的位置如下:

<resultMap id="BaseResultMap" type="com.test.dao.model.EsnSyncLog" >
  <id column="id" property="id" jdbcType="INTEGER" />
  <result column="sync_time" property="syncTime" jdbcType="TIMESTAMP" />
  <result column="total_num" property="totalNum" jdbcType="INTEGER" />
  <result column="success_total_num" property="successTotalNum" jdbcType="INTEGER" />
  <result column="type" property="type" typeHandler="com.test.common.EnumValueTypeHandler" />
  <result column="msg" property="msg" jdbcType="VARCHAR" />
 </resultMap>

其中type列设置了属性typeHandler,其值为自定义的枚举处理逻辑。

而在具体sql中也需要使用typeHandler属性,如:

 <if test="type != null" >
  #{type, typeHandler=com.test.common.EnumValueTypeHandler},
 </if>

在mybatis处理到该位置时,就会调用typeHandler指定的处理类来处理枚举类型。

关于怎么在mybatis中处理枚举类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI