泛型总结--待续

  http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

http://www.blogjava.net/icewee/archive/2012/04/27/376741.html  

  1 package com.zq.www.common;
  2 
  3 import java.lang.reflect.ParameterizedType;
  4 import java.util.List;
  5 
  6 import javax.annotation.Resource;
  7 
  8 import org.hibernate.Query;
  9 import org.hibernate.Session;
 10 import org.hibernate.SessionFactory;
 11 import org.springframework.transaction.annotation.Transactional;
 12 
 13 //使用泛型
 14 @Transactional 
 15 public class BaseDAO<T> {
 16 
 17     //spring里的注解,实现自动注入(自动new)
 18     @Resource
 19     private SessionFactory sessionFactory;
 20     
 21     
 22     /*
 23      * 得到session,所有增删改查都从session开始
 24      */
 25     
 26     public SessionFactory getSessionFactory() {
 27         return sessionFactory;
 28     }
 29 
 30 
 31     public void setSessionFactory(SessionFactory sessionFactory) {
 32         this.sessionFactory = sessionFactory;
 33     }
 34 
 35     public Session gs() {
 36         return this.sessionFactory.getCurrentSession();
 37     }
 38     
 39     
 40     
 41     /*
 42      * 根据T得到真正的class类型,使用反射
 43      */
 44     protected Class<T> entityClass;
 45 
 46     protected Class getEntityClass() {
 47         if (entityClass == null) {
 48             entityClass = (Class<T>) ((ParameterizedType) getClass()
 49                     .getGenericSuperclass()).getActualTypeArguments()[0];
 50         }
 51         return entityClass;
 52     }
 53 
 54     
 55 
 56 
 57     /*
 58      * 添加
 59      */
 60     @Transactional
 61     public void add(T t) {
 62          gs().save(t);
 63     }
 64     
 65     /*
 66      * 更新
 67      */
 68     @Transactional
 69     public void update(T t) {
 70         gs().update(t);
 71     }
 72 
 73     /*
 74      * 删除
 75      */
 76     @Transactional
 77     public void delete(Integer id) {
 78         gs().delete(this.get(id));
 79     }
 80     
 81 
 82     /*
 83      * 查询
 84      */
 85     @Transactional
 86     public T get(Integer id) {
 87         T instance = (T) gs().get(getEntityClass(), id);
 88         return instance;
 89        
 90     }
 91     
 92 
 93 
 94     /*
 95      * 自动判断添加或修改,保存
 96      */
 97     @Transactional
 98     public void save(T t) {
 99         gs().saveOrUpdate(t);
100     }
101     
102     
103     /*
104      * 查询全部
105      */
106     @Transactional
107     public List<T> listall() {
108         List<T> result=gs().createCriteria(getEntityClass()).list();
109         return result;
110     }
111     
112     
113     /*
114      * 通用查询
115      */    
116     @Transactional
117     public List<T> getListByHQL(final String hqlString, final List values) {
118         Query query = gs().createQuery(hqlString);
119         if (values != null) {
120             Object[] vvs = values.toArray(new Object[values.size()]);
121             for (int i = 0; i < vvs.length; i++) {
122                 query.setParameter(i, vvs[i]);
123             }
124         }
125         return query.list();
126     }
127 
128     /* 执行复杂的sql查询
129      * 
130      *  得到一列:List<String>
131      * 
132      * 得到多列:List<String[]>
133      */
134     @Transactional(readOnly = true)
135     public List getListBySQL(final String sqlString, final List values) {
136         Query query = gs().createSQLQuery(sqlString);
137         if (values != null) {
138             Object[] vvs = values.toArray(new Object[values.size()]);
139             for (int i = 0; i < vvs.length; i++) {
140                 query.setParameter(i, vvs[i]);
141             }
142         }
143         return query.list();
144     }
145     
146     
147     
148     /* 执行复杂的sql更新、删除、添加
149      * 
150      *  得到受影响的行数
151      * 
152      */
153     @Transactional
154     public Integer executeSQL(final String sqlString, final List values) {
155         Query query = gs().createSQLQuery(sqlString);
156         if (values != null) {
157             Object[] vvs = values.toArray(new Object[values.size()]);
158             for (int i = 0; i < vvs.length; i++) {
159                 query.setParameter(i, vvs[i]);
160             }
161         }
162         return query.executeUpdate();
163     }
164 
165     
166 }

//

 List<T> result=gs().createCriteria(getEntityClass()).list();??????
---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
原文地址:https://www.cnblogs.com/zzzzw/p/4954970.html