官方介绍
若想映射枚举类型 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();
}
}