多表查询(多对一)结果集处理方法(反射)

//T多表查询中的多 如:学生
//M多表查询中的一 如:年级
//field学生类中年级对象的属性名
//field2年级类在学生集合的属性名
//resultSet多表查询得到的结果集
方法体

 public static  <T, M> T test(Class<T> tClass, Class<M> mClass, String field, String field2, ResultSet resultSet) {
        Field[] tFields = tClass.getDeclaredFields();
        Method[] tMethods = tClass.getMethods();
        Field[] mFields = mClass.getDeclaredFields();
        Method[] mMethods = mClass.getMethods();
        T t = null;
        M m = null;
        try {
            m = mClass.newInstance();
            for (Field mField : mFields) {
                for (Method mMethod : mMethods) {
                    if (mMethod.getName().startsWith("set")) {
                        String mFieldByMethodName = mMethod.getName().substring(3, mMethod.getName().length()).toLowerCase();
                        if (mFieldByMethodName.equals(field)) {

                        } else {
                            if (mFieldByMethodName.equals(mField.getName())) {
                                mMethod.invoke(m, resultSet.getObject(mField.getName()));
                            }
                        }
                    }
                }
            }
            t = tClass.newInstance();
            for (Field tField : tFields) {
                for (Method tMethod : tMethods) {
                    if (tMethod.getName().startsWith("set")) {
                        String tFieldByMethodName = tMethod.getName().substring(3, tMethod.getName().length()).toLowerCase();
                        if (tFieldByMethodName.equals(tField.getName())) {
                            if (tFieldByMethodName.equals(field2)) {

                            } else {
                                tMethod.invoke(t, resultSet.getObject(tField.getName()));
                            }
                        }
                        if (tFieldByMethodName.equals(field2)) {
                            tMethod.invoke(t, m);
                        }
                    }
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return t;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值