Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自动填充字段问题 #265

Open
MzxVegle opened this issue Sep 10, 2024 · 5 comments
Open

自动填充字段问题 #265

MzxVegle opened this issue Sep 10, 2024 · 5 comments

Comments

@MzxVegle
Copy link

像create_time和update_time这种字段有自动填充注解的实现么?看了官网貌似只有拦截器来实现自动填充。虽然说一个项目基本上写一次就行了,但是总感觉有点啰嗦。像mybatis-flex这些都是有注解或者@column上有字段可选。为啥不弄一个注解或者@column字段去实现呢?会有什么弊端么?

@xuejmnet
Copy link
Collaborator

@MzxVegle 你是说 @column(onInsertValue = "now()")这种吗,这样不就和具体数据库绑定了吗,如果数据库没有now()函数怎么办,orm的作用是抽象而不是使用强类型来故意拼凑sql,你说是吗:)

@MzxVegle
Copy link
Author

@MzxVegle 你是说 @column(onInsertValue = "now()")这种吗,这样不就和具体数据库绑定了吗,如果数据库没有now()函数怎么办,orm的作用是抽象而不是使用强类型来故意拼凑sql,你说是吗:)

mybatis-flex这种确实是这样,倒觉得可以参考一下mybatis-plus的那种注解+handler的方式来显式的标记一下这个字段,感觉这样能更明显的看到哪个地方对这个字段处理了

@xuejmnet
Copy link
Collaborator

而且拦截器可以有无限可能更适合业务也更能抽象而不是传入字符串将设计卡死

@xuejmnet
Copy link
Collaborator

我觉得你是先入为主了,你也可以说下目前拦截器的缺点,因为拦截器可以在使用时动态启用禁用并且能支持他们没法支持的情况所以我目前还没找到mybatis-plus的那种方法优于我的地方如果有可以提出我们可以进行抉择

@MzxVegle
Copy link
Author

拦截器没啥大问题,为了灵活拦截器是必须的,mybatis-plus的那种说白了也就是类似于一个拦截器去处理字段。只是我觉得像create_time,update_time这种应该算是一种比较通用代码,完全可以做成框架的一部分,因为像create_time和update_time这种字段基本上玩不出什么花来,完全可以交给框架去管理,做成一种配置或者注解。像在php的orm里面基本上都是用户配置一下create_time和update_time的字段名,是否开启时间自动填充之类的,剩下的框架会去帮你完成填充,而完全不需要用户去再去定义一个拦截器去实现这些功能。当然,其他字段要做灵活的处理还是得用拦截器的
think-orm自动时间写入
你看像这种是不是更简单易用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants