Comments (4)
from anima.
看了下代码:
AnimaUtils 中针对枚举只做了toString() 和 ordinal() 的处理,还可以针对属性做处理,例如:
enum TYPE{
TYPE_1(10,"this is type 1"),
TYPE_2(20,"this is type 2"),
TYPE_3(30,"this is type 3");
private Integer code;
private String desc;
// getter setter constructor 略..
}
class Pojo{
@EnumMapping("code")
TYPE type;
}
上例中:当我注解 @EnumMapping("code") ,则希望Anima能自动调用 type.getCode() 来获取值 存入数据库,注解中的内容是枚举的某个属性的field,然后根据这个枚举的field值来进行映射
因为ordinal 是类似于索引,当枚举取值的顺序变动的时候 ordinal 值会变,如果是一个可能会变的枚举,如不定期添加类型的枚举,可能某些时候不适合用ordinal,然后数据库为了存储效率也没用String
from anima.
我有一个简单的想法,把枚举作为类似于 K/V
来映射一个数据库操作,使用者需要遵循一种约定,所有的枚举只有2个成员:K
是数据库存储的值,V
是字符串类型的描述。格式如下:
public enum PayTypeEnum {
WAIT_PAY(100, "等待支付"),
PAY_SUCCESS(200, "支付成功");
private Integer type;
private String desc;
PayTypeEnum(Integer type, String desc) {
this.type = type;
this.desc = desc;
}
// getter setter 省略
}
在 Model
中使用直接用枚举类型,不用添加注解配置了。
为什么要约定死格式?
Java 语言中使用枚举主要是为了方便其他的代码确定具体类型,我们为了在数据库操作时省去 列值
转换到枚举的这步操作使用了如上方式;而大部分的场景就是上述例子中的样子,只是 type
的类型可能不是 Integer
。
假设不约定格式,也就是可以自定义。会出现几个问题
- 使用者要添加注解
- 框架内部在处理的时候不知道有几个成员(属性),因为从数据库到枚举的过程是要构造一个枚举对象的
手动指定的方式和约定其实都会产生学习成本,只不过后者写的代码少,出错率也会低一些,个人见解。
from anima.
@biezhi 强约束有强约束的好处(有规范),但是肯定也有它的弊端(不够灵活,且限制的比较死)。就好比servlet,有人觉得它好,有人觉得它累赘麻烦。
看了下beetlsql处理枚举这部分的源码。他的做法是直接用个map缓存下来所有要映射枚举的值(一般来说枚举的值不会很多,所以缓存量不会很大)
https://gitee.com/xiandafu/beetlsql/blob/master/src/main/java/org/beetl/sql/core/kit/EnumKit.java
我觉得可以参考一下。
这样不注解的也可以走默认的约定(如可以文档提前约定 默认调用toString方法,或者默认调用 ordinal方法,不注解就走这种方法,同时如果开发者指定注解了按照用户配置的来,因为是用户自己指定的,所以也无需框架去知道有哪些属性,上述的1和2两个问题都达成了。)
from anima.
Related Issues (20)
- 继承了Model的类无法使用Gson转换为JSON HOT 3
- 很看好这个库,强烈支持,望今后持续更新下去!
- 一点疑惑
- 已经开始公司部分程序中使用anima了
- 使用Anima.atomic连接池泄露 HOT 1
- ORACLE 支持
- java.lang.NoClassDefFoundError: com/blade/reflectasm/MethodAccess HOT 1
- 帮忙看一下这个错误是如何引起的 HOT 1
- 0.2.5版本没有isNull()这个接口
- 对模型执行update操作后id属性被置为NULL HOT 2
- 请问在insert时除了save可以BY
- 请问在insert时除了save可以BYSQL HOT 1
- 请问使用这个库可以调用存储过程吗 HOT 1
- 关于methodToFieldName方法,methodName.replace("get", "")问题
- increase value by 1
- Model类为内部类时save会报错
- 动态条件的优化问题 HOT 1
- page()的时候是不是没有设置columnmapping 字段的映射关系?
- Using Anima.deleteBatch get an exception HOT 1
- Got Keys were not fetched from database, Postgresql
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anima.