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. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  2. UI进阶 动画

    前言:所谓动画,即应用界面上展示的各种过渡效果,不过其实没有动画并不影响我们产品的功能实现 一.动画 1.动画可以达到的效果 传达状态 提高用户对直接操作的感知 帮助用户可视化操作的结果 2.使用动画 ...

  3. 【Java】C/C++与Java的简单比较

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827273.html     C/C++:            编译(不同的系统编译出不同的机器码,所以同一 ...

  4. .Net 揭密--JIT怎样运行你的代码

    方法调用: 第一部分 (普通调用) 译者:我们都知道.NET托管代码如C#.VB.NET写成的代码,都是先被编译成中间语言(IL,Intermediate Language,在运行时,再由即时编译器( ...

  5. C++,C#,Python

    1.C++的思路:无论是基本类型,还是类类型,对象的传递提供了两种方式,一个是整体拷贝,一个是复制引用.整体拷贝对应着copy构造和copy赋值,复制引用就是通过引用或者指针实现的,当然指针本身还是整 ...

  6. IOS Note - Core NS Data Types

    NSString (Immutable)NSMutableString (rarely used)NSNumberNSValueNSData (bits)NSDateNSArray (Immutabl ...

  7. Codeforces Gym 100286B Blind Walk DFS

    Problem B. Blind WalkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/cont ...

  8. delphi 添加节点

      //在选中的节点中上添加1个节点 procedure TForm1.Button1Click(Sender: TObject); begin     with TreeView1 do     b ...

  9. ios开发——实用技术篇OC篇&获取设备唯一标识

    获取设备唯一标识 WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS ...

  10. mysqldump原理1