Skip to content

Commit ba31a48

Browse files
committed
add MyBatisEnum typeHandler
1 parent eebe7a5 commit ba31a48

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
layout: post
3+
title: "SSM(一)-MyBatis Enum 映射"
4+
subtitle: "extends typeHandler"
5+
date: 2017-11-20 12:00:00
6+
author:     "zhang.xx"
7+
header-img: "img/post-everydayguess.jpg"
8+
catalog: true
9+
tags:
10+
- SSM
11+
---
12+
13+
> 伤情最是晚凉天,憔悴斯人不堪怜。邀酒摧肠三杯醉,寻香惊梦五更寒。
14+
钗头凤斜卿有泪,荼蘼花了我无缘。小楼寂寞心与月,也难如钩也难圆。
15+
16+
---
17+
18+
## 前言
19+
这篇博文是偶来得之,今天在做SSM框架集成ElasticSearch的demo,先弄了个空的SSM框架,然后写了个简单的list查询,其中有个字段sex(性别),想用 **枚举** 来实现以下,因为之前用枚举类型比较少,正好借助这个熟练下,却想不到也遇到了不少的坑,花了两个多小时的时间,才实现了这个简单的功能。
20+
21+
## 概览
22+
23+
一开始我是直接简单暴力,直接照着另一个demo的代码,写了一个`SexEnum`枚举类,然后直接ResultMap都没写直接上来就强行映射,可想而知,是行不通的,于是开始google了几篇文章,才知道要自定义转换器,于是就有了以下操作;这些操作的目的:**可以做到直接使用枚举插入数据库中会自动变成数字或者其他东西,而查询时可以直接将数据库中的数字或其他东西转换成对应的枚举值**
24+
25+
代码托管在github:[SsmESDemo](https://github.com/zhangxx0/SsmESDemo)
26+
27+
同时,这个demo也是SSM集成ES的demo,不出意外的话,接下来会有介绍它的博文。
28+
29+
## 定义枚举类
30+
31+
```java
32+
/**
33+
* 性别枚举类
34+
*/
35+
public enum SexEnum {
36+
MALE(0,""),
37+
FEMALE(1,"");
38+
39+
private int sex;
40+
private String sexName;
41+
42+
SexEnum(int sex, String sexName) {
43+
this.sex = sex;
44+
this.sexName = sexName;
45+
}
46+
public int getSex() {
47+
return sex;
48+
}
49+
public String getSexName() {
50+
return sexName;
51+
}
52+
53+
}
54+
```
55+
56+
## 枚举映射Handler
57+
58+
继承`BaseTypeHandler`,并重写其四个方法:
59+
60+
```java
61+
/**
62+
* 性别枚举handler
63+
*
64+
* @date 2017年11月20日15:26:03
65+
*/
66+
public class SexEnumTypeHandler extends BaseTypeHandler<SexEnum> {
67+
68+
private Class<SexEnum> sexEnum;
69+
70+
public SexEnumTypeHandler(Class<SexEnum> sexEnum) {
71+
if (sexEnum == null) {
72+
throw new IllegalArgumentException("Type argument can not be null");
73+
}
74+
this.sexEnum = sexEnum;
75+
}
76+
77+
public void setNonNullParameter(PreparedStatement preparedStatement, int i, SexEnum sexEnum, JdbcType jdbcType) throws SQLException {
78+
preparedStatement.setInt(i,sexEnum.getSex());
79+
}
80+
81+
public SexEnum getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
82+
int i = resultSet.getInt(columnName);
83+
if (resultSet.wasNull()) {
84+
return null;
85+
} else {
86+
return getValuedEnum(i);
87+
}
88+
}
89+
90+
public SexEnum getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
91+
int i = resultSet.getInt(columnIndex);
92+
if (resultSet.wasNull()) {
93+
return null;
94+
} else {
95+
return getValuedEnum(i);
96+
}
97+
}
98+
99+
public SexEnum getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
100+
int i = callableStatement.getInt(columnIndex);
101+
if (callableStatement.wasNull()) {
102+
return null;
103+
} else {
104+
return getValuedEnum(i);
105+
}
106+
}
107+
108+
private SexEnum getValuedEnum(int value) {
109+
SexEnum[] objs = sexEnum.getEnumConstants();
110+
for(SexEnum em:objs){
111+
if(em.getSex()==value){
112+
return em;
113+
}
114+
}
115+
throw new IllegalArgumentException(
116+
"Cannot convert " + value + " to " + sexEnum.getSimpleName() + " by value.");
117+
}
118+
}
119+
```
120+
121+
## 实体类使用枚举
122+
123+
```java
124+
public class UserDemo {
125+
private int age;
126+
private Long id;
127+
private String userName;
128+
private SexEnum sex;
129+
private Date birthday;
130+
// ...
131+
}
132+
```
133+
134+
## MyBatis的xml文件修改
135+
136+
```xml
137+
<!DOCTYPE mapper
138+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
139+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
140+
<mapper namespace="com.danke.dao.UserDemoDao">
141+
<!--目的:为dao接口方法提供sql语句配置
142+
即针对dao接口中的方法编写我们的sql语句-->
143+
144+
<resultMap id="userDemoMap" type="UserDemo">
145+
<id column="id" javaType="LONG" property="id"/>
146+
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
147+
<result column="age" jdbcType="INTEGER" property="age"/>
148+
<result column="sex" property="sex" typeHandler="com.danke.util.typehandler.SexEnumTypeHandler"/>
149+
<result column="birthday" jdbcType="DATE" property="birthday"/>
150+
<result column="create_date" jdbcType="TIME" property="createDate"/>
151+
</resultMap>
152+
153+
<select id="queryAll" resultMap="userDemoMap">
154+
SELECT id,user_name,age,sex,birthday,create_date
155+
FROM t_user
156+
ORDER BY create_date DESC
157+
</select>
158+
159+
</mapper>
160+
```
161+
162+
## 前台页面对枚举的使用
163+
164+
```js
165+
<td>${item.sex.sexName}</td>
166+
```
167+
168+
169+
## 通用Enum映射的实现
170+
```java
171+
TODO
172+
```
173+
174+
175+
## 总结与参考
176+
177+
178+
参考:
179+
[MyBatis 使用通用 Enum 映射](http://albertchen.top/2016/02/18/MyBatis-%E4%BD%BF%E7%94%A8%E9%80%9A%E7%94%A8-Enum-%E6%98%A0%E5%B0%84/)
180+
[mybatis枚举自动转换实现](http://blog.csdn.net/fighterandknight/article/details/51520402)
181+
[mybatis 官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html#typeHandlers)
182+
183+
184+
2017年11月-by zhang.xx

0 commit comments

Comments
 (0)