mybatis处理枚举类型

前言: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>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值