问题描述
例如,我看到的一个命名约定是使用 get()
返回单个实体,然后 find()
返回实体列表。
如果没有一个,你的团队使用的是什么,为什么?
通常我的名字这种方法使得该名称提示将由该方法应用的CRUD操作的类型,例如 add *
, save *
或 find *
。
-
add *
可以应用于INSERT
操作,如addPhoneNumber(Long userId)
。 -
get *
可以申请SELECT
操作,如getEmailAddress(Long userId)
。 -
set *
可以应用于执行UPDATE
操作的方法。 -
delete *
可以应用于DELETE
操作,例如deleteUser(Long userId) code>。虽然我不太确定物理删除有多有用。就个人而言,我会设置一个标志,表示该行不会被使用,而不是执行物理删除。
-
*
可以应用于检查某些东西的方法,例如isUsernameAvailable(String username)
。
Is there a standard naming convention for DAO methods, similar to JavaBeans?
For example, one naming convention I've seen is to use get()
to return a single entity and find()
to return a List of entities.
If there isn't one, what's the one your team is using and why?
Usually I name the methods in such way that the name hints the type of the CRUD operation that will be applied by the method, like add*
, save*
or find*
.
add*
can be applied onINSERT
operations, likeaddPhoneNumber(Long userId)
.get*
can be applied forSELECT
operations, likegetEmailAddress(Long userId)
.set*
can be applied on method that performs anUPDATE
operation.delete*
can be applied onDELETE
operations, likedeleteUser(Long userId)
. Althought I'm not pretty sure how useful is the physical delete. Personally, I would set a flag that denotes that the row is not gonna be used, rather than performing a physical delete.is*
can be applied on a method that check something, for exampleisUsernameAvailable(String username)
.
这篇关于DAO方法的标准命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!