zoukankan      html  css  js  c++  java
  • Hibernate的BaseDao辅助类

    1、BaseDao接口类,该类封装了一些hibernate操作数据库的一些常用的方法,包括分页查询,使用该类极大的简化了hibernate的开发

    BaseDao.java

      1 package com.kjonline2.dao;
      2 
      3 import java.io.Serializable;
      4 import java.util.List;
      5 
      6 public interface BaseDao<T> {
      7 
      8     /** 
      9      * 保存一个对象 
     10      *  
     11      * @param o 
     12      * @return 
     13      */  
     14     public Serializable save(T o);  
     15    
     16     /** 
     17      * 删除一个对象 
     18      *  
     19      * @param o 
     20      */  
     21     public void delete(T o);  
     22    
     23     /** 
     24      * 更新一个对象 
     25      *  
     26      * @param o 
     27      */  
     28     public void update(T o);  
     29    
     30     /** 
     31      * 保存或更新对象 
     32      *  
     33      * @param o 
     34      */  
     35     public void saveOrUpdate(T o);  
     36    
     37     /** 
     38      * 查询 
     39      *  
     40      * @param hql 
     41      * @return 
     42      */  
     43     public List<T> find(String hql); 
     44     
     45     /**
     46      * 查询部分
     47      * @param hql
     48      * @param num
     49      * @return
     50      */
     51     public List<T> findPart(String hql,int num);  
     52    
     53     /** 
     54      * 查询集合 
     55      *  
     56      * @param hql 
     57      * @param param 
     58      * @return 
     59      */  
     60     public List<T> find(String hql, Object[] param);  
     61    
     62     /** 
     63      * 查询集合 
     64      *  
     65      * @param hql 
     66      * @param param 
     67      * @return 
     68      */  
     69     public List<T> find(String hql, List<Object> param);  
     70    
     71     /** 
     72      * 查询集合(带分页) 
     73      *  
     74      * @param hql 
     75      * @param param 
     76      * @param page 
     77      *            查询第几页 
     78      * @param rows 
     79      *            每页显示几条记录 
     80      * @return 
     81      */  
     82     public List<T> find(String hql, Object[] param, Integer page, Integer rows);  
     83    
     84     /** 
     85      * 查询集合(带分页) 
     86      *  
     87      * @param hql 
     88      * @param param 
     89      * @param page 
     90      * @param rows 
     91      * @return 
     92      */  
     93     public List<T> find(String hql, List<Object> param, Integer page, Integer rows);  
     94    
     95     /** 
     96      * 获得一个对象 
     97      *  
     98      * @param c 
     99      *            对象类型 
    100      * @param id 
    101      * @return Object 
    102      */  
    103     public T get(Class<T> c, Serializable id);  
    104    
    105     /** 
    106      * 获得一个对象 
    107      *  
    108      * @param hql 
    109      * @param param 
    110      * @return Object 
    111      */  
    112     public T get(String hql, Object[] param);  
    113    
    114     /** 
    115      * 获得一个对象 
    116      *  
    117      * @param hql 
    118      * @param param 
    119      * @return 
    120      */  
    121     public T get(String hql, List<Object> param);  
    122    
    123     /** 
    124      * select count(*) from 类 
    125      *  
    126      * @param hql 
    127      * @return 
    128      */ 
    129     
    130     public Long count(String hql);  
    131     /**
    132      * select count(*) from 类 
    133      * @param hql
    134      * @return
    135      */
    136     public Integer getCount(String hql);  
    137    
    138     /** 
    139      * select count(*) from 类 
    140      *  
    141      * @param hql 
    142      * @param param 
    143      * @return 
    144      */  
    145     public Long count(String hql, Object[] param);  
    146    
    147     /** 
    148      * select count(*) from 类 
    149      *  
    150      * @param hql 
    151      * @param param 
    152      * @return 
    153      */  
    154     public Long count(String hql, List<Object> param);  
    155    
    156     /** 
    157      * 执行HQL语句 
    158      *  
    159      * @param hql 
    160      * @return 响应数目 
    161      */  
    162     public Integer executeHql(String hql);  
    163    
    164     /** 
    165      * 执行HQL语句 
    166      *  
    167      * @param hql 
    168      * @param param 
    169      * @return 响应数目 
    170      */  
    171     public Integer executeHql(String hql, Object[] param);  
    172    
    173     /** 
    174      * 执行HQL语句 
    175      *  
    176      * @param hql 
    177      * @param param 
    178      * @return 
    179      */  
    180     public Integer executeHql(String hql, List<Object> param);  
    181 
    182 }

    该类使用泛型来做接口,使该类拥有极好的通用性和扩展性,可供多个不同类型的service来调用

    2、BaseDao的实现类,BaseDaoImpl.java,实现了BaseDao接口的java类,可供service的实现类来调用,使用spring的@Autowired注解来调用则更为方便

      1 package com.kjonline2.dao;
      2 
      3 import java.io.Serializable;
      4 import java.util.List;
      5 
      6 import org.hibernate.Query;
      7 import org.hibernate.Session;
      8 import org.hibernate.SessionFactory;
      9 import org.springframework.beans.factory.annotation.Autowired;
     10 import org.springframework.stereotype.Repository;
     11 import org.springframework.transaction.annotation.Transactional;
     12 
     13 @Repository("baseDao")
     14 @SuppressWarnings("all")  
     15 @Transactional
     16 public class BaseDaoImpl<T> implements BaseDao<T> {
     17      
     18      
     19         private SessionFactory sessionFactory;
     20      
     21         public SessionFactory getSessionFactory() {
     22             return sessionFactory;
     23         }
     24      
     25         @Autowired
     26         public void setSessionFactory(SessionFactory sessionFactory) {
     27             this.sessionFactory = sessionFactory;
     28         }
     29      
     30         private Session getCurrentSession() {
     31             return sessionFactory.getCurrentSession();
     32         }
     33      
     34         public Serializable save(T o) {
     35             return this.getCurrentSession().save(o);
     36         }
     37      
     38         public void delete(T o) {
     39             this.getCurrentSession().delete(o);
     40         }
     41      
     42         public void update(T o) {
     43             this.getCurrentSession().update(o);
     44         }
     45      
     46         public void saveOrUpdate(T o) {
     47             this.getCurrentSession().saveOrUpdate(o);
     48         }
     49      
     50         public List<T> find(String hql) {
     51             return this.getCurrentSession().createQuery(hql).list();
     52         }
     53      
     54         public List<T> find(String hql, Object[] param) {
     55             Query q = this.getCurrentSession().createQuery(hql);
     56             if (param != null && param.length > 0) {
     57                 for (int i = 0; i < param.length; i++) {
     58                     q.setParameter(i, param[i]);
     59                 }
     60             }
     61             return q.list();
     62         }
     63      
     64         public List<T> find(String hql, List<Object> param) {
     65             Query q = this.getCurrentSession().createQuery(hql);
     66             if (param != null && param.size() > 0) {
     67                 for (int i = 0; i < param.size(); i++) {
     68                     q.setParameter(i, param.get(i));
     69                 }
     70             }
     71             return q.list();
     72         }
     73      
     74         public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
     75             if (page == null || page < 1) {
     76                 page = 1;
     77             }
     78             if (rows == null || rows < 1) {
     79                 rows = 10;
     80             }
     81             Query q = this.getCurrentSession().createQuery(hql);
     82             if (param != null && param.length > 0) {
     83                 for (int i = 0; i < param.length; i++) {
     84                     q.setParameter(i, param[i]);
     85                 }
     86             }
     87             return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
     88         }
     89      
     90         public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
     91             if (page == null || page < 1) {
     92                 page = 1;
     93             }
     94             if (rows == null || rows < 1) {
     95                 rows = 10;
     96             }
     97             Query q = this.getCurrentSession().createQuery(hql);
     98             if (param != null && param.size() > 0) {
     99                 for (int i = 0; i < param.size(); i++) {
    100                     q.setParameter(i, param.get(i));
    101                 }
    102             }
    103             return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
    104         }
    105      
    106         public T get(Class<T> c, Serializable id) {
    107             return (T) this.getCurrentSession().get(c, id);
    108         }
    109      
    110         public T get(String hql, Object[] param) {
    111             List<T> l = this.find(hql, param);
    112             if (l != null && l.size() > 0) {
    113                 return l.get(0);
    114             } else {
    115                 return null;
    116             }
    117         }
    118      
    119         public T get(String hql, List<Object> param) {
    120             List<T> l = this.find(hql, param);
    121             if (l != null && l.size() > 0) {
    122                 return l.get(0);
    123             } else {
    124                 return null;
    125             }
    126         }
    127      
    128         public Long count(String hql) {
    129             return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
    130         }
    131      
    132         public Long count(String hql, Object[] param) {
    133             Query q = this.getCurrentSession().createQuery(hql);
    134             if (param != null && param.length > 0) {
    135                 for (int i = 0; i < param.length; i++) {
    136                     q.setParameter(i, param[i]);
    137                 }
    138             }
    139             return (Long) q.uniqueResult();
    140         }
    141      
    142         public Long count(String hql, List<Object> param) {
    143             Query q = this.getCurrentSession().createQuery(hql);
    144             if (param != null && param.size() > 0) {
    145                 for (int i = 0; i < param.size(); i++) {
    146                     q.setParameter(i, param.get(i));
    147                 }
    148             }
    149             return (Long) q.uniqueResult();
    150         }
    151      
    152         public Integer executeHql(String hql) {
    153             return this.getCurrentSession().createQuery(hql).executeUpdate();
    154         }
    155      
    156         public Integer executeHql(String hql, Object[] param) {
    157             Query q = this.getCurrentSession().createQuery(hql);
    158             if (param != null && param.length > 0) {
    159                 for (int i = 0; i < param.length; i++) {
    160                     q.setParameter(i, param[i]);
    161                 }
    162             }
    163             return q.executeUpdate();
    164         }
    165      
    166         public Integer executeHql(String hql, List<Object> param) {
    167             Query q = this.getCurrentSession().createQuery(hql);
    168             if (param != null && param.size() > 0) {
    169                 for (int i = 0; i < param.size(); i++) {
    170                     q.setParameter(i, param.get(i));
    171                 }
    172             }
    173             return q.executeUpdate();
    174         }
    175 
    176         public List<T> findPart(String hql, int num) {
    177             // TODO Auto-generated method stub
    178             Query q = this.getCurrentSession().createQuery(hql);
    179             q.setMaxResults(num);
    180             return q.list();
    181         }
    182 
    183         public Integer getCount(String hql) {
    184             // TODO Auto-generated method stub
    185             return ((Number) this.getCurrentSession().createQuery(hql).uniqueResult()).intValue();
    186         }
    187 
    188 
    189     }
    作者:no-npe
    出处:https://www.cnblogs.com/geekdc
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
    由于作者个人水平有限,如果文中有什么错误,欢迎指出。以免更多的人被误导。

  • 相关阅读:
    php函数
    php循环语句(二)
    php循环语句(一)
    php魔术常量
    php超级全局变量
    php数组函数
    php数组
    php条件语句(二)
    php条件语句(一)
    shell 中的判断
  • 原文地址:https://www.cnblogs.com/geekdc/p/5213079.html
Copyright © 2011-2022 走看看