15、处理枚举类型

官方介绍

若想映射枚举类型 Enum,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用。

比如说我们想存储取近似值时用到的舍入模式。默认情况下,MyBatis 会利用 EnumTypeHandler 来把 Enum 值转换成对应的名字。

注意 EnumTypeHandler 在某种意义上来说是比较特别的,其他的处理器只针对某个特定的类,而它不同,它会处理任意继承了 Enum 的类。
- 若需将Enum字段映射为字符串,则使用 EnumTypeHandler 。 (默认使用)

  • 若需将Enum字段映射为int数值,则使用 EnumOrdinalTypeHandler
    EnumOrdinalTypeHandler 局限性非常明显,其映射的数据直接使用枚举值的 ordinal 数值,因此与枚举值定义顺序紧耦合

EnumOrdinalTypeHandler的配置

<!-- mybatis-config.xml -->
<typeHandlers>
  <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/>
</typeHandlers>

项目配置

package com.lf.entity;

import com.lf.dict.Gender;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Date;
@Data
@ToString
@NoArgsConstructor
public class BlogPost {
    private String postid;

    private Gender userid;

    private Date postdate;

    private String postinfo;


}
<?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="com.lf.dao.BlogPostMapper">
  <resultMap id="BaseResultMap" type="com.lf.entity.BlogPost">
    <id column="postid" jdbcType="VARCHAR" property="postid" />
    <result column="userid"  property="userid" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
    <result column="postdate" jdbcType="DATE" property="postdate" />
    <result column="postinfo" jdbcType="VARCHAR" property="postinfo" />
  </resultMap>
  <sql id="Base_Column_List">
    postid, userid, postdate, postinfo
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from blog_post
    where postid = #{postid,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from blog_post
    where postid = #{postid,jdbcType=VARCHAR}
  </delete>

  <insert id="insertSelective" parameterType="com.lf.entity.BlogPost">
    insert into blog_post
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="postid != null">
        postid,
      </if>
      <if test="userid != null">
        userid,
      </if>
      <if test="postdate != null">
        postdate,
      </if>
      <if test="postinfo != null">
        postinfo,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="postid != null">
        #{postid,jdbcType=VARCHAR},
      </if>
      <if test="userid != null">
        #{userid,jdbcType=VARCHAR},
      </if>
      <if test="postdate != null">
        #{postdate,jdbcType=DATE},
      </if>
      <if test="postinfo != null">
        #{postinfo,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.lf.entity.BlogPost">
    update blog_post
    <set>
      <if test="userid != null">
        userid = #{userid,jdbcType=VARCHAR},
      </if>
      <if test="postdate != null">
        postdate = #{postdate,jdbcType=DATE},
      </if>
      <if test="postinfo != null">
        postinfo = #{postinfo,jdbcType=VARCHAR},
      </if>
    </set>
    where postid = #{postid,jdbcType=VARCHAR}
  </update>

</mapper>
package com.lf;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class App {
    public static void main(String[] args) throws IOException {
        String resouce = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resouce);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Object one = sqlSession.selectOne("com.lf.dao.BlogPostMapper.selectByPrimaryKey", "1");
        System.err.println(one);
//        Object one1 = sqlSession.selectOne("com.lf.dao.BlogMapper.selectByPrimaryKey", "1");
//        System.err.println(one1);
        sqlSession.commit();
    }
}

如果我们有几十个枚举,这样转换的话,那我们岂不是要分别为每一个枚举定义一个Handler,然后为每一个Handler注册。其实不必这样,我们可以定义成一个通用的枚举转换处理器,具体怎么实现呢?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值