| | |
| | | * @since JDK1.6 |
| | | * @history 2010-07-28 夏德虎 新建 |
| | | */ |
| | | @Transactional(readOnly=false) |
| | | public class BaseDAO extends HibernateDaoSupport { |
| | | |
| | | /** 注释 rawtypes */ |
| | | private static final String RAW_TYPES = "rawtypes"; |
| | | |
| | | |
| | | /** 注释 rawtypes */ |
| | | private static final String RAW_TYPES = "rawtypes"; |
| | | |
| | | /** |
| | | * 注入sessionFactory |
| | | * |
| | |
| | | * @return 查找到的结果,如果没找到,返回null |
| | | */ |
| | | public <T> T read(Class<T> clz, Serializable key) { |
| | | if(key==null){ |
| | | return null; |
| | | } |
| | | if(key==null){ |
| | | return null; |
| | | } |
| | | return clz.cast(this.getHibernateTemplate().get(clz, key)); |
| | | } |
| | | |
| | |
| | | public void update(Object obj) { |
| | | this.getHibernateTemplate().update(obj); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 保存对象 |
| | | */ |
| | |
| | | */ |
| | | public void saveOrUpdateAll(@SuppressWarnings(RAW_TYPES) Collection collection) { |
| | | for(Object obj:collection){ |
| | | this.getHibernateTemplate().saveOrUpdate(obj); |
| | | this.getHibernateTemplate().saveOrUpdate(obj); |
| | | } |
| | | } |
| | | |
| | |
| | | public void deleteAll(@SuppressWarnings(RAW_TYPES) Collection col) { |
| | | this.getHibernateTemplate().deleteAll(col); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public <T> List<T> find(String hql, Class<T> cls) { |
| | | public <T> List<T> find(String hql, Class<T> cls) { |
| | | return find(hql,Collections.EMPTY_LIST,cls); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | public <T> List<T> find(final String hql,final List<Object> args,final Class<T> cls) { |
| | | return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { |
| | | public <T> List<T> find(final String hql,final List<Object> args,final Class<T> cls) { |
| | | return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public List<T> doInHibernate(Session session) { |
| | | public List<T> doInHibernate(Session session) { |
| | | Query query = session.createQuery(hql); |
| | | int i = 0; |
| | | for (Object arg : args) { |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表 |
| | | * |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public List<Object[]> findwithRawResult(String hql, List<Object> args) { |
| | | @SuppressWarnings("unchecked") |
| | | public List<Object[]> findwithRawResult(String hql, List<Object> args) { |
| | | return (List<Object[]>) this.getHibernateTemplate().find(hql, args.toArray()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,返回列表数据总数 |
| | | * |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | public int findCount(final String hql, final List<Object> args) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | public int findCount(final String hql, final List<Object> args) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session){ |
| | | |
| | | Query query = session.createQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | int i = 0; |
| | | for (Object arg : args) { |
| | | |
| | | Query query = session.createQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | int i = 0; |
| | | for (Object arg : args) { |
| | | query.setParameter(i++, arg); |
| | | } |
| | | |
| | | if(null != query.uniqueResult()){ |
| | | return ((Long)query.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | |
| | | if(null != query.uniqueResult()){ |
| | | return ((Long)query.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | |
| | | /** |
| | | * 根据hql查询,返回列表数据总数 |
| | | * |
| | | * @param hql 查询语句 |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | public int findCountBySql(final String sql, final List<Object> args) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | public int findCountBySql(final String sql, final List<Object> args) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session){ |
| | | |
| | | Query query = session.createSQLQuery(sql.trim().startsWith("from")?"select count(1) ".concat(sql):sql); |
| | | int i = 0; |
| | | for (Object arg : args) { |
| | | |
| | | Query query = session.createSQLQuery(sql.trim().startsWith("from")?"select count(1) ".concat(sql):sql); |
| | | int i = 0; |
| | | for (Object arg : args) { |
| | | query.setParameter(i++, arg); |
| | | } |
| | | |
| | | if(null != query.uniqueResult()){ |
| | | return ((BigInteger)query.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | |
| | | if(null != query.uniqueResult()){ |
| | | return ((BigInteger)query.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,返回列表分页数据 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | @SuppressWarnings({ "unchecked" }) |
| | | public <T> List<T> findList(final String hql, final Pager page, final List<Object> args,Class<T> cls) { |
| | | public <T> List<T> findList(final String hql, final Pager page, final List<Object> args,Class<T> cls) { |
| | | return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { |
| | | |
| | | public List<T> doInHibernate(Session session) { |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,返回唯一的数据 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | @SuppressWarnings({ RAW_TYPES, "unchecked" }) |
| | | public <T> T findUnique(final String hql, final List<Object> args,Class<T> cls) { |
| | | public <T> T findUnique(final String hql, final List<Object> args,Class<T> cls) { |
| | | return (T)this.getHibernateTemplate().execute(new HibernateCallback() { |
| | | |
| | | public Object doInHibernate(Session session) { |
| | |
| | | |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | query.setParameter(i++, arg); |
| | | } |
| | | query.setFirstResult(pager.getPageSize()*(pager.getPageNum()-1)); |
| | | query.setMaxResults(pager.getPageSize()); |
| | | query.setMaxResults(pager.getPageSize()); |
| | | return query.list(); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * hql只含单一参数 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public Result bulkUpdateInLoop(String hql, Object args[]) { |
| | | int iCount = 0; |
| | | int iCount = 0; |
| | | for(Object arg:args){ |
| | | iCount += getHibernateTemplate().bulkUpdate(hql, arg); |
| | | iCount += getHibernateTemplate().bulkUpdate(hql, arg); |
| | | } |
| | | Map<String,Object> attrs = new HashMap<String,Object>(1); |
| | | attrs.put("doCount", iCount); |
| | | Result result = new Result(true); |
| | | result.setData(attrs); |
| | | return result; |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * hql只含单一参数 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public Result bulkUpdate(String hql, Object args[]) { |
| | | int count = getHibernateTemplate().bulkUpdate(hql, args); |
| | | int count = getHibernateTemplate().bulkUpdate(hql, args); |
| | | Map<String,Object> attrs = new HashMap<String,Object>(1); |
| | | attrs.put("doCount", count); |
| | | Result result = new Result(true); |
| | | result.setData(attrs); |
| | | return result; |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 存储过程语句 |
| | | * |
| | |
| | | * @param args 参数数组 |
| | | * @return |
| | | */ |
| | | public Result executeProduce(final String sql, final Object args[]) { |
| | | |
| | | Integer result = getHibernateTemplate().execute(new HibernateCallback<Integer>(){ |
| | | public Result executeProduce(final String sql, final Object args[]) { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session) |
| | | throws HibernateException { |
| | | |
| | | Query query = session.createSQLQuery("{ "+sql+" }"); |
| | | for(int i=0;i<args.length;i++){ |
| | | query.setParameter(i, args[i]); |
| | | } |
| | | Integer result = getHibernateTemplate().execute(new HibernateCallback<Integer>(){ |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session) |
| | | throws HibernateException { |
| | | |
| | | Query query = session.createSQLQuery("{ "+sql+" }"); |
| | | for(int i=0;i<args.length;i++){ |
| | | query.setParameter(i, args[i]); |
| | | } |
| | | return query.executeUpdate(); |
| | | } |
| | | |
| | | } |
| | | |
| | | }); |
| | | return new Result(true,String.valueOf(result)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 执行sql删除 |
| | | * |
| | |
| | | * @param args 参数数组 |
| | | * @return |
| | | */ |
| | | public int executeSqlDelete(final String sql, final Object args[]) { |
| | | |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>(){ |
| | | public int executeSqlDelete(final String sql, final Object args[]) { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session) |
| | | throws HibernateException { |
| | | |
| | | Query query = session.createSQLQuery(sql); |
| | | for(int i=0;i<args.length;i++){ |
| | | query.setParameter(i, args[i]); |
| | | } |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>(){ |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session) |
| | | throws HibernateException { |
| | | |
| | | Query query = session.createSQLQuery(sql); |
| | | for(int i=0;i<args.length;i++){ |
| | | query.setParameter(i, args[i]); |
| | | } |
| | | return query.executeUpdate(); |
| | | } |
| | | |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings({RAW_TYPES, "unchecked" }) |
| | | public List<Object[]> findRawByComplexHql(final String hql, final Map<String, Object> map) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | return (List<Object[]>)this.getHibernateTemplate().execute(new HibernateCallback<List>() { |
| | | public List doInHibernate(Session session) { |
| | | Query query = session.createQuery(hql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | return query.list(); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据hql查询,返回列表数据总数 |
| | | * |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | public int findCountByComplexHql(final String hql, final Map<String, Object> map) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | public int findCountByComplexHql(final String hql, final Map<String, Object> map) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session){ |
| | | |
| | | Query queryHql = session.createQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | |
| | | Query queryHql = session.createQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if(null != queryHql.uniqueResult()){ |
| | | return ((Long)queryHql.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if(null != queryHql.uniqueResult()){ |
| | | return ((Long)queryHql.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | |
| | | /** |
| | | * 根据sql查询,返回列表数据总数 |
| | | * |
| | | * @param hql 查询语句 |
| | |
| | | * @param cls 返回类型 |
| | | * @return |
| | | */ |
| | | public int findCountByComplexSql(final String hql, final Map<String, Object> map) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | public int findCountByComplexSql(final String hql, final Map<String, Object> map) { |
| | | return getHibernateTemplate().execute(new HibernateCallback<Integer>() { |
| | | |
| | | @Override |
| | | public Integer doInHibernate(Session session){ |
| | | |
| | | Query querySql = session.createSQLQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | |
| | | Query querySql = session.createSQLQuery(hql.trim().startsWith("from")?"select count(1) ".concat(hql):hql); |
| | | |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | querySql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | querySql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | querySql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if(null != querySql.uniqueResult()){ |
| | | return ((BigInteger)querySql.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | querySql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | querySql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | querySql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if(null != querySql.uniqueResult()){ |
| | | return ((BigInteger)querySql.uniqueResult()).intValue(); |
| | | }else{ |
| | | return 0 ; |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public <T> List<T> findByComplexHql(final String hql, final Map<String, Object> map, Class<T> cls) { |
| | | Pager page = new Pager(); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | page.setPageNum(1); |
| | | Pager page = new Pager(); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | page.setPageNum(1); |
| | | return findByComplexHql(hql, page, map, cls); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 复杂hql |
| | | * @param sql |
| | |
| | | * @return |
| | | */ |
| | | public <T> T findUniqueByHql(final String hql, final Map<String, Object> map){ |
| | | // 查询结果 |
| | | // 查询结果 |
| | | List<T> lst = this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { |
| | | |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public List<T> doInHibernate(Session session) { |
| | | public List<T> doInHibernate(Session session) { |
| | | Query queryHql = session.createQuery(hql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | queryHql.setFirstResult(0); |
| | | queryHql.setMaxResults(1); |
| | | |
| | | |
| | | List<?> r = queryHql.list(); |
| | | |
| | | |
| | | List<T> lst = new ArrayList<T>(r.size()); |
| | | for(Object o:r) { |
| | | lst.add((T)o); |
| | | lst.add((T)o); |
| | | } |
| | | return lst; |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | return lst.isEmpty()?null:lst.get(0); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果,列表数据为Map |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public List<Map<String,Object>> findListWithMapByHql(final String hql, final Map<String, Object> map) { |
| | | Pager page = new Pager(); |
| | | page.setPageNum(1); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | |
| | | Pager page = new Pager(); |
| | | page.setPageNum(1); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | |
| | | return findListWithMapByHql(hql,map,page); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果,列表数据为Map |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public List<Map<String,Object>> findListWithMapByHql(final String hql, final Map<String, Object> map,final Pager page) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | List<Map<String,Object>> lst = getHibernateTemplate().execute(new HibernateCallback<List<Map<String,Object>>>() { |
| | | public List<Map<String,Object>> doInHibernate(Session session) { |
| | | public List<Map<String,Object>> doInHibernate(Session session) { |
| | | Query query = session.createQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | query.setFirstResult(page.getPageSize()*(page.getPageNum()-1)); |
| | | query.setMaxResults(page.getPageSize()); |
| | | return query.list(); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | return lst; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回唯一一条结果 |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings({RAW_TYPES }) |
| | | public <T> T findUniqueByComplexHql(final String hql, final Map<String, Object> map, Class<T> cls) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | List<T> lst = this.getHibernateTemplate().execute(new HibernateCallback<List<T>>() { |
| | | public List doInHibernate(Session session) { |
| | | Query query = session.createQuery(hql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | query.setFirstResult(0); |
| | | query.setMaxResults(1); |
| | | return query.list(); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | return lst.isEmpty()?null:cls.cast(lst.get(0)); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 根据Sql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public <T> List<T> findByComplexSql(final String sql, final Map<String, Object> map, Class<T> cls) { |
| | | Pager page = new Pager(); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | page.setPageNum(1); |
| | | Pager page = new Pager(); |
| | | page.setPageSize(Integer.MAX_VALUE); |
| | | page.setPageNum(1); |
| | | return findByComplexSql(sql, page, map, cls); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 根据Sql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings({RAW_TYPES, "unchecked" }) |
| | | public List<Object[]> findRawByComplexSql(final String sql, final Map<String, Object> map) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | return (List<Object[]>)this.getHibernateTemplate().execute(new HibernateCallback<List>() { |
| | | public List doInHibernate(Session session) { |
| | | SQLQuery query = session.createSQLQuery(sql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | query.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | query.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | query.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | return query.list(); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据hql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings({RAW_TYPES }) |
| | | public <T> List<T> findByComplexHql(final String hql,final Pager page, final Map<String, Object> map, Class<T> cls) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | List lst = this.getHibernateTemplate().execute(new HibernateCallback<List>() { |
| | | public List doInHibernate(Session session) { |
| | | Query queryHql = session.createQuery(hql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | queryHql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | queryHql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | queryHql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | queryHql.setFirstResult(page.getPageSize()*(page.getPageNum()-1)); |
| | | queryHql.setMaxResults(page.getPageSize()); |
| | | return queryHql.list(); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | // 组装结果 |
| | | List<T> result = new ArrayList<T>(); |
| | | for (Object obj : lst) { |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据Sql查询,并返回执行类型的列表结果 |
| | | * |
| | |
| | | */ |
| | | @SuppressWarnings({RAW_TYPES }) |
| | | public <T> List<T> findByComplexSql(final String sql,final Pager page, final Map<String, Object> map, Class<T> cls) { |
| | | // 查询结果 |
| | | // 查询结果 |
| | | List lst = this.getHibernateTemplate().execute(new HibernateCallback<List>() { |
| | | public List doInHibernate(Session session) { |
| | | SQLQuery querySql = session.createSQLQuery(sql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | SQLQuery querySql = session.createSQLQuery(sql); |
| | | if (map != null) { |
| | | Set<String> keySet = map.keySet(); |
| | | for (String string : keySet) { |
| | | Object obj = map.get(string); |
| | | //这里考虑传入的参数是什么类型,不同类型使用的方法不同 |
| | | if(obj instanceof Collection<?>){ |
| | | querySql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | querySql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | querySql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | if(obj instanceof Collection<?>){ |
| | | querySql.setParameterList(string, (Collection<?>)obj); |
| | | }else if(obj instanceof Object[]){ |
| | | querySql.setParameterList(string, (Object[])obj); |
| | | }else{ |
| | | querySql.setParameter(string, obj); |
| | | } |
| | | } |
| | | } |
| | | querySql.setFirstResult(page.getPageSize()*(page.getPageNum()-1)); |
| | | querySql.setMaxResults(page.getPageSize()); |
| | | return querySql.list(); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | // 组装结果 |
| | | List<T> result = new ArrayList<T>(); |
| | | for (Object obj : lst) { |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |