SessionFacade模式并不是GOF经典设计模式之一,它是一种EJB Session Bean应用在J2ee架构中的一种相对比较好的实践 。
大家对DAO一定不陌生,我们通常用Dao维护单个对象的持久化。然后再从Dao之上加一个Facade设计模式,即为Service层。这里说的Session Facade模式也和这种模式类似,只是Dao换成了EAO,EAO组件本身就是一种无状态的 Local EJB,它同样需要使用SessionBean作为其Facade,这个SessionBean可能是Local的也可以能是Remote的。
使用Session Facade模式的架构如下:
在我们现在做得项目中,这种模式的具体实现为:
ICommonEao规定了公共Eao的有方法,CommonEaoImpl是它的实现。
IBusinessEao是针对具体模块的Eao接口,它继承自公共Eao,同时又添加了自己特有的方法。
BusinessEaoImpl是IBusinessEao的具体实现,它必须继承CommonEaoImpl。
整个项目中会有多个类似BusinessEaoImpl的组件,供上层SessionBean组合,来实现业务逻辑。上层SessionBean可看做外观层,可以是Remote的,也可以是Local的,依具体业务而定。
这里给出公共Eao的实现,其他的要根据自己的系统而定。
package ejb.common.eao; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import javax.ejb.Local; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import ejb.common.eao.ICommonEao; /** * 操作数据库的基接口的实现类 * * @author */ @Stateless(name = "common_EaoBean") @Local public class CommonEaoImpl implements ICommonEao { @PersistenceContext(unitName = "EjbPersistenceUnit") private EntityManager em; /** * 查询不带参数的原生SQL * * @param sql * 原生SQL * @return 记录 * @author */ @SuppressWarnings("unchecked") @Override public <T> T getByNativeSQLQuery(String sql) { Query query = em.createNativeQuery(sql); T obj = null; List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } return obj; } /** * 查询带参数的原生SQL * * @param sql * 原生SQL * @param values * 参数数组 * @return 记录 * @author */ @SuppressWarnings("unchecked") @Override public <T> T getByNativeSQLQuery(String sql, Object[] values) { Query query = em.createNativeQuery(sql); T obj = null; if (values != null && values.length != 0) { for (int i = 0; i < values.length; i++) { query.setParameter(i + 1, values[i]); } List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } } else { List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } } return obj; } /** * 查询不带参数的原生SQL,并且把查询的结果封装成实体 * * @param sql * 原生SQL * @param entityClass * 实体 * @return 实体 * @author */ @SuppressWarnings("unchecked") @Override public <T> T getByNativeSQLQuery(String sql, Class<T> entityClass) { Query query = em.createNativeQuery(sql, entityClass); T obj = null; List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } return obj; } /** * 查询带参数的原生SQL,并且把查询的结果封装成实体 * * @param sql * 原生SQL * @param entityClass * 实体 * @return 实体 * @author */ @SuppressWarnings("unchecked") @Override public <T> T getByNativeSQLQuery(String sql, Object[] values, Class<T> entityClass) { Query query = em.createNativeQuery(sql, entityClass); T obj = null; if (values != null && values.length != 0) { for (int i = 0; i < values.length; i++) { query.setParameter(i + 1, values[i]); } List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } } else { List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { obj = (T) listResult.get(0); } } return obj; } /** * 查询不带参数的原生SQL * * @param sql * 原生SQL * @return 记录集合 * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByNativeSQLQuery(String sql) { Query query = em.createNativeQuery(sql); List<T> listResult = (List<T>) query.getResultList(); return listResult; } /** * 查询带参数的原生SQL * * @param sql * 原生SQL * @param values * 参数数组 * @return 记录集合 * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByNativeSQLQuery(String sql, Object[] values) { Query query = em.createNativeQuery(sql); List<T> listResult = new ArrayList<T>(); if (values != null && values.length != 0) { for (int i = 0; i < values.length; i++) { query.setParameter(i + 1, values[i]); } listResult = (List<T>) query.getResultList(); } else { listResult = (List<T>) query.getResultList(); } return listResult; } /** * 查询不带参数的原生SQL,并且把查询的结果封装成实体 * * @param sql * 原生SQL * @param entityClass * 实体 * @return 实体集合 * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByNativeSQLQuery(String sql, Class<T> entityClass) { Query query = em.createNativeQuery(sql, entityClass); List<T> listResult = (List<T>) query.getResultList(); return listResult; } /** * 查询带参数的原生SQL,并且把查询的结果封装成实体 * * @param sql * 原生SQL * @param entityClass * 实体 * @return 实体集合 * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByNativeSQLQuery(String sql, Object[] values, Class<T> entityClass) { Query query = em.createNativeQuery(sql, entityClass); List<T> listResult = new ArrayList<T>(); if (values != null && values.length != 0) { for (int i = 0; i < values.length; i++) { query.setParameter(i + 1, values[i]); } listResult = (List<T>) query.getResultList(); } else { listResult = (List<T>) query.getResultList(); } return listResult; } /********************************************************************************************* * * * 上面是执行原型sql的接口实现,下面是执行QL的接口实现 * * *********************************************************************************************/ /** * 查找实体 * * @param entityClass * 实体类 * @param pk * 主键 * @return 根据指定主键返回实体 * @author */ public <T> T get(Class<T> entityClass, Object pk) { T obj = em.find(entityClass, pk); return obj; } /** * 根据相应的字段查询实体类 * * @param entityClass * 实体类 * @param fields * 相应的字段数组 * @param args * 字段对应的值 * @return 根据相应字段返回的实体 * @author */ @SuppressWarnings("unchecked") public <T> T get(Class<T> entityClass, Object[] fields, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpql(fields)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { T obj = (T) listResult.get(0); return obj; } return null; } /** * 根据相应的字段查询实体类 * * @param entityClass * 实体类 * @param fields * 相应的字段数组 * @param args * 字段对应的值 * @return 根据相应字段返回的实体 * @author */ @SuppressWarnings("unchecked") public <T> T getByLike(Class<T> entityClass, Object[] fields, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLike(fields)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } List<T> listResult = (List<T>) query.getResultList(); if (listResult.size() != 0) { T obj = (T) listResult.get(0); return obj; } return null; } /** * 保存实体 * * @param entity * 需要保存的实体 * @author */ public void save(Object entity) { em.persist(entity); } /** * 更新实体 * * @param entity * 需要更新的实体 * @author */ public void update(Object entity) { em.merge(entity); } /** * 更新方法 * * @param entityClass * 实体类 * @param setJpql * 需要更新的字段,action传入的信息如下:o.name=?1 and o.password=?2 * @param whereJpql * where查询条件,比如:where o.name=?3 or o.password=?4 or o.desc=?5 * @param args * 参数值 注意:参数值,其中对应需要更新的字段,以及where查询条件 */ public <T> void update(Class<T> entityClass, String setJpql, String whereJpql, Object... args) { String entityName = entityClass.getSimpleName(); // System.out.println("update "+entityName+ " as o set " // +setJpql+" "+whereJpql); // 拼接的字符串类似 // update User as o set o.name=1? where o.name=2? Query query = em.createQuery("update " + entityName + " as o set " + setJpql + " " + whereJpql); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } query.executeUpdate(); } /** * 更新方法 * * @param entityClass * 需要更新的实体类 * @param setFields * 需要更新的字段数组,传入的信息如下:{name,id,functionId} * @param whereFields * 查询的条件数组, 传入的信息如下:{name,id,functionId} * @param args * 需要更新的字段数组和查询条件数组的参数值。 */ public <T> void update(Class<T> entityClass, Object[] setFields, Object[] whereFields, Object... args) { String entityName = entityClass.getSimpleName(); System.out.println(entityName); System.out.println("update " + entityName + " as o " + buildsetWhereJpql(setFields, whereFields)); Query query = em.createQuery("update " + entityName + " as o " + buildsetWhereJpql(setFields, whereFields)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } query.executeUpdate(); } /** * 删除实体 * * @param entityClass * 需要删除的实体 * @param pk * 需要删除的实体的主键 * @author */ public void delete(Class<?> entityClass, Object pk) { em.remove(em.getReference(entityClass, pk)); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param whereJpql * 指定查询返回的第一条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultList(Class<T> entityClass, String whereJpql, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + whereJpql + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param fields * 指定查询条件的字段数组 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultList(Class<T> entityClass, Object[] fields, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpql(fields) + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param whereJpql * 指定查询返回的第一条记录 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultList(Class<T> entityClass, String whereJpql, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + whereJpql + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param fields * 指定查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultList(Class<T> entityClass, Object[] fields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpql(fields) + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param notNullfields * 指定查询条件的不是空的字段数组 * @param nullFields * 指定查询条件的是空的字段数组 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultList(Class<T> entityClass, Object[] notNullfields, Object[] nullFields, LinkedHashMap<String, String> orderBy, Object... notNullValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByEqualsAndOtherIsNull(notNullfields, nullFields) + buildOrderby(orderBy)); for (int i = 0; i < notNullValues.length; i++) { query.setParameter(i + 1, notNullValues[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法[插入相等和不等的参数进行查询] * * @param entityClass * 实体类 * @param equalFields * 指定相等查询条件的字段数组 * @param notEqualFields * 指定不相等查询条件的字段数组 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param equalValues * 作为JPQL相等查询字符串的参数值 * @param notEqualValues * 作为JPQL不等查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultList(Class<T> entityClass, Object[] equalFields, Object[] notEqualFields, LinkedHashMap<String, String> orderBy, Object[] equalValues, Object... notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByEqualsAndOtherNotEquals(equalFields, notEqualFields) + buildOrderby(orderBy)); for (int i = 0; i < equalFields.length; i++) { query.setParameter(i + 1, equalValues[i]); } for (int i = 0; i < notEqualFields.length; i++) { query.setParameter(i + 1 + equalFields.length, notEqualValues[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法[传入模糊查询和不相等的参数值来查询] * * @param entityClass * 实体类 * @param likeFields * 指定模糊查询条件的字段数组 * @param notEqualFields * 指定不相等查询条件的字段数组 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param likeValues * 作为JPQL模糊查询字符串的参数值 * @param notEqualValues * 作为JPQL不等查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByLike(Class<T> entityClass, Object[] likeFields, Object[] notEqualFields, LinkedHashMap<String, String> orderBy, Object[] likeValues, Object... notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLikeAndOtherNotEquals(likeFields, notEqualFields) + buildOrderby(orderBy)); for (int i = 0; i < likeFields.length; i++) { query.setParameter(i + 1, likeValues[i]); } for (int i = 0; i < notEqualFields.length; i++) { query.setParameter(i + 1 + likeFields.length, notEqualValues[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法[插入相等和不等的参数进行查询] * * @param entityClass * 实体类 * @param equalFields * 指定相等查询条件的字段数组 * @param notEqualFields * 指定不相等查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param equalValues * 作为JPQL相等查询字符串的参数值 * @param notEqualValues * 作为JPQL不等查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultList(Class<T> entityClass, Object[] equalFields, Object[] notEqualFields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object[] equalValues, Object... notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByEqualsAndOtherNotEquals(equalFields, notEqualFields) + buildOrderby(orderBy)); for (int i = 0; i < equalFields.length; i++) { query.setParameter(i + 1, equalValues[i]); } for (int i = 0; i < notEqualFields.length; i++) { query.setParameter(i + 1 + equalFields.length, notEqualValues[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法[传入模糊查询和不相等的参数值来查询] * * @param entityClass * 实体类 * @param likeFields * 指定模糊查询条件的字段数组 * @param notEqualFields * 指定不相等查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param likeValues * 作为JPQL模糊查询字符串的参数值 * @param notEqualValues * 作为JPQL不等查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByLike(Class<T> entityClass, Object[] likeFields, Object[] notEqualFields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object[] likeValues, Object... notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLikeAndOtherNotEquals(likeFields, notEqualFields) + buildOrderby(orderBy)); for (int i = 0; i < likeFields.length; i++) { query.setParameter(i + 1, likeValues[i]); } for (int i = 0; i < notEqualFields.length; i++) { query.setParameter(i + 1 + likeFields.length, notEqualValues[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param fields * 指定模糊查询条件的字段数组 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultListByLike(Class<T> entityClass, Object[] fields, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLike(fields) + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param fields * 指定模糊查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param args * 作为JPQL查询字符串的参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") public <T> List<T> getResultListByLike(Class<T> entityClass, Object[] fields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLike(fields) + buildOrderby(orderBy)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法 * * @param entityClass * 实体类 * @param likeFields * 指定模糊查询条件的字段数组 * @param equalFields * 指定相等的查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param equalValues * 作为JPQL查询字符串的相等的参数值 * @param likeValues * 作为JPQL查询字符串的模糊参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByLikeAndEquals(Class<T> entityClass, Object[] likeFields, Object[] equalFields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object[] equalValues, Object... likeValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLikeAndEqual(likeFields, equalFields) + buildOrderby(orderBy)); for (int i = 0; i < likeValues.length; i++) { query.setParameter(i + 1, likeValues[i]); } for (int i = 0; i < equalValues.length; i++) { query.setParameter(i + 1 + likeValues.length, equalValues[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 执行查询的方法[用于判断某些具体参数是不是null的情况] * * @param entityClass * 实体类 * @param likeFields * 指定模糊查询条件的字段数组 * @param equalFields * 指定相等的查询条件的字段数组 * @param firstResult * 指定查询返回的第一条记录 * @param maxResult * 设置查询最多返回多少条记录 * @param orderBy * 用于排序,如果无须排序,该参数设为null,Map 对象的key为实体对象的字段名,value为ASC/DESC. 例如 * LinkedHashMap<String,String> orderBy=new * LinkedHashMap<String,String>(); * orderBy.put("itemName","DESC");表明根据itemName降序排列 * 如果放入多个key-value值,则第一次放入的为首要排序关键字,第二次放入的为次要关键字... * @param equalValues * 作为JPQL查询字符串的相等的参数值 * @param likeValues * 作为JPQL查询字符串的模糊参数值 * @return 返回查询得到的实体List * @author */ @SuppressWarnings("unchecked") @Override public <T> List<T> getResultListByLikeAndEquals(Class<T> entityClass, Object[] likeFields, Object[] equalFields, int firstResult, int maxResult, LinkedHashMap<String, String> orderBy, Object... likeValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select o from " + entityName + " as o " + buildWhereJpqlByLikeAndEqualsIsNull(likeFields, equalFields) + buildOrderby(orderBy)); for (int i = 0; i < likeValues.length; i++) { query.setParameter(i + 1, likeValues[i]); } query.setMaxResults(maxResult).setFirstResult(firstResult); return (List<T>) query.getResultList(); } /** * 将hashmap中的数据拼成字符串 * * @param orderBy * LinkedHashMap对象,每个key-value为一个排序的条件 * @return 排序条件字符串 * @author */ private String buildOrderby(LinkedHashMap<String, String> orderBy) { StringBuffer out = new StringBuffer(""); if (orderBy != null && orderBy.size() > 0) { out.append(" order by "); for (String key : orderBy.keySet()) { out.append("o." + key + " " + orderBy.get(key)); out.append(","); } out.deleteCharAt(out.length() - 1); } return out.toString(); } /** * 拼接查询字符串 * * @param fields * 相应的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpql(Object[] fields) { StringBuffer out = new StringBuffer(""); if (fields != null && fields.length > 0) { out.append(" where "); for (int index = 0; index < fields.length; index++) { out.append(" o." + fields[index] + "=?" + (index + 1) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); } return out.toString(); } /** * 拼接查询字符串[用于判断所传入的值是否为null] * * @param fields * 相应的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlIsNull(Object[] fields) { StringBuffer out = new StringBuffer(""); if (fields != null && fields.length > 0) { out.append(" where "); for (int index = 0; index < fields.length; index++) { out.append(" o." + fields[index] + " is null "); out.append("and"); } out.delete(out.length() - 3, out.length()); } return out.toString(); } /** * 拼接set字段和where字段的hql语句 * * @param fields1 * set字段 * @param fields2 * where字段 * @return */ private String buildsetWhereJpql(Object[] fields1, Object[] fields2) { StringBuffer out = new StringBuffer(""); if (fields1 != null && fields1.length > 0) { out.append(" set "); for (int index = 0; index < fields1.length; index++) { out.append(" o." + fields1[index] + "=?" + (index + 1) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); out.append(" where "); for (int index = 0; index < (fields2.length); index++) { out.append(" o." + fields2[index] + "=?" + (fields1.length + (index + 1)) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); } return out.toString(); } /** * 拼接查询字符串 * * @param fields * 相应的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByLike(Object[] fields) { StringBuffer out = new StringBuffer(""); if (fields != null && fields.length > 0) { out.append(" where "); for (int index = 0; index < fields.length; index++) { out.append(" o." + fields[index] + " like ?" + (index + 1) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); } return out.toString(); } /** * 拼接查询字符串 * * @param notEquals * 相应的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlNotEqual(Object[] notEquals) { StringBuffer out = new StringBuffer(""); if (notEquals != null && notEquals.length > 0) { out.append(" where "); for (int index = 0; index < notEquals.length; index++) { out.append(" o." + notEquals[index] + "<>?" + (index + 1) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); } return out.toString(); } /** * 拼接查询字符串[用于判断某些参数相似有些参数不等情况] * * @param likeFields * 模糊字段数组 * @param notEqualFields * 不具体字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByLikeAndOtherNotEquals(Object[] likeFields, Object[] notEqualFields) { String result = ""; if ((likeFields != null && likeFields.length > 0) && (notEqualFields == null || notEqualFields.length == 0)) { result = buildWhereJpqlByLike(likeFields); } else if ((likeFields == null || likeFields.length == 0) && (notEqualFields != null && notEqualFields.length > 0)) { result = buildWhereJpqlNotEqual(notEqualFields); } else if ((likeFields == null || likeFields.length == 0) && (notEqualFields == null || notEqualFields.length == 0)) { result = ""; } else { StringBuffer out = new StringBuffer(""); out.append(" where "); for (int index = 0; index < likeFields.length; index++) { out.append(" o." + likeFields[index] + " like ?" + (index + 1) + " "); out.append("and"); } for (int index = 0; index < notEqualFields.length; index++) { out.append(" o." + notEqualFields[index] + " <> ?" + (index + 1 + likeFields.length) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); result = out.toString(); } return result; } /** * 拼接查询字符串[用于判断某些参数相等有些参数不想等的情况] * * @param equalFields * 相等字段数组 * @param notEqualFields * 不相等字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByEqualsAndOtherNotEquals( Object[] equalFields, Object[] notEqualFields) { String result = ""; if ((equalFields != null && equalFields.length > 0) && (notEqualFields == null || notEqualFields.length == 0)) { result = buildWhereJpql(equalFields); } else if ((equalFields == null || equalFields.length == 0) && (notEqualFields != null && notEqualFields.length > 0)) { result = buildWhereJpqlNotEqual(notEqualFields); } else if ((equalFields == null || equalFields.length == 0) && (notEqualFields == null || notEqualFields.length == 0)) { result = ""; } else { StringBuffer out = new StringBuffer(""); out.append(" where "); for (int index = 0; index < equalFields.length; index++) { out.append(" o." + equalFields[index] + " = ?" + (index + 1) + " "); out.append("and"); } for (int index = 0; index < notEqualFields.length; index++) { out.append(" o." + notEqualFields[index] + " <> ?" + (index + 1 + equalFields.length) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); result = out.toString(); } return result; } /** * 拼接查询字符串[用于判断某些具体参数是不是null的情况] * * @param likeFields * 模糊字段数组 * @param equalFields * 具体字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByLikeAndEqualsIsNull(Object[] likeFields, Object[] equalFields) { String result = ""; if ((likeFields != null && likeFields.length > 0) && (equalFields == null || equalFields.length == 0)) { result = buildWhereJpqlByLike(likeFields); } else if ((likeFields == null || likeFields.length == 0) && (equalFields != null && equalFields.length > 0)) { result = buildWhereJpqlIsNull(equalFields); } else if ((equalFields == null || equalFields.length == 0) && (likeFields == null || likeFields.length == 0)) { result = ""; } else { StringBuffer out = new StringBuffer(""); out.append(" where "); for (int index = 0; index < likeFields.length; index++) { out.append(" o." + likeFields[index] + " like ?" + (index + 1) + " "); out.append("and"); } for (int index = 0; index < equalFields.length; index++) { out.append(" o." + equalFields[index] + " is null "); out.append("and"); } out.delete(out.length() - 3, out.length()); result = out.toString(); } return result; } /** * 拼接查询字符串[某些参数不为null,某些参数为null的情况] * * @param notNullfields * 不为空的字段数组 * * @param nullFields * 为空的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByEqualsAndOtherIsNull(Object[] notNullfields, Object[] nullFields) { String result = ""; if ((notNullfields != null && notNullfields.length > 0) && (nullFields == null || nullFields.length == 0)) { result = buildWhereJpql(notNullfields); } else if ((notNullfields == null || notNullfields.length == 0) && (nullFields != null && nullFields.length > 0)) { result = buildWhereJpqlIsNull(nullFields); } else if ((notNullfields == null || notNullfields.length == 0) && (nullFields == null || nullFields.length == 0)) { result = ""; } else { StringBuffer out = new StringBuffer(""); out.append(" where "); for (int index = 0; index < notNullfields.length; index++) { out.append(" o." + notNullfields[index] + "=?" + (index + 1) + " "); out.append("and"); } for (int index = 0; index < nullFields.length; index++) { out.append(" o." + nullFields[index] + " is null "); out.append("and"); } out.delete(out.length() - 3, out.length()); result = out.toString(); } return result; } /** * 拼接查询字符串 * * @param fields * 相应的字段数组 * @return 拼接查询的字符串 * @author */ private String buildWhereJpqlByLikeAndEqual(Object[] likeFields, Object[] equalFields) { String result = ""; if ((likeFields != null && likeFields.length > 0) && (equalFields == null || equalFields.length == 0)) { result = buildWhereJpqlByLike(likeFields); } else if ((likeFields == null || likeFields.length == 0) && (equalFields != null && equalFields.length > 0)) { result = buildWhereJpql(equalFields); } else if ((equalFields == null || equalFields.length == 0) && (likeFields == null || likeFields.length == 0)) { result = ""; } else { StringBuffer out = new StringBuffer(""); out.append(" where "); for (int index = 0; index < likeFields.length; index++) { out.append(" o." + likeFields[index] + " like ?" + (index + 1) + " "); out.append("and"); } for (int index = 0; index < equalFields.length; index++) { out.append(" o." + equalFields[index] + "=?" + (index + 1 + likeFields.length) + " "); out.append("and"); } out.delete(out.length() - 3, out.length()); result = out.toString(); } return result; } /** * 查询数据总数 * * @param entityClass * 实体类 * @param whereJpql * 查询条件 * @param args * 查询参数 * @return 数据总数 * @author */ public <T> Long getTotalCount(Class<T> entityClass, Object[] fields, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + buildWhereJpqlByLike(fields)); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } return (Long) query.getResultList().get(0); } /** * 查询数据总数 * * @param entityClass * 实体类 * @param whereJpql * 查询条件 * @param args * 查询参数 * @return 数据总数 * @author */ @Override public <T> Long getTotalCount(Class<T> entityClass, String whereJpql, Object... args) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + whereJpql); for (int i = 0; i < args.length; i++) { query.setParameter(i + 1, args[i]); } return (Long) query.getResultList().get(0); } /** * 获得查询数据总数 * * @param entityClass * 实体类 * @param likeFields * 模糊查询条件 * @param equalFields * 等于查询条件 * @param equalValues * 等于查询的值 * @param likeValues * 模糊查询的值 * @return 数据条数 * @author */ @Override public <T> Long getTotalCount(Class<T> entityClass, Object[] likeFields, Object[] equalFields, Object[] equalValues, Object... likeValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + buildWhereJpqlByLikeAndEqual(likeFields, equalFields)); for (int i = 0; i < likeValues.length; i++) { query.setParameter(i + 1, likeValues[i]); } for (int i = 0; i < equalValues.length; i++) { query.setParameter(i + 1 + likeValues.length, equalValues[i]); } return (Long) query.getResultList().get(0); } /** * 获得查询数据总数[用于判断某些具体参数是不是null的情况] * * @param entityClass * 实体类 * @param likeFields * 模糊查询条件 * @param equalFields * 等于查询条件 * @param equalValues * 等于查询的值 * @param likeValues * 模糊查询的值 * @return 数据条数 * @author */ @Override public <T> Long getTotalCount(Class<T> entityClass, Object[] likeFields, Object[] equalFields, Object... likeValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + buildWhereJpqlByLikeAndEqualsIsNull(likeFields, equalFields)); for (int i = 0; i < likeValues.length; i++) { query.setParameter(i + 1, likeValues[i]); } return (Long) query.getResultList().get(0); } /** * 获得查询数据总数[用于判断某些参数相等有些参数不想等的情况] * * @param entityClass * 实体类 * @param equalFields * 查询相等条件 * @param notEqualFields * 不等于查询条件 * @param equalValues * 等于查询的值 * @param notEqualValues * 不等于查询的值 * @return 数据条数 * @author */ @Override public <T> Long getTotalCountByEqualAndNotEqual(Class<T> entityClass, Object[] equalFields, Object[] notEqualFields, Object[] equalValues, Object... notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + buildWhereJpqlByEqualsAndOtherNotEquals(equalFields, notEqualFields)); for (int i = 0; i < equalValues.length; i++) { query.setParameter(i + 1, equalValues[i]); } for (int i = 0; i < notEqualValues.length; i++) { query.setParameter(i + 1 + equalValues.length, notEqualValues[i]); } return (Long) query.getResultList().get(0); } /** * 获得查询数据总数[用于判断某些参数相似某些参数不等的情况] * * @param entityClass * 实体类 * @param likeFields * 模糊查询条件 * @param notEqualFields * 不等于查询条件 * @param likeValues * 模糊查询的值 * @param notEqualValues * 不等于查询的值 * @return 数据条数 * @author */ @Override public <T> Long getTotalCountByLikeAndNotEqual(Class<T> entityClass, Object[] likeFields, Object[] notEqualFields, Object[] likeValues, Object[] notEqualValues) { String entityName = entityClass.getSimpleName(); Query query = em.createQuery("select count(o) from " + entityName + " as o " + buildWhereJpqlByLikeAndOtherNotEquals(likeFields, notEqualFields)); for (int i = 0; i < likeValues.length; i++) { query.setParameter(i + 1, likeValues[i]); } for (int i = 0; i < notEqualValues.length; i++) { query.setParameter(i + 1 + likeValues.length, notEqualValues[i]); } return (Long) query.getResultList().get(0); } }