猿问

根据 ResultSetMetaData 中的列数在 Setter 中设置值

我正在寻找动态方法来设置 setter 方法中的值,在该方法中,我从 中获取列的总数ResultSetMetaData,基于这些结果,我想在相应的 setter 方法中设置值。我已经在我的实用程序类中准备了代码片段。


public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {

    ResultSetMetaData rsmd = rs.getMetaData();

    int columnCount = rsmd.getColumnCount();

    for(int i = 1 ; i <= columnCount ; i++){

        SQLColumn column = new SQLColumn();

        String columnName = rsmd.getColumnName(i);

    }

    return columnCount;

}

现在的场景是我想设置Pay_codes员工,现在总共有 95 个工资代码,但在员工的情况下有所不同。


例如,普通员工有 13 个工资代码,而联系员工有 6 个代码,同样社会员工有 8 个工资代码。


现在我需要代表员工显示工资代码列表。我通过了EmployeeCode,我得到了结果。


现在的问题

是我是否在我的 RowMapper 中设置了所有列,如下所示


Paycode pc=new PayCode();

if(rs.getString("ESIC") != null){  

    pc.setESICCode(rs.getString("ESIC"));

}

if(rs.getString("EPF") != null){  

    pc.setEPFCode(rs.getString("EPF"));

}

if(rs.getString("Allowance") != null){  

    pc.setAllowance(rs.getString("Allowance"));

}

and many more columns........till 300+ lines

这看起来很糟糕,因为可以根据客户的要求增加或减少 Paycode。


所以,我正在寻找 2-3 行动态代码,它根据“ResultSetMetaData”中的列名在 setter 中设置值。所以请建议我最好的方法并帮助我实现相同的目标。


但是我正在考虑泛型实现,泛型是否有一些魔力,如果是> 那么如何?请建议我。


米脂
浏览 112回答 2
2回答

肥皂起泡泡

使用此代码:private void mapFields(PayCode p, String setterName, Object value) {&nbsp; &nbsp; Method[] methods = PayCode.class.getDeclaredMethods();&nbsp; &nbsp; for(Method&nbsp; method: methods){&nbsp; &nbsp; &nbsp; &nbsp; if(method.getName().contains(setterName)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.invoke(p, value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InvocationTargetException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}因此,您使用 Java 反射 API withPayCode.class.getDeclaredMethods来获取类方法,然后遍历方法名称,搜索包含属性名称的方法。一旦找到它,就用 将值设置为对象method.invoke(p, value),然后退出循环。如果您存储PayCode.class.getDeclaredMethods(),然后使用方法名称作为键,方法作为值创建一个散列集,则可以使事情变得更快Map<String, Method>:static Map<String, Method> mapFields(Class clazz) {&nbsp; &nbsp; Method[] methods = clazz.getDeclaredMethods();&nbsp; &nbsp; Map<String, Method> result = new HashMap<>();&nbsp; &nbsp; for(Method&nbsp; method: methods){&nbsp; &nbsp; &nbsp; &nbsp; String methodName = method.getName();&nbsp; &nbsp; &nbsp; &nbsp; if(methodName.indexOf("set") == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.put(methodName.substring(3), method);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}然后使用它:Map<String, Method> fields = mapFields(PayCode.class);if(fields.containsKey(name)){&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; fields.get(name).invoke(p, value);&nbsp; &nbsp; } catch (IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } catch (InvocationTargetException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}

慕码人8056858

你可以使用反射package com.test;import java.lang.reflect.Method;class PayCode {&nbsp; &nbsp; private String esic;&nbsp; &nbsp; private String epf;&nbsp; &nbsp; public String getEsic() {&nbsp; &nbsp; &nbsp; &nbsp; return esic;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEsic(String esic) {&nbsp; &nbsp; &nbsp; &nbsp; this.esic = esic;&nbsp; &nbsp; }&nbsp; &nbsp; public String getEpf() {&nbsp; &nbsp; &nbsp; &nbsp; return epf;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEpf(String epf) {&nbsp; &nbsp; &nbsp; &nbsp; this.epf = epf;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "PayCode [esic=" + esic + ", epf=" + epf + "]";&nbsp; &nbsp; }}public class DynamicSetter {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; PayCode payCode = new PayCode();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Here you can take "set" hardcoded and generate suffix "Esic"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* dynamically, may be using apache StringUtils or implement using&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Substring, based on your class setter methods.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method esicMethod = PayCode.class.getMethod("set" + "Esic", String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; esicMethod.invoke(payCode, "Test Paycode");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Check paycode in object : " + payCode);&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchMethodException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您可以使用 HashMap<> 在其中添加所有支付代码。您还可以为每个支付代码实现 gtter 以从 HashMap 中读取。package com.test;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;class Employee {&nbsp; &nbsp; private Map<String, String> payCodes = new HashMap<>();&nbsp; &nbsp; private String epf;&nbsp; &nbsp; public String getEsic() {&nbsp; &nbsp; &nbsp; &nbsp; return payCodes.get("ESIC");&nbsp; &nbsp; }&nbsp; &nbsp; public String getEpf() {&nbsp; &nbsp; &nbsp; &nbsp; return payCodes.get("EPF");&nbsp; &nbsp; }&nbsp; &nbsp; public Map<String, String> getPayCodes() {&nbsp; &nbsp; &nbsp; &nbsp; return payCodes;&nbsp; &nbsp; }}public class DynamicSetter {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; }&nbsp; &nbsp; public static Integer extractData(ResultSet rs) throws SQLException, DataAccessException {&nbsp; &nbsp; &nbsp; &nbsp; while(rs.next()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Employee e = new Employee();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> payCodes = e.getPayCodes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set app paycode in payCodes, you can use column name as key and fetch value from result set.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答