15、处理枚举类型

来源:互联网 发布:网络优化塔工2017招聘 编辑:程序博客网 时间:2024/06/05 01: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@NoArgsConstructorpublic 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注册。其实不必这样,我们可以定义成一个通用的枚举转换处理器,具体怎么实现呢?

原创粉丝点击