• HibernateBaseDAO


    HibernateBaseDAO接口
     1 package com.iotek.homework.dao;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 
     6 /**
     7  * Created by Bo on 2017/4/9.
     8  */
     9 public interface HibernateBaseDAO<T> {
    10 
    11     //添加
    12     void doCreate(T entity) throws Exception;
    13 
    14     //删除
    15     void doDelete(T entity) throws Exception;
    16 
    17     //修改
    18     void doUpdate(T entity) throws Exception;
    19 
    20     //查询
    21     List<T> doFind(String hql, Object...param) throws Exception;
    22 
    23     //根据主键ID查询
    24     T doFindById(Class<T> tClass, Serializable id) throws Exception;
    25 
    26     //分页查询
    27     List<T> queryPage(String hql, final int START_INDEX, final int PAGE_SIZE, Object...param) throws Exception;
    28 
    29     //用于分页查询的总行数
    30     List<Long> queryTotalRows(String hql, Object...param) throws Exception;
    31 }

    HibernateBaseDAOImpl实现类
     1 package com.iotek.homework.dao;
     2 
     3 import org.hibernate.HibernateException;
     4 import org.hibernate.Query;
     5 import org.hibernate.Session;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.orm.hibernate4.HibernateCallback;
     8 import org.springframework.orm.hibernate4.HibernateTemplate;
     9 
    10 import java.io.Serializable;
    11 import java.util.List;
    12 
    13 /**
    14  * Created by Bo on 2017/4/9.
    15  */
    16 public class HibernateBaseDAOImpl<T> implements HibernateBaseDAO<T> {
    17 
    18     @Autowired
    19     private HibernateTemplate hibernateTemplate;
    20 
    21     @Override
    22     public void doCreate(T entity) throws Exception {
    23         hibernateTemplate.save(entity);
    24     }
    25 
    26     @Override
    27     public void doDelete(T entity) throws Exception {
    28         hibernateTemplate.delete(entity);
    29     }
    30 
    31     @Override
    32     public void doUpdate(T entity) throws Exception {
    33         hibernateTemplate.update(entity);
    34     }
    35 
    36     @Override
    37     public List<T> doFind(String hql, Object... param) throws Exception {
    38         return (List<T>) hibernateTemplate.find(hql,param);
    39     }
    40 
    41     @Override
    42     public T doFindById(Class<T> tClass, Serializable id) throws Exception {
    43         return hibernateTemplate.get(tClass, id);
    44     }
    45 
    46     @Override
    47     public List<T> queryPage(String hql, int START_INDEX, int PAGE_SIZE, Object... param) throws Exception {
    48         return hibernateTemplate.execute(new HibernateCallback<List<T>>() {
    49             @Override
    50             public List<T> doInHibernate(Session session) throws HibernateException {
    51                 Query query = session.createQuery(hql);
    52                 if(param != null){
    53                     for(int i = 0 ; i < param.length ; i ++){
    54                         query.setParameter(i, param[i]);
    55                     }
    56                 }
    57                 query.setFirstResult(START_INDEX);
    58                 query.setMaxResults(PAGE_SIZE);
    59                 return query.list();
    60             }
    61         });
    62     }
    63 
    64     @Override
    65     public List<Long> queryTotalRows(String hql, Object...param) throws Exception {
    66         return (List<Long>) hibernateTemplate.find(hql,param);
    67     }
    68 }

    继承使用

    public interface UserDAO extends HibernateBaseDAO<User>{}
    
    public class UserDAOImpl extends HibernateBaseDAOImpl<User> implements UserDAO{}
  • 相关阅读:
    vm扩容
    手算二维离散傅里叶变换
    取消vim模式匹配的高亮
    DS DI ES SI等等
    int and Integer
    为什么超类能引用子类的对象
    voltile解析
    java集合
    疑问:无限定通配符
    layui table 导出
  • 原文地址:https://www.cnblogs.com/BobXie85/p/6699210.html
Copyright © 2020-2023  润新知