-
Notifications
You must be signed in to change notification settings - Fork 55
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
希望添加一个主键生成接口标准,让用户自主实现接口来返回主键Id #231
Labels
enhancement
New feature or request
Comments
@chengliang4810 可以通过以下方式来实现 public interface HasId{
String getId();
void setId(String id);
}
@Data
public class MyUser implements HasId{
private String id;
} 拦截器判断
当然这只是一种手段你也可以使用注解模式 @Data
@HasIdAnnotation
public class MyUser{
private String id;
}
@Override
public void configureInsert(Class<?> entityClass, EntityInsertExpressionBuilder entityInsertExpressionBuilder, Object entity) {
EntityMetadata entityMetadata = entityInsertExpressionBuilder.getRuntimeContext().getEntityMetadataManager().getEntityMetadata(entityClass);
String singleKey = entityMetadata.getSingleKeyProperty();//multi keys will throw exception
ColumnMetadata columnOrNull = entityMetadata.getColumnOrNull(singleKey);
if(columnOrNull!=null){
Object idValue=columnOrNull.getGetterCaller().apply(entity);
if(idValue==null){
columnOrNull.getSetterCaller().call(entity,"我的id");
}
}
// EntityMetadata entityMetadata = entityInsertExpressionBuilder.getRuntimeContext().getEntityMetadataManager().getEntityMetadata(entityClass);
//
// Collection<String> keyProperties = entityMetadata.getKeyProperties();
// if(EasyCollectionUtil.isSingle(keyProperties)){
// String singleKey = EasyCollectionUtil.first(keyProperties);
// ColumnMetadata columnOrNull = entityMetadata.getColumnOrNull(singleKey);
// if(columnOrNull!=null){
// Object idValue=columnOrNull.getGetterCaller().apply(entity);
// if(idValue==null){
// columnOrNull.getSetterCaller().call(entity,"我的id");
// }
// }
// }
//apply处判断是否有注解HasIdAnnotation
} |
我只是给出了一种模式您如果想要其他的可以自行实现毕竟我也不知道你要什么id或者说id本来就是跟着业务走的而不是框架提供的如果我提供了雪花id那么可能使用雪花id的人很方便但是如果您使用变种的雪花id那么我是否又要增加一个呢 |
如果由您来实现每一种生成方式肯定不合适,比较理想的方案应该是您来定个标准接口即可,例如下方代码
您的程序中只需要调用该接口的实例来获取结果即可,当然在这个标准接口下您可以增加一些常用的Id生成策略,比如雪花ID、UUId之类的。 |
非常棒的一个想法 |
xuejmnet
added a commit
that referenced
this issue
Jun 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我看到官网案例中的案例是,在Base实体类中设置主键字段,在实体拦截器中转换为Base实体类进行设置值。那么当不继承Base类的情况,就没办法给对象的主键设置主键Id。
The text was updated successfully, but these errors were encountered: