支持联合主键的基类设计

1. 子类的主键名称不相同。

2. 基类可能支持联合主键,也可能是单一主键。

实现方法:

添加一个参数列表,来记录主键的名称,如下:

    private String[] primaryKey;

    public void sPrimaryKey(String... primaryKey){
        Assert.notEmpty(primaryKey, "'primaryKey' must not be empty");
        this.primaryKey = primaryKey;
    }
    
    public String[] gPrimaryKey() {
        return primaryKey;
    }

其中不用setter和getter,是为了避免子类与xml转换时把primaryKey也当做属性参数。这里primaryKey只是辅助基类。

我们可以通过primaryKey的值,来获取子类主键的值。如下:

    public Object[] gID() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
        int i = 0;
        if(this.primaryKey != null)
            i = this.primaryKey.length;
        Object[] id = new Object[i];
        while(i > 0){
            Field f = null;
            try{
                f = this.getClass().getDeclaredField(primaryKey[i-1]);  
            }catch (NoSuchFieldException e){
                if(this.getClass().getSuperclass() != null)
                    f = this.getClass().getSuperclass().getDeclaredField(primaryKey[i-1]);
            }
            f.setAccessible(true);  
            id[i-1] = f.get(this);
            i--;
        }
        return id;
    }
    public boolean sID(Object[] para) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        int i = 0;
        if(this.primaryKey != null)
            i = this.primaryKey.length;
        int j = 0;
        while(j < i){
            Field f = null;
            try{
                f = this.getClass().getDeclaredField(primaryKey[i-1]);  
            }catch (NoSuchFieldException e){
                if(this.getClass().getSuperclass() != null)
                    f = this.getClass().getSuperclass().getDeclaredField(primaryKey[i-1]);
            }  
            f.setAccessible(true);  
            f.set(this, para[j]);
            j++;
        }
        boolean ret =false;
        return ret;
    }

其中使用getDeclaredField来获取主键的属性,然后进行set或者get。

注意如果主键是继承自父类的,则需要从父类获取。如上的catch块。

具体使用,如下:

    public boolean delete(T entity){
        Object[] id = null;
        try {
            id = entity.gID();
        } catch (IllegalArgumentException | IllegalAccessException
                | NoSuchFieldException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i = id.length;
        String url = this.URL + id[i-1];
        i--;
        while(i > 0){
            i--;
            url +=("/"+id[i]);
        }
        
        Document dom = sendDelete(url);
        return judgeStatus(dom);
    }

    public boolean update(T entity){
        Object[] id = null;
        try {
            id = entity.gID();
        } catch (IllegalArgumentException | IllegalAccessException
                | NoSuchFieldException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i = id.length;
        String url = this.URL + id[i-1];
        i--;
        while(i > 0){
            i--;
            url +=("/"+id[i]);
        }
        Document dom = sendPut(url, entity.Object2Map());
        return judgeStatus(dom);
    }

将继承的子类属性转换成map<paraName, paraValue>的操作如下:

    public Map<String, String> Object2Map() {
        Map<String, String> params = new HashMap<String, String>();
        Object obj = null;
        String value;
        try {
            Class<?> theclass = this.getClass();
            Class<?> superclass = theclass.getSuperclass();
            Field[] fields = theclass.getDeclaredFields();
            Field[] superfields = null;
            
            if(superclass != null){
                superfields = superclass.getDeclaredFields();
            }
            Field[] allFields = Arrays.copyOf(fields,fields.length+superfields.length);
            System.arraycopy(superfields, 0, allFields, fields.length, superfields.length);
            for (Field field:allFields) {
                field.setAccessible(true);
                if (field.isAccessible()) {
                    obj = field.get(this);
                    if (obj != null) {
                        if(field.getType().getName().equals("java.util.Date")) {
                            value = MyDateFormat.DateToString(Date.class.cast(obj));
                        }
                        else {
                            value = field.getType().cast(obj)
                                    .toString();
                        }
                        params.put(field.getName(), value);
                    }
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException
                | SecurityException e) {
            Logger.loging(Level.SEVERE, "%s Exception, message: %s.
", e.getClass()
                    .getName(), e.getMessage());
            return null;
        }
        return params;
    }

从map转变为object

public boolean loadFromParams(Map<String, String> params) {
        try {
            Class<?> theclass = this.getClass();
            Class<?> superclass = theclass.getSuperclass();
            Field[] fields = theclass.getDeclaredFields();
            Field[] superfields = null;
            
            if(superclass != null){
                superfields = superclass.getDeclaredFields();
            }
            Field[] allFields = Arrays.copyOf(fields,fields.length+superfields.length);
            System.arraycopy(superfields, 0, allFields, fields.length, superfields.length);
            for (Field field:allFields) {
                field.setAccessible(true);
                if (field.isAccessible()) {
                    String s = params.get(field.getName());
                    if (s != null) {
                        if(field.getType().getName().equals("java.util.Date")) {
                            Date obj = MyDateFormat.StringToDate(s);
                            field.set(this, obj);
                        }
                        else {
                            Object obj = field.getType()
                                    .getConstructor(new Class[] { String.class })
                                    .newInstance(s);
                            field.set(this, obj);
                        }
                    }
                }
            }
        } catch (IllegalAccessException | SecurityException
                | IllegalArgumentException | InstantiationException
                | NoSuchMethodException | InvocationTargetException e) {
            Logger.loging(Level.SEVERE, "%s Exception, message: %s.
", e.getClass()
                    .getName(), e.getMessage());
            return false;
        }
        return true;
    }
原文地址:https://www.cnblogs.com/dorothychai/p/3781021.html