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应用的整 ...
随机推荐
- JAVA_将唐诗按照古文样式输出
1. 如有唐诗: 锄禾日当午 汗滴禾下土 谁知盘中餐 粒粒皆辛苦 要求将这首唐诗按照古文样式输出,输出格式如下: 粒谁汗锄 粒知滴禾 皆盘禾日 辛中下当 苦餐土午 public class Text ...
- 定点分析: MySQL InnoDB是如何保证系统异常断电情况下的数据可靠性?
MySQL支持事务,所以保证数据可靠的前提是对数据的修改事务已经成功提交 这个问题可以解释为'MySQL InnoDB是如何保证事务C(一致性)D(持久性)性的?' 可能出现的两种情况: (一致性)数 ...
- 常见web安全隐患及解决方案
Abstract 有关于WEB服务以及web应用的一些安全隐患总结资料. 1. 常见web安全隐患 1.1. 完全信赖用户提交内容 开发人员决不能相信一个来自外部的数据.不管它来自用户提交 ...
- Docker配置加速器
我们国内使用官方Docker Hub仓库实在是太慢了,很影响效率 使用命令编辑文件: vim /etc/docker/daemon.json 加入下面的数据: docker-cn镜像: { " ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 使用springcloud zuul构建接口网关
一 微服务网关背景及简介 不同的微服务一般有不同的网络地址,而外部的客户端可能需要调用多个服务的接口才能完成一个业务需求.比如一个电影购票的收集APP,可能回调用电影分类微服务,用户微服务,支付微服 ...
- [Codeforces 946F]Fibonacci String Subsequences
Description 题库链接 定义 \(F(x)\) 为 \(F(x-1)\) 与 \(F(x-2)\) 的连接(其中 \(F(0) = "0",F(1) = "1& ...
- codeforces 842D Vitya and Strange Lesson
题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...
- 【NOIP2004】虫食算
Description 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045 +. 8468#6633 444455 ...
- hdu 5476 (计算几何)
题意:求三角形内∠MPB+∠APC=∠MPC+∠APB的轨迹长度- - 1.基于M的中垂线 2.三角形内的圆弧(比赛只有看自己能否猜中),ps.以下是别人家的证明 #include < ...