我想生成自定义getter和setter,所以当我将这些实例保存到SQL数据库中时,我可以更好地处理变量。我想生成类似的东西:
public class Test extends SQLEntry {
private static final String NAME = "Name";
public String getName() {
return get(NAME);
}
public void setName(String name) {
set(NAME, name);
}
}
但正如我在Eclipse中看到的,它只生成以下代码:
public class Test {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
是否有插件,可以做到吗?或者我错过了什么? 我有20个课程,我不会手动编写。
答案 0 :(得分:1)
我不知道你为什么需要这个,但这里是自定义Getters和Setter的方法。 您可以转到首选项>更新所有生成的setter和getter。 java>代码风格>代码模板和选择代码然后编辑Getter body和Setter body并将其放入:
Getter body: return get($ {field});
Setter body:设置($ {field},$ {param});
如果有效,请告诉我
答案 1 :(得分:1)
我建议您使用Spring Data而不是按照您的描述进行操作。具体而言,BeanPropertyRowMapper
包中的org.springframework.jdbc.core
类将执行您想要的操作。
在Spring API documentation中阅读更多内容。
答案 2 :(得分:0)
没有其他插件可用!
某些插件如何编写特定于您的业务逻辑的代码!
你必须为所有类中的setter和getter手动编写代码!
答案 3 :(得分:0)
试试write-it-once。基于模板的代码生成器您使用Groovy编写自定义模板,并根据Java反射生成文件。这是生成任何文件的最简单方法。您可以通过生成AspectJ或java文件,基于JPA注释的SQL,基于枚举的插入/更新等来生成getters / settest / toString。
答案 4 :(得分:0)
最后,我发现最好自己动手......
如果你喜欢编写代码,那么你最喜欢这个解决方案。
public class CodeGenerator {
private final static String ENCODING = "UTF-8";
private final static String FILE_NAME = "File.txt";
public static void main(String[] args) {
try {
ArrayList<Carriage> names = getNames();
for (Carriage c : names) {
createSetter(c.name, c.capitalName);
createGetter(c.name, c.capitalName);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
private static ArrayList<Carriage> getNames() throws FileNotFoundException {
File file = new File("/");
InputStream is = CodeGenerator.class.getResourceAsStream(FILE_NAME);
Scanner s = new java.util.Scanner(is, ENCODING).useDelimiter("\\A");
String content = s.next();
String[] lines = content.split(System.getProperty("line.separator"));
ArrayList<Carriage> ret = new ArrayList<Carriage>();
for (String line : lines) {
line = line.replaceAll("\\r", "");
int firstCapitalIndex = line.indexOf("String") + 7;
int secondCapitalIndex = line.indexOf(" ", firstCapitalIndex);
int firstIndex = line.indexOf("\"") + 1;
int secondIndex = line.indexOf("\"", firstIndex + 1);
Carriage c = new Carriage();
c.name = line.substring(firstIndex, secondIndex);
c.capitalName = line.substring(firstCapitalIndex, secondCapitalIndex);
ret.add(c);
}
return ret;
}
public static void createSetter(String name, String capitalName) {
String str = "public void set" + name + "(String val) {\n"
+ "\tset(" + capitalName + ", val);\n"
+ "}\n";
System.out.println(str);
}
public static void createGetter(String name, String capitalName) {
String str = "public String get" + name + "() {\n"
+ "\treturn (String) get(" + capitalName + ");\n"
+ "}\n";
System.out.println(str);
}
滑架:
package codegenerator;
public class Carriage {
public String name;
public String capitalName;
}
对于File.txt,我只是将所有已定义的常量变为coppy并运行生成器......
public static final String NAME = "Name";
public static final String PHONE = "Phone";
public static final String EMAIL = "Email";
public static final String ADDRESS_1 = "Address1";
public static final String ADDRESS_2 = "Address2";
public static final String ADDRESS_3 = "Address3";
public static final String ICO = "Ico";
public static final String DIC = "Dic";
public static final String ADMIN_LOGIN = "AdminLogin";
public static final String ADMIN_PASSWORD = "AdminPassword";
public static final String LANGUAGE = "Language";
public static final String CODE = "CODE";
public static final String MONTHLY_PAYMENT = "MonthlyPayment";