简体   繁体   English

如何在方法ActiveJDBC中将模型作为参数传递

[英]How to pass Model as an argument in a method ActiveJDBC

Can I pass Model as an argument in a method using ActiveJDBC? 我可以在使用ActiveJDBC的方法中将模型作为参数传递吗?

Something like this: 像这样:

public Set<String> getColNames(Model modelName){
    Set<String> set = modelName.attributeNames();
    for(String x: set){
       System.out.prtinln(x);
    }
    return set;
}

That way I can just pass the model and would save a lot of time on doing the same code on each Model right? 这样,我就可以通过模型,并在每个模型上执行相同的代码会节省很多时间,对吗? Like this: 像这样:

Staff staff = new Staff();
Set<String> set = getColNames(staff);

Is this possible??? 这可能吗??? Getting the attribute names is just an great example on this, this is not only the purpose why I asked this. 获取属性名称只是一个很好的例子,这不仅是我问这个问题的目的。

Help would be appreciated! 帮助将不胜感激!

If you could explain why you need this, it would be easier to give directions. 如果您可以解释为什么需要这样做,那么指导会更容易。

In any case, you can do this with one line of code; 无论如何,您都可以使用一行代码来完成此操作。

Map values = model.toMap();

See: Model#toMap() 请参阅: Model#toMap()

I think that what Igor means, is that you can build your own static method, on a class called Util. 我认为Igor的意思是,您可以在称为Util的类上构建自己的静态方法。 This method would be something like this. 这种方法将是这样的。

public static Set<String > getColNames(Model model) {
    return model.toMap().keySet();
}

Then, you can use it like this: 然后,您可以像这样使用它:

Staff staff = new Staff();
Set<String> set = Util.getColNames(staff);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM