首页 > 代码库 > 通过Java反射做实体查询

通过Java反射做实体查询

我们在使用hibernate的时候,查询的时候都会和实体中的一些字段相结合去查询,当然字段少了,还算是比较简单,当字段多了,就不那么容易了,所以就自己写了个方法,根据实体中的字段信息去查询,废话不多说上代码:

/**     * 根据实体查询,将所有的参数封装到实体中查询即可,不可查询为null的信息     * @param t     * @return     * @throws SecurityException     * @throws NoSuchMethodException     * @throws IllegalArgumentException     * @throws IllegalAccessException     * @throws InvocationTargetException     */    public List<T> queryAll(T t) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{        //存放参数        List<Object> paraList = new ArrayList<Object>();        List<T> list = new ArrayList<T>();        StringBuffer sb = new StringBuffer("from ");        sb.append(entityClass.getSimpleName()).append(" where 1=1 ");        if( t == null){            list = find(sb.toString());            return list;        }        //拼接hql语句        //获取类中所有的属性        Field[] fields = t.getClass().getDeclaredFields();        for(Field field : fields){            //字段名称            String fieldName = field.getName();            //方法名称            String methodName = getMethodName(fieldName, "get");            //方法            Method method = UserInfo.class.getDeclaredMethod(methodName);            //方法的返回值类型            String methodReturnType = method.getReturnType().getSimpleName();            Object obj = null;            obj = method.invoke(t);            //返回值为空则跳过            if( obj == null ){                continue;            }            if ("String".equals(methodReturnType)) {                String str = String.valueOf(obj.toString());                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(str);            } else if ("Date".equals(methodReturnType)) {                Date date = (Date)obj;                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(date);            } else if ("Integer".equals(methodReturnType)                    || "int".equals(methodReturnType)) {                Integer i = Integer.valueOf(obj.toString());                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(i);            } else if ("Long".equalsIgnoreCase(methodReturnType)) {                Long L = Long.valueOf(obj.toString());                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(L);            } else if ("Double".equalsIgnoreCase(methodReturnType)) {                Double d = Double.valueOf(obj.toString());                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(d);            } else if ("Boolean".equalsIgnoreCase(methodReturnType)) {                Boolean b = Boolean.valueOf(obj.toString());                sb.append(" and ").append(fieldName).append("= ? ");                paraList.add(b);            }        }        Query query = createQuery(sb.toString(), paraList.toArray());        list = query.list();        return list;    }        /**     * 或实体中属性的get或set方法     * @param methodName  字段名     * @param methodType  方法类型:get或set     * @return     */    private String getMethodName(String methodName, String methodType){        if( methodName == null || "".equals(methodName.trim()) ){            return null;        }        StringBuffer sb = new StringBuffer();        sb.append(methodType);        sb.append(methodName.substring(0, 1).toUpperCase());        sb.append(methodName.substring(1));        return sb.toString();    }

本人反射学得比较浅显,希望高手能够多多指点。