Code Monkey home page Code Monkey logo

Comments (4)

hellokaton avatar hellokaton commented on June 3, 2024

test code

from anima.

ydq avatar ydq commented on June 3, 2024

看了下代码:
image
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.

hellokaton avatar hellokaton commented on June 3, 2024

我有一个简单的想法,把枚举作为类似于 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

假设不约定格式,也就是可以自定义。会出现几个问题

  1. 使用者要添加注解
  2. 框架内部在处理的时候不知道有几个成员(属性),因为从数据库到枚举的过程是要构造一个枚举对象的

手动指定的方式和约定其实都会产生学习成本,只不过后者写的代码少,出错率也会低一些,个人见解。

from anima.

ydq avatar ydq commented on June 3, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.