前言:mybatis自带两种对枚举类的处理方式,分别为EnumTypeHandler(默认使用)和EnumOrdinalTypeHandler, 可以使用这两种方式插入枚举对象的name值和序号值.但在实际的开发中我们常常希望存储的是枚举类中自定义的一个属性值(一般是一个int类型的value值),这种情况下我们可以通过以下方式来实现
一: 定义一个通用的泛型枚举接口
package org.power.quickbuy.enums;
public interface IntEnum<E extends Enum<E>> {
int getIntValue();
}
二:自定义一个Handler类并继承于BaseTypeHandler
package org.power.quickbuy.enums;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
public class IntEnumTypeHandler<E extends Enum<E> & IntEnum<E>> extends BaseTypeHandler<IntEnum> {
private Class<IntEnum> type;
public IntEnumTypeHandler(Class<IntEnum> type) {
if (type == null)
throw new IllegalArgumentException("Type argument cannot be null");
this.type = type;
}
private IntEnum convert(int status) {
IntEnum[] objs = type.getEnumConstants();
for (IntEnum em : objs) {
if (em.getIntValue() == status) {
return em;
}
}
return null;
}
@Override
public IntEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
return convert(rs.getInt(columnName));
}
@Override
public IntEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return convert(rs.getInt(columnIndex));
}
@Override
public IntEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return convert(cs.getInt(columnIndex));
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, IntEnum enumObj, JdbcType jdbcType)
throws SQLException {
// baseTypeHandler已经帮我们做了parameter的null判断
ps.setInt(i, enumObj.getIntValue());
}
}
三:创建自己的枚举类并实现IntEnum接口
package org.power.quickbuy.enums;
public enum EmpStatus implements IntEnum<EmpStatus> {
ON_LINE("在线", 1), OFF_LINE("下线", 0), AWAY("离开", 5);
private String tagName;
private Integer value;
private EmpStatus(String tagName, Integer value) {
this.tagName = tagName;
this.value = value;
}
public String getTagName() {
return tagName;
}
public Integer getValue() {
return value;
}
@Override
public int getIntValue() {
// 该处返回的值即为我们希望在数据库中存储的值
return this.value;
}
}
四: 在Mapper.xml文件的插入、查询等操作中对指定的枚举类型字段添加typeHandler处理
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.power.quickbuy.dao.EmpMapper">
<!--实体映射-->
<resultMap id="empResultMap" type="org.power.quickbuy.entity.Emp">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="status" column="status" typeHandler="org.power.quickbuy.enums.IntEnumTypeHandler" />
</resultMap>
<!-- 查询(根据主键ID查询) -->
<select id="selectByPrimaryKey" resultMap="empResultMap" parameterType="java.lang.Integer">
SELECT
id,
name,
status
FROM emp
WHERE id = #{id}
</select>
<!-- 添加 -->
<insert id="insert" parameterType="emp">
INSERT IGNORE INTO emp(
<include refid="Base_Column_List" />
)VALUES(
#{id},
#{name},
#{status, typeHandler=org.power.quickbuy.enums.IntEnumTypeHandler}
)
</insert>
</mapper>