我决定在我的抽象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);
}
}
我用这些假设写了它:
您如何看待这段代码?有更好的体验吗? 如果您发现任何错误或类似的情况,请告诉我。
答案 0 :(得分:0)
我的最终版本代码解决了这个问题,我希望它也可以帮到你。
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);
}
}