JPQL 中的 FindByExample

FindByExample in JPQL

我决定在我的抽象 DAO 中编写一个通用的 findByExample 方法。

我的结果是:

public List<T> findByExample(T example) throws DAOException {
    try {
        Object object = example;
        String query = "SELECT e from " + object.getClass().getName() + " e where 1 = 1";
        for (Field field : object.getClass().getDeclaredFields()) {
            Object fieldValue;
            field.setAccessible(true);
            if (field.get(object) instanceof List || (java.lang.reflect.Modifier.isStatic(field.getModifiers()))) {
                continue;
            } else {
                fieldValue = field.get(object);
                if (fieldValue != null) {
                    if((fieldValue instanceof String) && !((String)fieldValue).isEmpty())
                    {
                        query += " and e." + field.getName() + " LIKE :" + field.getName();
                    }
                    else
                        query += " and e." + field.getName() + " = :" + field.getName();
                }
            }
        }

        Query q = em.createQuery(query, object.getClass());

        for (Field field : object.getClass().getDeclaredFields()) {
            Object fieldValue;
            field.setAccessible(true);
            if (field.get(object) instanceof List || (java.lang.reflect.Modifier.isStatic(field.getModifiers()))) {
                continue;
            } else {
                fieldValue = field.get(object);
                if (fieldValue != null) {
                    if((fieldValue instanceof String) && !((String)fieldValue).isEmpty())
                    {
                        q.setParameter(field.getName(), "%" + field.get(object) + "%");
                    }
                    else
                        q.setParameter(field.getName(), field.get(object));
                }
            }
        }
        return q.getResultList();

    } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {

        throw new DAOException("", e);
    }
}

我用这些假设编写了它:

  1. 我的查询将搜索双向字符串,例如搜索我的 字符串字段
  2. 我实体的所有声明字段都对我有效 查询。

您如何看待这段代码?有更好的体验吗? 如果您发现任何错误或类似问题,也请告诉我。

我解决这个问题的最终版本代码如下,希望对你也有帮助。

public List<T> findByExample(T example, Map<String, SortOrder> sortMap, int startIndex, int pageSize) throws DAOException {
    try {
        Object object = example;
        String query = "SELECT e from " + object.getClass().getName() + " e where 1 = 1";
        if(!(object instanceof BaseEntity))
        {
            throw new DAOException(null, null);
        }
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            Object fieldValue;
            field.setAccessible(true);
            if (field.get(object) instanceof List || (java.lang.reflect.Modifier.isStatic(field.getModifiers())) || field.getName().startsWith("_")) {
                continue;
            } else {
                fieldValue = field.get(object);
                if (fieldValue != null) {
                    if ((fieldValue instanceof String) && !((String) fieldValue).isEmpty()) {
                        query += " and e." + field.getName() + " LIKE :" + field.getName();
                    } else {
                        query += " and e." + field.getName() + " = :" + field.getName();
                    }
                }
            }
        }
        if(sortMap != null && !sortMap.isEmpty())
        {
            for(Entry<String, SortOrder> entry : sortMap.entrySet())
            {
                query += " ORDER BY e." + entry.getKey() + " " + entry.getValue().name();
            }
        }
        Query q = em.createQuery(query, object.getClass());

        for (Field field : fields) {
            Object fieldValue;
            field.setAccessible(true);
            if (field.get(object) instanceof List || (java.lang.reflect.Modifier.isStatic(field.getModifiers())) || field.getName().startsWith("_")) {
                continue;
            } else {
                fieldValue = field.get(object);
                if (fieldValue != null) {
                    if ((fieldValue instanceof String) && !((String) fieldValue).isEmpty()) {
                        q.setParameter(field.getName(), "%" + field.get(object) + "%");
                    } else {
                        q.setParameter(field.getName(), field.get(object));
                    }
                }
            }
        }
        return q.setFirstResult(startIndex).setMaxResults(pageSize).getResultList();

    } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {

        throw new DAOException(null, e);
    }
}