MyBatis supports persisting enum type properties out of the box. Assume that the STUDENTS table has a column gender of the type varchar to store either MALE or FEMALE as the value. And, the Student object has a gender property that is of the type enum as shown in the following code:

public enum Gender {
FEMALE,
MALE;
}

By default, MyBatis uses EnumTypeHandler to handle enum type Java properties and stores the name of the enum value. You don't need any extra configuration to do this. You can use enum type properties just like primitive type properties as shown in the following code:

public class Student {
private Integer id;
private String name;
private String email;
private PhoneNumber phone;
private Address address;
private Gender gender; //setters and getters
}
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
insert into students(name,email,addr_id, phone,gender) values(#{name},#{email},#{address.addrId},#{phone},#{gender})
</insert>

When you execute the insertStudent statement, MyBatis takes the name of the Gender enum (FEMALE/MALE) and stores it in the GENDER column.

If you want to store the ordinal position of the enum instead of the enum name, you will need to explicitly configure it.

So if you want to store 0 for FEMALE and 1 for MALE in the gender column, you'll need to register EnumOrdinalTypeHandler in the mybatis-config.xml file.

<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mybatis3.domain.Gender"/>

Be careful to use ordinal values to store in the DB. Ordinal values are assigned to enum values based on their order of declaration. If you change the declaration order in Gender enum, the data in the database and ordinal values will be mismatched.

MyBatis(3.2.3) - Handling enumeration types的更多相关文章

  1. MyBatis(3.2.3) - Handling the CLOB/BLOB types

    MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...

  2. Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

    代码如下: var query = from s in db.LoginUserServices join ss in db.Services on s.ServiceType equals ss.C ...

  3. Table of Contents - MyBatis

    Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...

  4. Mapper XML Files详解

    扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...

  5. “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册

    原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...

  6. IronPython .NET Integration官方文档翻译笔记

    http://ironpython.net/documentation/dotnet/这是原文地址 以下笔记仅记录阅读过程中我认为有必要记录的内容,大多数都是依赖翻译软件的机翻,配合个人对代码的理解写 ...

  7. 深入理解Java枚举

    深入理解Java枚举 重新认识Java枚举 老实说,挺羞愧的,这么久了,一直不知道Java枚举的本质是啥,虽然也在用,但是真不知道它的底层是个啥样的 直到2020年4月28日的晚上20点左右,我才真的 ...

  8. C#中DataTable转化为List<T>解析

    在.net项目中使用到DataTable和List<T>集合的地方较多, 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行装箱和拆箱,或对引用类 ...

  9. 领域驱动设计常见问题FAQ

    本文出处:http://www.cqrs.nu/Faq What is a domain? The field for which a system is built. Airport managem ...

随机推荐

  1. HDU 2050 折线分割平面 (递推)

    题意:略. 析:多写几个就找到规律了,第1条是2,2条时是7个,3条时是16,4条时是29,.... 那么规律就出来了2 * n * n + 1 - n; 也可以递推,第n条折线的两条边都与前n-1条 ...

  2. 任务分发系统gearman

    1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...

  3. Random的nextInt用法

    因为想当然的认为Random类中nextInt()(注:不带参数),会产生伪随机的正整数,采用如下的方式生成0~99之间的随机数: Random random = new Random(); rand ...

  4. 求一列的和,awk和perl哪个快?

    下午和群里的朋友争论了一下,有关awk和perl处理文本的速度,自己一直比较推崇perl,对awk知之甚少,结果就想当然的觉得perl快,结果一番争吵后,觉得还是实验一下靠谱,(其实是想证明一下per ...

  5. 剑指OFFER之二进制中1的个数(九度OJ1513)

    题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例.对于每个输入文件,第一行输入一个整数T,代表测试样例的数量.对于每个测试样例输入为一个整 ...

  6. 基于 Paramiko 的 SSH 通讯类

    # -*- coding: UTF-8 -*-import paramikoimport time################################################### ...

  7. java 方法调用绑定--《java编程思想》学习笔记

    将一个方法调用同一个方法主体关联起来,就是绑定. 绑定分两种 :前期绑定 和 后期绑定 . 绑定------------- | -----前期绑定-------编译期绑定 { static , fin ...

  8. Winform开发框架之权限管理系统

    本文章转载:http://www.cnblogs.com/wuhuacong/archive/2011/05/08/2040620.html 至此,权限管理模块介绍已经完毕,下面给出一个调用例子Dem ...

  9. 关于Ajax使用 Callback 函数

    1.onreadystatechange 事件 当请求被发送到服务器时,我们需要执行一些基于响应的任务. 每当 readyState 改变时,就会触发 onreadystatechange 事件. r ...

  10. pjsip视频通信开发(上层应用)之数字键盘的制作

    在pjsip视频通信开发(上层应用)之EditText重写中我制作了一个显示输入内容的EditText,这里将制作一个数字键盘,其实跟计算器一样,最多的就是用TableLayout来实现,内部通过权重 ...