MyBatis(3.2.3) - Handling enumeration types
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的更多相关文章
- 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 ...
- 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 ...
- Table of Contents - MyBatis
Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...
- Mapper XML Files详解
扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...
- “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册
原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...
- IronPython .NET Integration官方文档翻译笔记
http://ironpython.net/documentation/dotnet/这是原文地址 以下笔记仅记录阅读过程中我认为有必要记录的内容,大多数都是依赖翻译软件的机翻,配合个人对代码的理解写 ...
- 深入理解Java枚举
深入理解Java枚举 重新认识Java枚举 老实说,挺羞愧的,这么久了,一直不知道Java枚举的本质是啥,虽然也在用,但是真不知道它的底层是个啥样的 直到2020年4月28日的晚上20点左右,我才真的 ...
- C#中DataTable转化为List<T>解析
在.net项目中使用到DataTable和List<T>集合的地方较多, 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行装箱和拆箱,或对引用类 ...
- 领域驱动设计常见问题FAQ
本文出处:http://www.cqrs.nu/Faq What is a domain? The field for which a system is built. Airport managem ...
随机推荐
- HDU 3265 Posters (线段树+扫描线)(面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...
- IP访问SQL数据库设置
http://wenku.baidu.com/link?url=mnjuPMo9qJvzluCHEvqVDawpuloKeGla05a2L3UtqzD_bF1VJMb7jHY4SBhuYH3-K_xF ...
- AxWindowsMediaPlayer创建、添加播放列表(C#)
// 创见打开对话框对象实例 OpenFileDialog openFileDialog = new OpenFileDialog(); //设置为可以打开多个文件 ...
- Windows 8 之 windbg 配置
怎么安装windbg? 在Win8中,要通过安装windows 8 SDK来安装. 安装之后,在C:\Program Files (x86)\Windows Kits\8.0\Debuggers\x6 ...
- UIButton的imageEdgeInsets 和 titleEdgeInsets
我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets ...
- 在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库
在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库 http://files.cnblogs.com/files/lxzhangying/SQl2000S ...
- [Ruby01]Class, Module, Object,Kernel的关系
puts Class.ancestorsputs '11111111111111111111'puts Module.ancestorsputs '2222222222222222222'puts O ...
- EcShop之路-Smarty
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 使用Unity制作游戏关卡的教程(二)
转自:http://gamerboom.com/archives/75554 作者:by Matthias Zarzecki 本文是“使用Unity制作<The Fork Of Truth> ...
- win7重新安装win7
本方法适用于原本Win7系统下安装,无需U盘,非Ghost,简单几步操作之后与光盘安装过程一样,简便易操作,愿与和我一样有“洁癖”的朋友分享. 1.下载Win7原版光盘镜像,附下载地址: Win7旗舰 ...