hibernate dao 公共方法

Posted 薄焦尾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate dao 公共方法相关的知识,希望对你有一定的参考价值。

  1 package com.dao.impl;
  2 
  3 import java.lang.reflect.ParameterizedType;
  4 import java.util.Collection;
  5 import java.util.List;
  6 
  7 import org.hibernate.Query;
  8 import org.hibernate.Session;
  9 import org.hibernate.SessionFactory;
 10 import org.springframework.beans.factory.annotation.Autowired;
 11 import org.springframework.beans.factory.annotation.Qualifier;
 12 import org.springframework.stereotype.Repository;
 13 
 14 import com.dao.IBaseDao;
 15 import com.exception.CustomException;
 16 import com.utils.BeanUtil;
 17 import com.utils.Page;
 18 import com.utils.QueryObject;
 19 import com.utils.QueryParam;
 20 
 21 @SuppressWarnings("unchecked")
 22 @Repository
 23 public class BaseDao<T> implements IBaseDao<T>{
 24 
 25     @Autowired(required = true)
 26     @Qualifier("sessionFactory")
 27     private SessionFactory sessionFactory;
 28 
 29     protected Session getCurrentSession() {
 30         return this.sessionFactory.getCurrentSession();
 31     }
 32 
 33     /**
 34      * @Description: 根据ID获取对象
 35      * @param id ID
 36      * @return T
 37      * @throws Exception
 38      */
 39     public T get(String id) throws CustomException
 40     {
 41         List<T> objects = this.getAllByProperty("objId", id);
 42         if (!objects.isEmpty())
 43         {
 44             return objects.get(0);
 45         }
 46         return null;
 47     }
 48 
 49     /**
 50      * @Description: 根据ID获取对象
 51      * @param id ID
 52      * @param c
 53      * @return T
 54      * @throws Exception
 55      */
 56     public Object get(String id,Class c) throws CustomException
 57     {
 58         List<T> objects = (List<T>)this.getAllByProperty("objId", id,c);
 59         if (!objects.isEmpty())
 60         {
 61             return objects.get(0);
 62         }
 63         return null;
 64     }
 65 
 66     /**
 67      * @Description: 根据属性获取对象
 68      * @param propertyName 属性
 69      * @param value 值
 70      * @return T
 71      * @throws Exception
 72      */
 73     public T getObjectByProperty(String propertyName,Object value) throws CustomException
 74     {
 75         List<T> objects = this.getAllByProperty(propertyName, value);
 76         if (!objects.isEmpty())
 77         {
 78             return objects.get(0);
 79         }
 80         return null;
 81     }
 82 
 83     /**
 84      * @Description: 根据属性获取对象
 85      * @param propertyName 属性
 86      * @param value 值
 87      * @param c
 88      * @return T
 89      * @throws Exception
 90      */
 91     public T getObjectByProperty(String propertyName,Object value,Class c) throws CustomException
 92     {
 93         List<T> objects = (List<T>)this.getAllByProperty(propertyName, value, c);
 94         if (!objects.isEmpty())
 95         {
 96             return objects.get(0);
 97         }
 98         return null;
 99     }
100 
101     /**
102      * @Description: 获取所有对象
103      * @return List<T>
104      * @throws Exception
105      */
106     public List<T> getAll() throws CustomException
107     {
108         Class baseClass;
109         try
110         {
111             baseClass = Class.forName(BeanUtil.getParamType(this.getClass()).replace("class ", ""));
112         } catch (ClassNotFoundException e)
113         {
114             throw new CustomException(this.getClass()+"没有泛型! 或者没有重写  getBaseClass() method");
115         }
116         return this.getAll(baseClass);
117     }
118 
119     /**
120      * @Description: 获取所有对象
121      * @param c
122      * @return List<T>
123      * @throws Exception
124      */
125     public List<T> getAll(Class c) throws CustomException
126     {
127         String hql = " from "+c.getSimpleName();
128         List<T> objects = this.getCurrentSession().createQuery(hql).list();
129         return objects;
130     }
131 
132     /**
133      * @Description: 根据属性获取所有对象
134      * @param propertyName 属性
135      * @param value 值
136      * @return List<T>
137      * @throws Exception
138      */
139     public List<T> getAllByProperty(String propertyName,Object value) throws CustomException
140     {
141         Class baseClass;
142         try
143         {
144             baseClass = Class.forName(BeanUtil.getParamType(this.getClass()).replace("class ", ""));
145         } catch (ClassNotFoundException e)
146         {
147             throw new CustomException(this.getClass()+"没有泛型! 或者没有重写  getBaseClass() method");
148         }
149         return (List<T>)this.getAllByProperty(propertyName, value, baseClass);
150     }
151 
152     /**
153      * @Description: 根据属性获取所有对象
154      * @param propertyName 属性
155      * @param value 值
156      * @param c
157      * @return List<T>
158      * @throws Exception
159      */
160     public List<Object> getAllByProperty(String propertyName,Object value,Class c) throws CustomException
161     {
162         StringBuffer hql = new StringBuffer();
163         hql.append(" from "+c.getSimpleName()+" where "+propertyName+" = ‘"+value+"‘");
164         List<Object> objects = this.getCurrentSession().createQuery(hql.toString()).list();
165         return objects;
166     }
167     
168     /**
169      * @Description: 保存Object
170      * @param object 实体
171      * @return void
172      * @throws Exception
173      */
174     public void save(T object)throws CustomException
175     {
176         this.getCurrentSession().saveOrUpdate(object);
177     }
178 
179     /**
180      * @Description: 保存Object
181      * @param object 实体
182      * @param c
183      * @return void
184      * @throws Exception
185      */
186     public void save(Object object,Class c)throws CustomException
187     {
188         this.getCurrentSession().saveOrUpdate(object);
189     }
190 
191     /**
192      * @Description: 保存Object
193      * @param objects 实体
194      * @return void
195      * @throws Exception
196      */
197     public void saveCollections(Collection<T> objects) throws CustomException
198     {
199         for (Object object:objects)
200         {
201             this.save((T)object);
202         }
203     }
204 
205     /**
206      * @Description: 保存Object
207      * @param objects 实体
208      * @param c
209      * @return void
210      * @throws Exception
211      */
212     public void saveCollections(Collection<T> objects,Class c) throws CustomException
213     {
214         for (Object object:objects)
215         {
216             this.save((T)object);
217         }
218     }
219 
220     /**
221      * @Description: 删除对象
222      * @param id
223      * @return void
224      * @throws Exception
225      */
226     public void remove(String id)
227     {
228         Class baseClass;
229         try
230         {
231             baseClass = Class.forName(BeanUtil.getParamType(this.getClass()).replace("class ", ""));
232         } catch (ClassNotFoundException e)
233         {
234             throw new CustomException(this.getClass()+"没有泛型! 或者没有重写  getBaseClass() method");
235         }
236         this.remove(id, baseClass);
237     }
238 
239     /**
240      * @Description: 删除对象
241      * @param ids
242      * @param c
243      * @return void
244      * @throws Exception
245      */
246     public void remove(String[] ids,Class c)
247     {
248         for (String id:ids)
249         {
250             this.remove(id, c);
251         }
252     }
253 
254     /**
255      * @Description: 删除对象
256      * @param id
257      * @param c
258      * @return void
259      * @throws Exception
260      */
261     public void remove(String id,Class c)
262     {
263         this.removeObjectByProperty("objId", id, c);
264     }
265 
266     /**
267      * @Description: 删除对象
268      * @param object 实体
269      * @return void
270      * @throws Exception
271      */
272     public void remove(T object)
273     {
274         this.getCurrentSession().delete(object);
275     }
276 
277     /**
278      * @Description: 删除对象
279      * @param objects
280      * @return void
281      * @throws Exception
282      */
283     public void removeCollections(Collection<T> objects) throws CustomException
284     {
285         for (Object object:objects)
286         {
287             this.remove((T)object);
288         }
289     }
290 
291     /**
292      * @Description: 删除对象
293      * @param propertyName 属性名
294      * @param value 值
295      * @return void
296      * @throws Exception
297      */
298     public void removeObjectByProperty(String propertyName,Object value)throws CustomException
299     {
300         Class baseClass;
301         try
302         {
303             baseClass = Class.forName(BeanUtil.getParamType(this.getClass()).replace("class ", ""));
304         }
305         catch (ClassNotFoundException e)
306         {
307             throw new CustomException(this.getClass()+"没有泛型! 或者没有重写  getBaseClass() method");
308         }
309         this.removeObjectByProperty(propertyName, value, baseClass);
310     }
311 
312     /**
313      * @Description: 删除对象
314      * @param propertyName 属性名
315      * @param value 值
316      * @param c
317      * @return void
318      * @throws Exception
319      */
320     public void removeObjectByProperty(String propertyName,Object value,Class c)throws CustomException
321     {
322         StringBuffer hql = new StringBuffer();
323         hql.append(" delete ").append(c.getSimpleName()).append(" ");
324         hql.append(" where ").append(propertyName).append(" = ? ");
325         this.getCurrentSession().createQuery(hql.toString()).setParameter(0, value).executeUpdate();
326     }
327 
328     /**
329      * @Description: 分页查询
330      * @param queryObject
331      * @param page
332      * @return void
333      * @throws Exception
334      */
335     public void findByQueryObject(QueryObject queryObject,Page page)
336     {
337         StringBuffer hql = new StringBuffer();//hql语句
338         hql.append(" from ").append(queryObject.getEntityClass().getSimpleName()).append(" where 1=1 ");
339         String bieming = "bm";
340         for (QueryParam param:queryObject.getQueryParam().getAndParams()) //循环参数集合,拼接查询条件
341         {
342             if(!QueryParam.OPERATOR_IS.equals(param.getOperator())){
343                 hql.append(" and ").append(param.getName()).append(" ").append(param.getOperator()).append(" ").append(":").append(bieming).append(" ");
344             }else{
345                 hql.append(" and ").append(param.getName()).append(" ").append(param.getOperator()).append(" ").append("null");
346             }
347             bieming += bieming;
348         }
349         String sql = " ";
350         if(queryObject.getQueryProjections().getOrderProperty()!=null)//拼接排序条件
351         {
352             for(String s :queryObject.getQueryProjections().getOrderProperty())
353             {
354                 sql = "order by "+ s ;
355                 if(queryObject.getQueryProjections().getDescFlag()!=null &&queryObject.getQueryProjections().getDescFlag()[0])
356                 {
357                     sql = sql +" desc";
358                 }
359                 else
360                 {
361                     sql = sql + " asc";
362                 }
363             }
364         }
365         Query query = this.getCurrentSession().createQuery(hql.toString()+sql);//查询结果集
366 
367         bieming = "bm";
368         for (QueryParam param:queryObject.getQueryParam().getAndParams())
369         {
370             if(    QueryParam.OPERATOR_IN.equals(param.getOperator()) || QueryParam.OPERATOR_NIN.equals(param.getOperator()) || QueryParam.OPERATOR_NOTIN.equals(param.getOperator())){
371                 query.setParameterList(bieming, param.getValue().toString().split(","));
372             }else{
373                 if(!QueryParam.OPERATOR_IS.equals(param.getOperator())){
374                     query.setParameter(bieming, param.getValue());
375                 }
376             }
377             bieming += bieming;
378         }
379         query.setFirstResult(page.getFirstResultNum());//设置分页显示的记录数
380         query.setMaxResults((int)page.getPageSize());
381         page.setData(query.setCacheable(true).list());
382 
383         //查询总记录数
384         String hql_1 = "select count(*) " + hql.toString();
385         query = this.getCurrentSession().createQuery(hql_1);
386         bieming = "bm";
387         for (QueryParam param:queryObject.getQueryParam().getAndParams())
388         {
389             if(    QueryParam.OPERATOR_IN.equals(param.getOperator())|| QueryParam.OPERATOR_NIN.equals(param.getOperator()) || QueryParam.OPERATOR_NOTIN.equals(param.getOperator())){
390                 query.setParameterList(bieming, param.getValue().toString().split(","));
391             }else{
392                 if(!QueryParam.OPERATOR_IS.equals(param.getOperator())){
393                     query.setParameter(bieming, param.getValue());
394                 }
395             }
396             bieming += bieming;
397         }
398         page.setTotal((Long)(query.setCacheable(true).list().get(0)));
399     }
400 
401     /**
402      * @Description: 公共查询
403      * @param queryObject
404      * @return List<T>
405      * @throws Exception
406      */
407     public List<T> findByQueryObject(QueryObject queryObject)
408     {
409         StringBuffer hql = new StringBuffer();
410         hql.append(" from ").append(queryObject.getEntityClass().getSimpleName()).append(" where 1=1 ");
411         String bieming = "bm";
412         for (QueryParam param:queryObject.getQueryParam().getAndParams())
413         {
414             hql.append(" and ").append(param.getName()).append(" ").append(param.getOperator()).append(" ").append(":").append(bieming).append(" ");
415             bieming += bieming;
416         }
417         Query query = this.getCurrentSession().createQuery(hql.toString());
418         bieming = "bm";
419         for (QueryParam param:queryObject.getQueryParam().getAndParams())
420         {
421             if(    QueryParam.OPERATOR_IN.equals(param.getOperator())|| QueryParam.OPERATOR_NIN.equals(param.getOperator()) || QueryParam.OPERATOR_NOTIN.equals(param.getOperator())){
422                 query.setParameterList(bieming, param.getValue().toString().split(","));
423             }else{
424                 query.setParameter(bieming, param.getValue());
425             }
426             bieming += bieming;
427         }
428         return query.list();
429     }
430     
431     /**
432      * 
433      * @Description: sql+条件查询
434      * @param @param queryObject
435      * @param @return   
436      * @return List<String>  
437      * @throws
438      * @author ningpeng
439      * @date 2016年10月21日
440      */
441     public List<Object[]> findDomainByQueryObject(QueryObject queryObject)
442     {
443         StringBuffer SQL = new StringBuffer();
444         String sql = queryObject.getQuerySql();
445         SQL.append(sql);
446         
447         for (QueryParam param:queryObject.getQueryParam().getAndParams())
448         {
449             SQL.append(" and ").append(param.getName()).append(" ").append(param.getOperator()).append(" ").append(" ? ");
450         }
451         Query query = this.getCurrentSession().createSQLQuery(sql.toString());
452         for (int i=0;i<queryObject.getQueryParam().getAndParams().size();i++)
453         {
454             query.setParameter(i, queryObject.getQueryParam().getAndParams().get(i).getValue());
455         }
456         return query.list();
457     }
458     
459     
460     /**
461      * @Description: 修改   
462      * @param 修改字段,修改值,条件字段,条件值
463      * @return void
464      * @throws Exception
465      */
466     public void update(String propertyName,Object value,String conditionName,Object conditionValue)throws CustomException
467     {
468         Class baseClass;
469         try
470         {
471             baseClass = Class.forName(BeanUtil.getParamType(this.getClass()).replace("class ", ""));
472         }
473         catch (ClassNotFoundException e)
474         {
475             throw new CustomException(this.getClass()+"没有泛型! 或者没有重写  getBaseClass() method");
476         }
477         this.update(propertyName, value,conditionName,conditionValue,baseClass);
478     }
479     
480     /**
481      * @Description: 修改   修改字段,修改值,条件字段,条件值
482      * @param 修改字段,修改值,条件字段,条件值
483      * @param c
484      * @return void
485      * @throws Exception
486      */
487     public void update(String propertyName,Object value,String conditionName,Object conditionValue,Class c)throws CustomException
488     {
489         StringBuffer hql = new StringBuffer();
490         hql.append(" update ").append(c.getSimpleName()).append(" ");
491         hql.append(" set ").append(propertyName).append(" = ? ");
492         hql.append(" where 1=1 and ").append(conditionName).append(" = ? ");
493         this.getCurrentSession().createQuery(hql.toString()).setParameter(0, value).setParameter(1, conditionValue).executeUpdate();
494     }
495     
496     /**
497      * @Description: 批量修改   修改字段,修改值,条件字段,条件值
498      * @param 修改字段,修改值,条件字段,条件值
499      * @param c
500      * @return void
501      * @throws Exception
502      */
503     public void update(String propertyName,Object value,String conditionName,Object[] conditionValue,Class c)
504     {
505         for (Object id:conditionValue)
506         {
507             this.update(propertyName, value, conditionName, id);
508         }
509     }
510 
511     @Override
512     public String saveObjId(T object) throws CustomException {
513         // TODO Auto-generated method stub
514         return (String)this.getCurrentSession().save(object);
515     }
516     
517     public static String getParamType(Class c){
518         if (null != c ) {
519             ParameterizedType type = (ParameterizedType)c.getGenericSuperclass();
520             if (null != type.getActualTypeArguments() && type.getActualTypeArguments().length>0) {
521                 return BeanUtil.toString(type.getActualTypeArguments());
522             }
523         }
524         return "";
525     }
526 
527 }

 

  1  package com.utils;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 /**
  7  * 查询参数对象
  8  * @ClassName QueryParam
  9  * @Description:TODO(这里用一句话描述这个类的作用)
 10  */
 11 public final class QueryParam {
 12 
 13     public final static String AND = "and";
 14 
 15     public final static String OR = "or";
 16 
 17     public final static String NOT = "not";
 18 
 19     public final static String OPERATOR_EQ = "=";
 20     
 21     public final static String OPERATOR_BT = "bt";
 22 
 23     public final static String OPERATOR_NE = "!=";
 24 
 25     public final static String OPERATOR_NE_ANSINULL_OFF = "!=%";
 26 
 27     public final static String OPERATOR_GE = ">=";
 28 
 29     public final static String OPERATOR_GT = ">";
 30 
 31     public final static String OPERATOR_NGE = "!>=";
 32 
 33     public final static String OPERATOR_NGT = "!>";
 34 
 35     public final static String OPERATOR_LE = "<=";
 36 
 37     public final static String OPERATOR_LT = "<";
 38 
 39     public final static String OPERATOR_NLE = "!<=";
 40 
 41     public final static String OPERATOR_NLT = "!<";
 42 
 43     public final static String OPERATOR_LIKE = "like";
 44     
 45     public final static String OPERATOR_LEFTLIKE = "llike";
 46     
 47     public final static String OPERATOR_RIGHTLIKE = "rlike";
 48 
 49     public final static String OPERATOR_NLIKE = "!like";
 50     
 51     public final static String OPERATOR_NLEFTLIKE = "!llike";
 52     
 53     public final static String OPERATOR_NRIGHTLIKE = "!rlike";
 54 
 55     public final static String OPERATOR_INCLUDE = "include";
 56 
 57     public final static String OPERATOR_NINCLUDE = "!include";
 58 
 59     public final static String OPERATOR_ILIKE = "ilike";
 60 
 61     public final static String OPERATOR_NILIKE = "!ilike";
 62 
 63     public final static String OPERATOR_IINCLUDE = "iinclude";
 64 
 65     public final static String OPERATOR_NIINCLUDE = "!iinclude";
 66 
 67     public final static String OPERATOR_IS = "is";
 68 
 69     public final static String OPERATOR_NIS = "!is";
 70 
 71     public final static String OPERATOR_IN = "in";
 72 
 73     public final static String OPERATOR_NIN = "!in";
 74     
 75     public final static String OPERATOR_NOTIN = "not in";
 76     
 77     public final static String OPERATOR_EXIST = "exists";
 78     
 79     public final static String OPERATOR_NEXIST = "not exists";
 80     
 81     public final static String FETCH = "fetch";
 82 
 83     private String name; //实体属性名,对应请求的键
 84 
 85     private Object value; //查询参数,对应请求的值
 86 
 87     private String operator = OPERATOR_EQ;//操作符,对应包含“_op”键的值
 88 
 89     private List<QueryParam> andParams = new ArrayList<QueryParam>(0);
 90     private List<QueryParam> orParams = new ArrayList<QueryParam>(0);
 91     private List<QueryParam> notParams = new ArrayList<QueryParam>(0);
 92 
 93     public QueryParam() {
 94         
 95     }
 96     
 97     /**
 98      * 
 99      * @Title QueryParam
100      * @Description TODO(这里用一句话描述这个方法的作用)
101      * @param name 查询实体的属性名
102      * @param operator 操作符,从请求包含“_op”的键中获取的值
103      * @param value 查询实体属性名所对应的键值
104      * @throws
105      */
106     public QueryParam(String name, String operator, Object value) {
107         if (OPERATOR_LIKE.equals(operator)||OPERATOR_NLIKE.equals(operator)) {
108             value = "%"+value+"%";
109         }
110         if(OPERATOR_IS.equals(operator) || OPERATOR_NIS.equals(operator)) {
111             value=null;
112         }
113         if (null == value || "".equals(value)) {
114             if (OPERATOR_EQ.equals(operator)) {
115                 operator = OPERATOR_IS;
116             } else if (OPERATOR_NE.equals(operator)) {
117                 operator = OPERATOR_NIS;
118             }
119         } else {
120             if (OPERATOR_IS.equals(operator)) {
121                 operator = OPERATOR_EQ;
122             } else if (OPERATOR_NIS.equals(operator)) {
123                 operator = OPERATOR_NE;
124             }
125         }
126         if (OPERATOR_IN.equals(operator) || OPERATOR_NIN.equals(operator)) {
127             //value = value.toString().replaceAll(",", "‘,‘");
128         }
129         if (OPERATOR_EXIST.equals(operator) || OPERATOR_NEXIST.equals(operator)) {
130             value = value.toString().replaceAll(",", "‘,‘");
131         }
132         this.name = name;
133         this.value = value;
134         this.operator = operator;
135         this.validateOperator();
136     }
137 
138     public void and(QueryParam queryParam) {
139         andParams.add(queryParam);
140     }
141 
142     public void or(QueryParam queryParam) {
143         orParams.add(queryParam);
144     }
145 
146     public void not(QueryParam queryParam) {
147         notParams.add(queryParam);
148     }
149 
150     public String getName() {
151         return name;
152     }
153 
154     public String getOperator() {
155         return operator;
156     }
157 
158     public Object getValue() {
159         return value;
160     }
161 
162     //检查操作符是否有效
163     private void validateOperator() {
164         if(this.operator.startsWith("not")){
165             if(OPERATOR_NEXIST.equals(this.operator))
166                 return;
167             if(OPERATOR_NOTIN.equals(this.operator))
168                 return;
169         }else if (this.operator.startsWith("!")) {
170             if (this.operator.endsWith("%"))
171                 return;
172             if (OPERATOR_NE.equals(this.operator))
173                 return;
174             if (OPERATOR_NGE.equals(this.operator))
175                 return;
176             if (OPERATOR_NGT.equals(this.operator))
177                 return;
178             if (OPERATOR_NLE.equals(this.operator))
179                 return;
180             if (OPERATOR_NLT.equals(this.operator))
181                 return;
182             if (OPERATOR_NLIKE.equals(this.operator))
183                 return;
184             if (OPERATOR_NLEFTLIKE.equals(this.operator))
185                 return;
186             if (OPERATOR_NRIGHTLIKE.equals(this.operator))
187                 return;
188             if (OPERATOR_NINCLUDE.equals(this.operator))
189                 return;
190             if (OPERATOR_NILIKE.equals(this.operator))
191                 return;
192             if (OPERATOR_NIINCLUDE.equals(this.operator))
193                 return;
194             if (OPERATOR_NIS.equals(this.operator))
195                 return;
196             if (OPERATOR_NIN.equals(this.operator))
197                 return;
198         } else {
199             if (OPERATOR_EQ.equals(this.operator))
200                 return;
201             if (OPERATOR_GE.equals(this.operator))
202                 return;
203             if (OPERATOR_GT.equals(this.operator))
204                 return;
205             if (OPERATOR_LE.equals(this.operator))
206                 return;
207             if (OPERATOR_LT.equals(this.operator))
208                 return;
209             if (OPERATOR_LIKE.equals(this.operator))
210                 return;
211             if (OPERATOR_LEFTLIKE.equals(this.operator))
212                 return;
213             if (OPERATOR_RIGHTLIKE.equals(this.operator))
214                 return;
215             if (OPERATOR_INCLUDE.equals(this.operator))
216                 return;
217             if (OPERATOR_ILIKE.equals(this.operator))
218                 return;
219             if (OPERATOR_IINCLUDE.equals(this.operator))
220                 return;
221             if (OPERATOR_IS.equals(this.operator))
222                 return;
223             if (OPERATOR_IN.equals(this.operator))
224                 return;
225             if (FETCH.equals(this.operator))
226                 return;
227             if (OPERATOR_BT.equals(this.operator))
228                 return;
229             if(OPERATOR_EXIST.equals(this.operator))
230                 return;
231         }
232         throw new RuntimeException ("The operator " + this.operator + " could be incorrect!");
233     }
234 
235     public List<QueryParam> getAndParams() {
236         return andParams;
237     }
238 
239     public List<QueryParam> getNotParams() {
240         return notParams;
241     }
242 
243     public List<QueryParam> getOrParams() {
244         return orParams;
245     }
246 
247 }

 

 1 package com.utils;
 2 import java.lang.reflect.ParameterizedType;
 3 
 4 /**
 5  * @Description: 查询对象实现 
 6  * @param <T>   
 7  * @version V1.0
 8  */
 9 public class QueryObjectBase<T> implements QueryObject<T> {
10     /**
11      * 泛型类参数类型
12      */
13     private Class<T> entityClass;
14 
15     private QueryProjections queryProjections=new QueryProjections();
16     
17     private String sql;
18     
19     /**
20      * 查询参数
21      */
22     private QueryParam queryParam=new QueryParam();
23 
24     @SuppressWarnings("unchecked")
25     public Class<T> getEntityClass() {
26         if(this.entityClass==null)
27             return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
28         return this.entityClass;
29     }
30     public void setEntityClass(Class<T> entityClass) {
31         this.entityClass = entityClass;
32     }
33     public QueryProjections getQueryProjections() {
34         return queryProjections;
35     }
36     public void setQueryProjections(QueryProjections queryProjections) {
37         this.queryProjections = queryProjections;
38     }
39     public QueryParam getQueryParam() {
40         return queryParam;
41     }
42     public void setQueryParam(QueryParam queryParam) {
43         this.queryParam = queryParam;
44     }
45     public void setQuerySql(String sql) {
46         this.sql = sql;
47     }
48     public String getQuerySql() {
49         return sql;
50     }
51 }

 

以上是关于hibernate dao 公共方法的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 通用 dao 重载 removeById

请问为啥我在hibernate dao中测试save or update()方法成功了,但是在ssh dao 中用同样的方法却不成功啊

在通用 DAO 中使用 Hibernate 调用存储过程的最佳方法是啥?

在DAO,服务层架构中使用spring MVC和Hibernate的正确方法是啥

java - 如何在Java Spring和Hibernate的单个事务中管理2个DAO方法?

Generic Hibernate DAO