MyBatis 查询映射自定义枚举
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * * @ClassName: IEnum * @Description: 本系统所有枚举实现的接口 规范key value 用于MyBatis枚举映射 * @author jerome_s@qq.com * @date 2015年11月29日 下午9:29:35 * */public interface IEnum { int getKey(); void setKey(int key); String getValue(); void setValue(String value);} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/** * * @ClassName: EnumKeyTypeHandler * @Description: 本系统所有枚举都是使用这个处理器将key定为存取的依据 * @author jerome_s@qq.com * @date 2015年11月29日 下午9:34:14 * @see 参考源码EnumOrdinalTypeHandler/EnumTypeHandler * @see 参考 http://mybatis.org/mybatis-3/zh/configuration.html#typeHandlers * */public class EnumKeyTypeHandler extends BaseTypeHandler<IEnum>{ private Class<IEnum> type; private final IEnum[] enums; /** * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现 * @param type 配置文件中设置的转换类 */ public EnumKeyTypeHandler(Class<IEnum> type) { if (type == null) throw new IllegalArgumentException("Type argument cannot be null"); this.type = type; this.enums = type.getEnumConstants(); if (this.enums == null) throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); } @Override public IEnum getNullableResult(ResultSet rs, String columnName) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = rs.getInt(columnName); if (rs.wasNull()) { return null; } else { // 根据数据库中的code值,定位IEnum子类 return locateIEnum(i); } } @Override public IEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = rs.getInt(columnIndex); if (rs.wasNull()) { return null; } else { // 根据数据库中的code值,定位IEnum子类 return locateIEnum(i); } } public IEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = cs.getInt(columnIndex); if (cs.wasNull()) { return null; } else { // 根据数据库中的code值,定位IEnum子类 return locateIEnum(i); } } public void setNonNullParameter(PreparedStatement ps, int i, IEnum parameter, JdbcType jdbcType) throws SQLException { // baseTypeHandler已经帮我们做了parameter的null判断 ps.setInt(i, parameter.getKey()); } /** * 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷 * @param key 数据库中存储的自定义code属性 * @return code对应的枚举类 */ private IEnum locateIEnum(int key) { for(IEnum status : enums) { if(status.getKey()== key) { return status; } } throw new IllegalArgumentException("未知的枚举类型:" + key + ",请核对" + type.getSimpleName()); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/** * 性别枚举 * * @author jerome_s@qq.com */public enum SexEnum implements IEnum { MAN(1, "男"), WOMAN(2, "女"); private int key; private String value; private SexEnum(int key, String value) { this.key = key; this.value = value; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; }} |
|
1
2
3
4
|
List<User> users = sqlSession.selectList("UserMapper.selectUsers4Enum");for (User u : users) { System.out.println(u.toString());} |
|
1
2
3
4
5
6
|
<resultMap id="selectUsers4EnumResultMap" type="User"> <result property="sex" column="sex" javaType="com.hqu.enums.SexEnum" typeHandler="com.hqu.enums.EnumKeyTypeHandler"/></resultMap><select id="selectUsers4Enum" resultMap="selectUsers4EnumResultMap"> SELECT * FROM t_user</select> |
|
1
2
3
4
|
User [id=1, user_name=jerome100, age=26, sex=MAN, birthday=Sat Dec 31 00:00:00 CST 2016]User [id=3, user_name=jelly, age=25, sex=WOMAN, birthday=Sat Dec 31 00:00:00 CST 2016]User [id=4, user_name=jerome, age=26, sex=MAN, birthday=Sat Dec 31 00:00:00 CST 2016]User [id=5, user_name=jelly, age=25, sex=WOMAN, birthday=Sat Dec 31 00:00:00 CST 2016] |
MyBatis 查询映射自定义枚举的更多相关文章
- mybatis-枚举类型的typeHandler&自定义枚举类型typeHandler
MyBatis内部提供了两个转化枚举类型的typeHandler给我们使用. org.apache.ibatis.type.EnumTypeHandler 是使用枚举字符串名称作为参数传递的 org. ...
- Spring Boot入门系列(十七)整合Mybatis,创建自定义mapper 实现多表关联查询!
之前讲了Springboot整合Mybatis,介绍了如何自动生成pojo实体类.mapper类和对应的mapper.xml 文件,并实现最基本的增删改查功能.mybatis 插件自动生成的mappe ...
- 【转载】Mybatis多参数查询映射
转载地址:http://www.07net01.com/zhishi/402787.html 最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴 露了很多问题,其 ...
- Mybatis实战之自定义TypeHandler处理枚举
在Mybatis中,处理枚举类的TypeHandler有两个: EnumTypeHandler: 用于保存枚举名 EnumOrdinalTypeHandler: 用于保存枚举的序号. 在实际项目中,以 ...
- 学习Spring Boot:(十二)Mybatis 中自定义枚举转换器
前言 在 Spring Boot 中使用 Mybatis 中遇到了字段为枚举类型,数据库存储的是枚举的值,发现它不能自动装载. 解决 内置枚举转换器 MyBatis内置了两个枚举转换器分别是:org. ...
- AutoMapper在MVC中的运用03-字典集合、枚举映射,自定义解析器
本篇AutoMapper使用场景: ※ 源字典集合转换成目标字典集合 ※ 枚举映射 ※ 自定义解析器 ※ 源中的复杂属性和Get...方法转换成目标属性 源字典集合转换成目标字典集合 □ Domain ...
- mybatis自定义枚举转换类
转载自:http://my.oschina.net/SEyanlei/blog/188919 mybatis提供了EnumTypeHandler和EnumOrdinalTypeHandler完成枚举类 ...
- mybatis 高级映射和spring整合之查询缓存(5)
mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...
- Mybatis sql映射文件浅析 Mybatis简介(三)
简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML为载体映射SQL 之前提到过,各项配置信息将Mybatis应用的整 ...
随机推荐
- [翻译] Tensorflow中name scope和variable scope的区别是什么
翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...
- [Linux]使用awk批量杀进程的命令
碰到需要杀掉某一类进程的时候,如何批量杀掉这些进程,使用awk命令是很好的选择. ps -ef|grep aaa|grep -v grep|awk '{print "kill -9 &quo ...
- jQuery系列 第三章 jQuery框架操作CSS
第三章 jQuery框架操作CSS 3.1 jQuery框架的CSS方法 jQuery框架提供了css方法,我们通过调用该方法传递对应的参数,可以方便的来批量设置标签的CSS样式. 使用JavaScr ...
- vue的入门/简介
vue 特点 1. 响应的数据绑定/响应式编程 2. 组件化 vue优点 1. 轻量级的框架 2. 简单易学 3. 双向数据绑定 4. 组件化 5. 视图,数据,结构分离 6. 虚拟DOM 7. ...
- [LeetCode] Largest Palindrome Product 最大回文串乘积
Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...
- [HNOI 2011]卡农
Description 题库链接 在集合 \(S=\{1,2,...,n\}\) 中选出 \(m\) 个子集,满足三点性质: 所有选出的 \(m\) 个子集都不能为空. 所有选出的 \(m\) 个子集 ...
- TopCoder SRM 566 Div 1 - Problem 1000 FencingPenguins
传送门:https://284914869.github.io/AEoj/566.html 题目简述: 平面上有中心在原点,一个点在(r,0)处的正n边形的n个顶点.平面上还有m个企鹅,每个企鹅有一个 ...
- codeforces Gym - 100633J Ceizenpok’s formula
拓展Lucas #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- POJ 3261 可重叠k次最长重复子串
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13127 Accepted: 5842 Ca ...