MyBatis配置之枚举类型typeHandler讲解(上)

之前发布了一篇《MyBatis配置之typeHandler类型处理器》的文章,讲解了在使用MyBatis时如何自定义typeHandler。但是在MyBatis中枚举类型的typeHandler则有自己特殊的规则。

在MyBatis中提供了两个转化枚举类型的typeHandler给用户使用。

  • org.apache.ibatis.type.EnumTypeHandler
  • org.apache.ibatis.type.EnumOrdinalTypeHandler

其中,EnumTypeHandler是使用枚举字符串名称作为参数传递的,EnumOrdinalTypeHandler是使用整数下标作为参数传递的。如果枚举和数据库字典项保持一致,就可以使用它们。然而这两个枚举类型应用却不是那么广泛,更多的情况下是使用自定义的typeHandler处理枚举类型。

本篇文章中就以性别为例,讲解一下怎样使用枚举类typeHandler。

定义一个性别枚举类

public enum Sex {
  MALE(1,"男"),FEMALE(2,"女");
  private int id;
  private String name;
  private Sex(int id,String name) {
    this.id = id;
    this.name = name;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
   this.id = id;
  }
  public Sstring getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public static Sex getSex(int id) {
    if(id == 1) {
      return MALE;
    } else if(id == 2) {
      return FEMALE;
    }
    return null;
  }
}

在没有配置的情况下,MyBatis默认使用EnumOrdinalTypeHandler枚举类型处理器。为了能让EnumOrdinalTypeHandler能够处理我们自定义的枚举类,需要在MyBatis做如下配置。

<typeHandlers>
  <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.demo.enums.Sex" />
</typeHandlers>

这样MyBatis就可以识别我们自定义的枚举类了。当然这还不够,还需要在对应的mapper映射文件中进行响应的配置。我们自定义的枚举类是在用户信息实体类中使用,所以需要在用户实体类对应的mapper映射文件中进行配置。

<?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.demo.mapper.UserMapper">
  <resultMap type="com.demo.entity.UserEntity" id="userMap">
    <id column="id" property="id" javaType="long" jdbcType="BIGINT" />
    <result column="user_name" property="userName" />
    <result column="cnname" property="cnname" />
    <result column="birthday" property="birthday" />
    <result column="sex" property="sex" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" />
    <result column="email" property="email" />
    <result column="mobile" property="mobile" />
    <result column="note" property="note" />
  </resultMap>
  <select id="getUser" parameterType="long" resultMap="userMap">
    select id,user_name,cnname,birthday,sex,email,mobile,note from t_user where id=#{id}
  </select>
  <insert parameterType="com.demo.entity.UserEntity" id="insertUser">
    insert into t_user(user_name,cnname,birthday,sex,email,mobile,note) values(#{userName},#{cnname},#{birthday},#{sex,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler},#{email},#{mobile},#{note})
   </insert>
</mapper>

UserMapper.java

public interface UserMapper {
  public User getUser(Long id);
  public int insertUser(UserEntity userEntity);
}

接下来就可以测试了

TestMyTypeHandler.java

public class TestMyTypeHandler {
  public static void main(String[] args) {
    SqlSession sqlSession=null;
    try {
      sqlSession = SqlSessionFactoryUtil.openSqlSession();
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      UserEntity userEntity = new UserEntity();
      userEntity.setUserName("liubei");
      userEntity.setCnname("刘备");
      userEntity.setMobile("18888888888888");
      userEntity.setSex(Sex.MALE);
      userEntity.setEmail("liubei@163.com");
      userEntity.setNote("测试枚举类型转换器");
      userEntity.setBirthday(new Date());
      userMapper.insertUser(userEntity);
      UserEntity userEntity2 = userMapper.getUser(1L);
      System.out.println(userEntity2.getSex());
      sqlSession.commit();
    } catch(Exception ex) {
      System.err.println(ex.getMessage());
      sqlSession.rollback();
    } finally {
      if(sqlSession != null) {
        sqlSession.close();
      }
    }
  }
}

原创文章,作者:ZERO,如若转载,请注明出处:https://www.edu24.cn/course/mybatis-enumordinaltypehandler.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ZEROZERO
上一篇 2022年4月20日
下一篇 2022年10月27日

相关推荐

  • JAVA基础知识整理

    终于下定决心2020年转JAVA开发,自学之路坎坷曲折。俗话说的话,好记性不如烂笔头。如果有小伙伴们也像我一样在JAVA自学之路上徘徊,那就关注一下我的博客网站。我会不定期更新一下…

    2020年1月11日
    1.8K
  • JavaScript中call、apply及bind的深度解析

    函数原型链中的 apply,call 和 bind 方法是 JavaScript 中相当重要的概念,与 this 关键字密切相关,相当一部分人对它们的理解还是比较浅显,所谓js基础…

    2019年8月5日
    1.6K
  • 回调函数散记

    今天被将要入职的公司的开发人员询问了一个项目中遇到的问题,关于函数内访问外部函数的情况。大致现象如下:js文件中有两个同级函数FnA和FnB,想在函数FnA中调用FnB。 一看就是…

    2019年8月16日
    1.5K
  • spring4.x学习之用户登录与注册

    在之前的文章中我已经把后端工程项目创建好了,接下来就是编写项目了。 首先,我先创建一个数据库。数据库使用的是MySQL,数据库管理工具用的是Navicat。 打开数据库管理工具Na…

    2019年3月21日
    2.1K
  • JavaScript基础知识八问

    JavaScript是前端开发中非常重要的一门语言,浏览器是他主要运行的地方。JavaScript是一个非常有意思的语言,但是他有很多一些概念,大家经常都会忽略。比如说,原型,闭包…

    2020年12月30日
    1.0K
  • MySQL数据库基础之索引相关知识点整理

    数据库对象索引的出现,除了可以提高数据库管理系统的查找速度,而且还可以保证字段的唯一性,从而实现数据库表的完整性。 MySQL支持6种索引:普通索引、唯一索引、全文索引、单列索引、…

    2020年7月9日
    1.7K
  • Java自学之反射机制

    重用性是面向对象设计的核心原则。为了进一步提升代码的重用性,Java提供了反射机制。反射技术首先考虑的是“反”与“正”的操作,所谓的“正”操作,是指当开发者使用一个类的时候,一定要…

    2020年12月24日
    1.2K
  • Webpack入门,模块打包之CommonJS简介

    CommonJS 说到前端的模块,不得不讲一下CommonJS。CommonJS是由JavaScript社区于2009年提出的包含模块、文件、IO、控制台在内的一系列标准。Node…

    2022年11月10日
    558
  • Java自学之泛型

    在Java语言中,为了方便接收参数类型的统一,提供了核心类Object,利用此类对象可以接收所有类型的数据(包括基本数据类型和引用数据类型)。但是由于其所描述的数据范围过大,所以在…

    2020年12月8日
    1.3K