zoukankan      html  css  js  c++  java
  • baseDao 使用spring3+hibernate3方式

     1 package cn.zk.pic.service.dao;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 import java.util.Map;
     6 /**
     7  * 
     8  * @author zh
     9  *
    10  * @param <T>
    11  */
    12 public interface IbaseDao<T extends Serializable> {
    13     
    14     void save(T t);
    15     void update(T t);
    16     void save(String hql,Object[] params);
    17     void delete(T t);
    18     void deleteById(String hql, Object[] params);
    19     int update(String hql,Object[] params);
    20     T get(final Serializable id);
    21     T load(final Serializable id);
    22     public List<T> listNotIn(String hql, Object[] params);
    23     public List<T> getByHqlNotIn(String hql, Object[] params);
    24     List<T> listAll();
    25     List<T> listFenYe(int firstResult, int maxResults);
    26     List<T> listFenYeNotIn(int firstResult, int maxResults,
    27             String hql, Object[] params);
    28     long getCount();
    29     long getCount(String hql,Object[] params);
    30     List<T> listByHql(String hql,Object[] params);
    31     T getByHql(String hql,Object[] params);
    32     List<T> listFenYeAddparams(int firstResult, int maxResults,String hql,Object[] params);
    33     List<T> queryPage(Map map,int firstResult, int maxResults,String hql) ;
    34     long getCount(String hql, Map map);
    35 }
      1 package cn.zk.pic.service.dao;
      2 
      3 import java.io.Serializable;
      4 import java.lang.reflect.ParameterizedType;
      5 import java.lang.reflect.Type;
      6 import java.util.Iterator;
      7 import java.util.List;
      8 import java.util.Map;
      9 
     10 import javax.annotation.Resource;
     11 
     12 import org.hibernate.Query;
     13 import org.hibernate.SessionFactory;
     14 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     15 /**
     16  * BaseDaoImpl不能在类型未确定前直接实例化
     17  * @author zh
     18  * @param <T>
     19  */
     20 public class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements IbaseDao<T> {
     21     
     22     @Resource(name="sessionFactory")
     23     public void setSupperSessionFactory(SessionFactory sessionFactory){
     24             super.setSessionFactory(sessionFactory);
     25     }
     26     
     27 
     28     @SuppressWarnings("rawtypes")
     29     private Class Tclass;
     30     @SuppressWarnings("rawtypes")
     31     public BaseDaoImpl(){
     32          Type type = this.getClass().getGenericSuperclass();  
     33             if (type.toString().indexOf("BaseDao") != -1) {  
     34                 ParameterizedType type1 = (ParameterizedType) type;  
     35                 Type[] types = type1.getActualTypeArguments();  
     36                 setTclass((Class) types[0]);  
     37             }else{  
     38                 type = ((Class)type).getGenericSuperclass();  
     39                 ParameterizedType type1 = (ParameterizedType) type;  
     40                 Type[] types = type1.getActualTypeArguments();  
     41                 setTclass((Class) types[0]);  
     42             } 
     43      }
     44     
     45     /**
     46      * 保存对象
     47      * 
     48      * @param Object
     49      */
     50 
     51     public void save(T t) {
     52         //this.getHibernateTemplate().save(t)
     53         getSession().save(t);
     54         
     55     }
     56 
     57     /**
     58      * 更新对象内容
     59      * 
     60      * @param Object
     61      */
     62     public void update(T t) {
     63         //this.getHibernateTemplate().update(t);
     64         getSession().update(t);
     65     }
     66     
     67     /**
     68      * 删除对象
     69      * @param t
     70      */
     71     public void delete(T t){
     72         //this.getHibernateTemplate().delete(t);
     73         getSession().delete(t);
     74     }
     75     
     76     public void deleteById(String hql, Object[] params){
     77         Query query = this.getSession().createQuery(hql);
     78         for (int i = 0; i < params.length; i++) {
     79             query.setParameter(i, params[i]);
     80         }
     81         query.executeUpdate();
     82     }
     83     
     84     /**
     85      * ͨ通过ID 得到对象
     86      * @param Serializable
     87      */
     88     @SuppressWarnings("unchecked")
     89     public T get(Serializable id) {
     90         //return (T) this.getHibernateTemplate().get(getTclass(), id);
     91         return (T)getSession().get(getTclass(), id);
     92     }
     93     
     94     
     95 
     96     /**
     97      * 取得所有对象
     98      * 
     99      */
    100     @SuppressWarnings("unchecked")
    101     public List<T> listAll() {
    102         String hql = "from "+getTclass().getSimpleName();
    103         return this.getSession().createQuery(hql).list();
    104     }
    105 
    106     
    107 
    108     /**
    109      * hql解决方案
    110      * 
    111      * @param String hql
    112      *          
    113      * @param Object[] params
    114      *           参数列表  顺序不能颠倒
    115      */
    116 
    117     @SuppressWarnings("unchecked")
    118     public List<T> listByHql(String hql, Object[] params) {
    119         Query query = this.getSession().createQuery(hql);
    120         if(null == params || params.length == 0){
    121             return query.list();
    122         }else{
    123             for (int i = 0; i < params.length; i++) {
    124                 query.setParameter(i, params[i]);
    125             }
    126         }
    127         return query.list();
    128     }
    129 
    130     /**
    131      * 同类分页
    132      * 
    133      * @param int firstResult
    134      *           从第几条开始
    135      * @param int maxResults
    136      *           要取几条
    137      */
    138     @SuppressWarnings({ "unchecked" })
    139     public List<T> listFenYe(int firstResult, int maxResults) {
    140         String hql = "from "+getTclass().getSimpleName();
    141         Query query = this.getSession().createQuery(hql).setMaxResults(maxResults).setFirstResult(firstResult);
    142         return query.list();
    143     }
    144     
    145     /**
    146      * 取得行数
    147      */
    148 
    149     public long getCount() {
    150         String hql = "select count(*) from "+getTclass().getSimpleName();
    151         return (Long) this.getSession().createQuery(hql).uniqueResult();
    152     }
    153     /**
    154      * 懒加载load
    155      */
    156     @SuppressWarnings("unchecked")
    157     public T load(Serializable id) {
    158         //return (T) this.getHibernateTemplate().load(this.getClass(), id);
    159         return (T) getSession().load(this.getClass(), id);
    160     }
    161 
    162     @SuppressWarnings("rawtypes")
    163     public Class getTclass() {
    164         return Tclass;
    165     }
    166     @SuppressWarnings("rawtypes")
    167     public void setTclass(Class tclass) {
    168         Tclass = tclass;
    169     }
    170 
    171     @SuppressWarnings("unchecked")
    172     public T getByHql(String hql, Object[] params) {
    173         Query query = this.getSession().createQuery(hql);
    174         for (int i = 0; i < params.length; i++) {
    175             query.setParameter(i, params[i]);
    176         }
    177         return (T) query.uniqueResult();
    178     }
    179     
    180     @SuppressWarnings("unchecked")
    181     public List<T> getByHqlNotIn(String hql, Object[] params) {
    182         System.out.println("Tclass:"+Tclass);
    183         Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    184         List<T> list = null ;
    185         for (int i = 0; i < params.length; i++) {
    186             query.setParameter(i, params[i]);
    187         }
    188         list = query.list();
    189         return list;
    190     }
    191 
    192     @SuppressWarnings("unchecked")
    193     public List<T> listFenYeAddparams(int firstResult, int maxResults,
    194             String hql, Object[] params) {
    195         Query query = this.getSession().createQuery(hql);
    196         List<T> list = null ;
    197         if(null == params || params.length == 0){
    198             list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    199         }
    200         if(null != params || params.length != 0){
    201             for (int i = 0; i < params.length; i++) {
    202                 System.out.println("params:"+params[i]);
    203                 query.setParameter(i, params[i]);
    204             }
    205             list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    206         }
    207         return list;
    208     }
    209 
    210     @SuppressWarnings("unchecked")
    211     public List<T> listFenYeNotIn(int firstResult, int maxResults,
    212             String hql, Object[] params) {
    213         Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    214         List<T> list = null ;
    215         if(null == params || params.length == 0){
    216             list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    217         }
    218         if(null != params || params.length != 0){
    219             for (int i = 0; i < params.length; i++) {
    220                 System.out.println("params:"+params[i]);
    221                 query.setParameter(i, params[i]);
    222             }
    223             list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    224         }
    225         return list;
    226     }
    227     
    228     @SuppressWarnings("unchecked")
    229     public List<T> listNotIn(String hql, Object[] params) {
    230         Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    231         List<T> list = null ;
    232         if(null != params || params.length != 0){
    233             for (int i = 0; i < params.length; i++) {
    234                 System.out.println("params:"+params[i]);
    235                 query.setParameter(i, params[i]);
    236             }
    237         }
    238         list = query.list();
    239         return list;
    240     }
    241     
    242     
    243     
    244     public long getCount(String hql, Object[] params) {
    245         Query query = this.getSession().createQuery(hql);
    246         if(null != params || params.length != 0){
    247             for (int i = 0; i < params.length; i++) {
    248                 query.setParameter(i, params[i]);
    249             }
    250         }
    251         return (Long) query.uniqueResult();
    252     }
    253     @SuppressWarnings("unchecked")
    254     public List<T> queryPage(Map map,int firstResult, int maxResults,
    255             String hql) {
    256         StringBuffer buffer = new StringBuffer(hql);
    257         StringBuffer wherestring = new StringBuffer();
    258         Iterator paramnames  = map.keySet().iterator();
    259         while(paramnames .hasNext())
    260         {
    261             String paramname = (String) paramnames.next();
    262             String value = null;
    263             value = String.valueOf(map.get(paramname));
    264             if(value!=null)
    265             {
    266                 value = value.trim();
    267                 if(value.equals(""))
    268                 {
    269                     continue;
    270                 }
    271             }
    272             if(wherestring.length()==0)
    273             {
    274                 wherestring.append(" where ");
    275             }else{
    276                 wherestring.append(" and ");
    277             }
    278             wherestring.append(paramname).append("=").append(value);
    279             buffer.append(wherestring);
    280         }
    281         Query query = this.getSession().createQuery(buffer.toString());
    282         System.out.println(buffer.toString());
    283         List<T> list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    284         return list;
    285     }
    286     public long getCount(String hql, Map map) {
    287         StringBuffer buffer = new StringBuffer(hql);
    288         StringBuffer wherestring = new StringBuffer();
    289         Iterator paramnames  = map.keySet().iterator();
    290         while(paramnames .hasNext())
    291         {
    292             String paramname = (String) paramnames.next();
    293             String value = null;
    294             //value = (String) map.get(paramname);
    295             value = String.valueOf(map.get(paramname));
    296             if(value!=null)
    297             {
    298                 value = value.trim();
    299                 if(value.equals(""))
    300                 {
    301                     continue;
    302                 }
    303             }
    304             if(wherestring.length()==0)
    305             {
    306                 wherestring.append(" where ");
    307             }else{
    308                 wherestring.append(" and ");
    309             }
    310             wherestring.append(paramname).append("=").append(value);
    311             buffer.append(wherestring);
    312         }
    313         System.out.println(buffer.toString());
    314         Query query = this.getSession().createQuery(buffer.toString());
    315         return (Long) query.uniqueResult();
    316     }
    317 
    318     public int update(String hql, Object[] params) {
    319         Query query = this.getSession().createQuery(hql);
    320         if(params == null){
    321             return query.executeUpdate();
    322         }
    323         for (int i = 0; i < params.length; i++) {
    324             query.setParameter(i, params[i]);
    325         }
    326         return query.executeUpdate();
    327     }
    328 
    329     public void save(String hql, Object[] params) {
    330         Query query = this.getSession().createQuery(hql);
    331         for (int i = 0; i < params.length; i++) {
    332             query.setParameter(i, params[i]);
    333         }
    334         query.executeUpdate();
    335     }
    336 
    337 }
  • 相关阅读:
    河北省重大技术需求征集系统(10)
    河北省重大技术需求征集系统(9)
    大三上学期周总结
    河北省重大技术需求征集系统(8)
    《代码大全》阅读笔记(三)
    河北省重大技术需求征集系统(7)
    河北省重大技术需求征集系统(6)
    河北省重大技术需求征集系统(5)
    【自动化】Aritest+python实现客户端UI自动化
    【自动化】Monkey自动化测试实现总结
  • 原文地址:https://www.cnblogs.com/a757956132/p/5364570.html
Copyright © 2011-2022 走看看