- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章优雅地在Java应用中实现全局枚举处理的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
背景描述 。
为了表达某一个属性,具备一组可选的范围,我们一般会采用两种方式。枚举类和数据字典,两者具有各自的优点。枚举类写在java代码中,方便编写相应的判断逻辑,代码可读性高,枚举类中的属性是可提前预估和确定的。数据字典,一般保存在数据库,不便于编写判断和分支逻辑,因为数据如果有所变动,那么对应的代码逻辑很有可能失效,强依赖数据库数据的正确性,数据字典中对应的属性对业务影响并不大,日常开发中常用做分类,打标签使用,属性的多少无法估计.
目前基本上没有一个很好的全局处理枚举类的方案,所以我就自己综合各方面资料写了一个.
代码 。
架构还在不断完善中,代码不一定可以跑起来,不过关于枚举的配置已经完成,大家可以阅读并参考借鉴:pretty-demo 。
前言 。
大多数公司处理枚举的时候,会自定义一个枚举转换工具类,或者在枚举类中编写一个静态方法实现integer转换枚举的方式.
比如:
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
|
// 静态方法方式
public
enum
genderenum {
// 代码略
public
static
genderenum get(
int
value) {
for
(genderenum item : genderenum.values()) {
if
(value == item.getvalue()) {
return
item;
}
}
return
null
;
}
}
// 工具类方式
public
class
enumutil {
public
static
<e
extends
enumerable> e of(
@nonnull
class
<e> classtype,
int
value) {
for
(e enumconstant : classtype.getenumconstants()) {
if
(value == enumconstant.getvalue()) {
return
enumconstant;
}
}
return
null
;
}
}
genderenum gender = enumutil.of(genderenum.
class
,
1
);
|
这种方式很麻烦,或者需要手动编写对应的静态方法,或者需要手动调用工具类进行转换.
解决方案 。
为了方便起见,我做了一个全局枚举值转换的方案,这个方案可以实现前端通过传递int到服务端,服务端自动转换成枚举类,进行相应的业务判断之后,再以数字的形式存到数据库;我们在查数据的时候,又能将数据库的数字转换成java枚举类,在处理完对应的业务逻辑之后,将枚举和枚举类对应的展示信息一起传递到前台,前台不需要维护这个枚举类和展示信息的对应关系,同时展示信息支持国际化处理,具体的方案如下:
1、基于约定大于配置的原则,制定统一的枚举类的编写规则。大概规则如下:
下面是枚举接口和一个枚举示例:
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
|
public
interface
enumerable<e
extends
enumerable> {
/**
* 获取在i18n文件中对应的 key
* @return key
*/
@nonnull
string getkey();
/**
* 获取最终保存到数据库的值
* @return 值
*/
@nonnull
int
getvalue();
/**
* 获取 key 对应的文本信息
* @return 文本信息
*/
@nonnull
default
string gettext() {
return
i18nmessageutil.getmessage(
this
.getkey(),
null
);
}
}
public
enum
genderenum
implements
enumerable {
/** 男 */
male(
1
,
"male"
),
/** 女 */
female(
2
,
"female"
);
private
int
value;
private
string key;
genderenum(
int
value, string key) {
this
.value = value;
this
.key = key;
}
@override
public
string getkey() {
return
this
.key;
}
@override
public
int
getvalue() {
return
this
.value;
}
}
|
我们要做的就是,每个我们编写的枚举类,都需要按这样的方式进行编写,按照规范定义的枚举类方便下面统一编写.
2、我们分析下controller层面的数据进和出,从而处理好枚举类和int值的转换,在spring mvc中,框架帮我们做了数据类型的转换,所以我们以 spring mvc作为切入点。前台发送到服务端的请求,一般有参数在url中和body中两种方式为主,分别以get请求和post请求配合@requestbody为代表.
【入参】get方法为代表,请求的mediatype为"application/x-www-form-urlencoded",此时将 int 转换成枚举,我们注册一个新的converter,如果spring mvc判断到一个值要转换成我们定义的枚举类对象时,调用我们设定的这个转换器 。
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
|
@configuration
public
class
mvcconfiguration
implements
webmvcconfigurer, webbindinginitializer {
/**
* [get]请求中,将int值转换成枚举类
* @param registry
*/
@override
public
void
addformatters(formatterregistry registry) {
registry.addconverterfactory(
new
enumconverterfactory());
}
}
public
class
enumconverterfactory
implements
converterfactory<string, enumerable> {
private
final
map<
class
, converter> convertercache =
new
weakhashmap<>();
@override
@suppresswarnings
({
"rawtypes"
,
"unchecked"
})
public
<t
extends
enumerable> converter<string, t> getconverter(
@nonnull
class
<t> targettype) {
return
convertercache.computeifabsent(targettype,
k -> convertercache.put(k,
new
enumconverter(k))
);
}
protected
class
enumconverter<t
extends
enumerable>
implements
converter<integer, t> {
private
final
class
<t> enumtype;
public
enumconverter(
@nonnull
class
<t> enumtype) {
this
.enumtype = enumtype;
}
@override
public
t convert(
@nonnull
integer value) {
return
enumutil.of(
this
.enumtype, value);
}
}
}
|
【入参】post为代表,将 int 转换成枚举。这块我们和前台达成一个约定( ajax中applicationtype),所有在body中的数据必须为json格式。同样后台@requestbody对应的参数的请求的mediatype为"application/json",spring mvc中对于json格式的数据,默认使用 jackson2httpmessageconverter。在jackson转换成实体时候,有@jsoncreator和@jsonvalue两个注解可以用,但是感觉还是有点麻烦。为了统一处理,我们需要修改jackson对枚举类的序列化和反序列的支持。配置如下:
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
|
@configuration
@slf4j
public
class
jacksonconfiguration {
/**
* jackson的转换器
* @return
*/
@bean
@primary
@suppresswarnings
({
"rawtypes"
,
"unchecked"
})
public
mappingjackson2httpmessageconverter mappingjacksonhttpmessageconverter() {
final
mappingjackson2httpmessageconverter converter =
new
mappingjackson2httpmessageconverter();
objectmapper objectmapper = converter.getobjectmapper();
// include.non_empty 属性为 空("") 或者为 null 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
objectmapper.setserializationinclusion(jsoninclude.include.non_empty);
// 反序列化时候,遇到多余的字段不失败,忽略
objectmapper.configure(deserializationfeature.fail_on_unknown_properties,
false
);
// 允许出现特殊字符和转义符
objectmapper.configure(jsonparser.feature.allow_unquoted_control_chars,
true
);
// 允许出现单引号
objectmapper.configure(jsonparser.feature.allow_single_quotes,
true
);
simplemodule customermodule =
new
simplemodule();
customermodule.adddeserializer(string.
class
,
new
stringtrimdeserializer(string.
class
));
customermodule.adddeserializer(enumerable.
class
,
new
enumdeserializer(enumerable.
class
));
customermodule.addserializer(enumerable.
class
,
new
enumserializer(enumerable.
class
));
objectmapper.registermodule(customermodule);
converter.setsupportedmediatypes(immutablelist.of(mediatype.text_html, mediatype.application_json));
return
converter;
}
}
public
class
enumdeserializer<e
extends
enumerable>
extends
stddeserializer<e> {
private
class
<e> enumtype;
public
enumdeserializer(
@nonnull
class
<e> enumtype) {
super
(enumtype);
this
.enumtype = enumtype;
}
@override
public
e deserialize(jsonparser jsonparser, deserializationcontext deserializationcontext)
throws
ioexception {
return
enumutil.of(
this
.enumtype, jsonparser.getintvalue());
}
}
|
【出参】当我们查询出结果,要展示给前台的时候,我们会对结果集增加@responsebody注解,这时候会调用jackson的序列化方法,所以我们增加了枚举类的序列配置。如果我们只简单的将枚举转换成 int 给前台,那么前台需要维护这个枚举类的 int 和对应展示信息的关系。所以这块我们将值和展示信息一同返给前台,减轻前台的工作压力.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 注册枚举类序列化处理类
customermodule.addserializer(enumerable.
class
,
new
enumserializer(enumerable.
class
));
public
class
enumserializer
extends
stdserializer<enumerable> {
public
enumserializer(
@nonnull
class
<enumerable> type) {
super
(type);
}
@override
public
void
serialize(enumerable enumerable, jsongenerator jsongenerator, serializerprovider serializerprovider)
throws
ioexception {
jsongenerator.writestartobject();
jsongenerator.writenumberfield(
"value"
, enumerable.getvalue());
jsongenerator.writestringfield(
"text"
, enumerable.gettext());
jsongenerator.writeendobject();
}
}
|
这样关于入参和出参的配置都完成了,我们可以保证,所有前台传递到后台的 int 都会自动转换成枚举类。如果返回的数据有枚举类,枚举类也会包含值和展示文本,方便简单.
3、存储层关于枚举类的转换。这里选的 ORM 框架为 Mybatis ,但是你如果翻看官网,官网的资料只提供了两个方案,就是通过枚举隐藏字段name和ordinal的转换,没有一个通用枚举的解决方案。但是通过翻看 github 中的 issue 和 release 记录,发现在 3.4.5版本中就提供了对应的自定义枚举处理配置,这块不需要我们做过多的配置,我们直接增加 mybatis-spring-boot-starter 的依赖,直接配置对应的Yaml 文件就实现了功能.
1
2
3
4
5
|
application.yml
--
mybatis:
configuration:
default
-
enum
-type-handler: github.shiyajian.pretty.config.enums.enumtypehandler
|
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
|
public
class
enumtypehandler<e
extends
enumerable>
extends
basetypehandler<e> {
private
class
<e> enumtype;
public
enumtypehandler() {
/* instance */
}
public
enumtypehandler(
@nonnull
class
<e> enumtype) {
this
.enumtype = enumtype;
}
@override
public
void
setnonnullparameter(preparedstatement preparedstatement,
int
i, e e, jdbctype jdbctype)
throws
sqlexception {
preparedstatement.setint(i, e.getvalue());
}
@override
public
e getnullableresult(resultset rs, string columnname)
throws
sqlexception {
int
value = rs.getint(columnname);
return
rs.wasnull() ?
null
: enumutil.of(
this
.enumtype, value);
}
@override
public
e getnullableresult(resultset rs,
int
columnindex)
throws
sqlexception {
int
value = rs.getint(columnindex);
return
rs.wasnull() ?
null
: enumutil.of(
this
.enumtype, value);
}
@override
public
e getnullableresult(callablestatement cs,
int
columnindex)
throws
sqlexception {
int
value = cs.getint(columnindex);
return
cs.wasnull() ?
null
: enumutil.of(
this
.enumtype, value);
}
}
|
这样我们就完成了从前台页面到业务代码到数据库的存储,从数据库查询到业务代码再到页面的枚举类转换。整个项目中完全不需要再手动去处理枚举类了。我们的开发流程简单了很多.
结语 。
一个好的方案并不需要多么高大上的技术,比如各种反射,各种设计模式,只要设计合理,就是简单易用,类似中国古代的榫卯.
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:https://www.cnblogs.com/shiyajian/p/10363554.html 。
最后此篇关于优雅地在Java应用中实现全局枚举处理的方法的文章就讲到这里了,如果你想了解更多关于优雅地在Java应用中实现全局枚举处理的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!