• HibernateDaoSupport的使用


    1、HibernateDaoSupport是有spring提供的一个hibernate模版工具类,或不多说,直接上代码

         接口 IGenericDao.java

    package org.hibernatetemplate.test;
    
    /**
     * 功能:hibernate  操作 模板基类设计
     * @author geenkDC
     * @version 1.0  2016-01-04 17:01:42
     * @JDK 7
     */
    import java.io.Serializable;
    import java.util.Collection;
    import java.util.List;
    
    import org.hibernate.LockMode;
    import org.springframework.dao.DataAccessException;
    
    
    public interface IGenericDao<T, ID extends Serializable> {
    
        public T load(ID id) throws DataAccessException;
    
        public T get(ID id) throws DataAccessException;
    
        public boolean contains(T t) throws DataAccessException;
    
        public void refresh(T t, LockMode lockMode) throws DataAccessException;
    
        public void refresh(T t) throws DataAccessException;
    
        public Serializable save(T t) throws DataAccessException;
    
        public void saveOrUpdate(T t) throws DataAccessException;
    
        public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException;
    
        public void update(T t, LockMode lockMode) throws DataAccessException;
    
        public void update(T t) throws DataAccessException;
    
        public void delete(T t, LockMode lockMode) throws DataAccessException;
    
        public void delete(T t) throws DataAccessException;
    
        public void deleteAll(Collection<T> entities) throws DataAccessException;
    
        public List<T> find(String queryString, Object value) throws DataAccessException;
    
        public List<T> find(String queryString, Object[] values) throws DataAccessException;
    
        public List<T> find(String queryString) throws DataAccessException;
    
        public List<T> list() throws DataAccessException;
    
        public List<T> findByNamedQuery(String queryName) throws DataAccessException;
    
        public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException;
    
        public List<T> findByNamedQuery(String queryName, Object[] values) throws DataAccessException;
    
    
    }

          接口实现类:

      1 package org.hibernatetemplate.test;
      2 
      3 /**
      4  * 功能:hibernate  操作 模板基类设计
      5  * @author geenkDC
      6  * @version 1.0  2016-01-04 17:01:42
      7  * @JDK 7
      8  */
      9 import java.io.Serializable;
     10 import java.lang.reflect.ParameterizedType;
     11 import java.util.Collection;
     12 import java.util.List;
     13 
     14 import org.apache.commons.logging.Log;
     15 import org.apache.commons.logging.LogFactory;
     16 import org.hibernate.LockMode;
     17 import org.springframework.dao.DataAccessException;
     18 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     19 
     20 
     21 @SuppressWarnings("unchecked")
     22 public class GenericDao<T, ID extends Serializable> extends HibernateDaoSupport implements IGenericDao<T, ID> {
     23     private Log logger = LogFactory.getLog(getClass());
     24 
     25     protected Class<T> entityClass;
     26 
     27     public GenericDao() {
     28 
     29     }
     30 
     31     protected Class getEntityClass() {
     32         if (entityClass == null) {
     33             entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
     34             logger.debug("T class = " + entityClass.getName());
     35         }
     36         return entityClass;
     37     }
     38 
     39     public void saveOrUpdate(T t) throws DataAccessException {
     40         this.getHibernateTemplate().saveOrUpdate(t);
     41     }
     42 
     43     public T load(ID id) throws DataAccessException {
     44         T load = (T) getHibernateTemplate().load(getEntityClass(), id);
     45         return load;
     46     }
     47 
     48     public T get(ID id) throws DataAccessException {
     49         T load = (T) getHibernateTemplate().get(getEntityClass(), id);
     50         return load;
     51     }
     52 
     53     public boolean contains(T t) throws DataAccessException {
     54         return getHibernateTemplate().contains(t);
     55     }
     56 
     57     public void delete(T t, LockMode lockMode) throws DataAccessException {
     58         getHibernateTemplate().delete(t, lockMode);
     59     }
     60 
     61     public void delete(T t) throws DataAccessException {
     62         getHibernateTemplate().delete(t);
     63     }
     64 
     65     public void deleteAll(Collection<T> entities) throws DataAccessException {
     66         getHibernateTemplate().deleteAll(entities);
     67     }
     68 
     69     public List<T> find(String queryString, Object value) throws DataAccessException {
     70         List<T> find = (List<T>) getHibernateTemplate().find(queryString, value);
     71         return find;
     72     }
     73 
     74     public List<T> find(String queryString, Object[] values) throws DataAccessException {
     75         List<T> find = (List<T>) getHibernateTemplate().find(queryString, values);
     76         return find;
     77     }
     78 
     79     public List<T> find(String queryString) throws DataAccessException {
     80         return (List<T>) getHibernateTemplate().find(queryString);
     81     }
     82 
     83     public void refresh(T t, LockMode lockMode) throws DataAccessException {
     84         getHibernateTemplate().refresh(t, lockMode);
     85     }
     86 
     87     public void refresh(T t) throws DataAccessException {
     88         getHibernateTemplate().refresh(t);
     89     }
     90 
     91     public Serializable save(T t) throws DataAccessException {
     92         return getHibernateTemplate().save(t);
     93     }
     94 
     95     public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException {
     96         getHibernateTemplate().saveOrUpdateAll(entities);
     97     }
     98 
     99     public void update(T t, LockMode lockMode) throws DataAccessException {
    100         getHibernateTemplate().update(t, lockMode);
    101     }
    102 
    103     public void update(T t) throws DataAccessException {
    104         getHibernateTemplate().update(t);
    105     }
    106 
    107     public List<T> list() throws DataAccessException {
    108         return getHibernateTemplate().loadAll(getEntityClass());
    109 
    110     }
    111 
    112     public List<T> findByNamedQuery(String queryName) throws DataAccessException {
    113         return getHibernateTemplate().findByNamedQuery(queryName);
    114     }
    115 
    116     public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException {
    117         return getHibernateTemplate().findByNamedQuery(queryName, value);
    118     }
    119 
    120     public List<T> findByNamedQuery(String queryName, Object[] values) throws DataAccessException {
    121         return getHibernateTemplate().findByNamedQuery(queryName, values);
    122     }
    123 
    124 }

    怎么使用,自行补脑吧

  • 相关阅读:
    解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题
    百度编辑器
    java程序中输出console的日志到文本
    收集常用的.net开源项目
    Html Agility Pack基础类介绍及运用
    HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
    HTML WEB 和HTML Agility Pack结合
    HtmlWeb类
    简单方便统一封装的傻瓜式GET/POST库AliasNet正式公布~开源喽~
    HtmlAgilityPack.dll的使用 获取HTMLid
  • 原文地址:https://www.cnblogs.com/geekdc/p/5228090.html
Copyright © 2020-2023  润新知