HibernateBaseDAO接口
 package com.iotek.homework.dao;

 import java.io.Serializable;
import java.util.List; /**
* Created by Bo on 2017/4/9.
*/
public interface HibernateBaseDAO<T> { //添加
void doCreate(T entity) throws Exception; //删除
void doDelete(T entity) throws Exception; //修改
void doUpdate(T entity) throws Exception; //查询
List<T> doFind(String hql, Object...param) throws Exception; //根据主键ID查询
T doFindById(Class<T> tClass, Serializable id) throws Exception; //分页查询
List<T> queryPage(String hql, final int START_INDEX, final int PAGE_SIZE, Object...param) throws Exception; //用于分页查询的总行数
List<Long> queryTotalRows(String hql, Object...param) throws Exception;
}
HibernateBaseDAOImpl实现类
 package com.iotek.homework.dao;

 import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate; import java.io.Serializable;
import java.util.List; /**
* Created by Bo on 2017/4/9.
*/
public class HibernateBaseDAOImpl<T> implements HibernateBaseDAO<T> { @Autowired
private HibernateTemplate hibernateTemplate; @Override
public void doCreate(T entity) throws Exception {
hibernateTemplate.save(entity);
} @Override
public void doDelete(T entity) throws Exception {
hibernateTemplate.delete(entity);
} @Override
public void doUpdate(T entity) throws Exception {
hibernateTemplate.update(entity);
} @Override
public List<T> doFind(String hql, Object... param) throws Exception {
return (List<T>) hibernateTemplate.find(hql,param);
} @Override
public T doFindById(Class<T> tClass, Serializable id) throws Exception {
return hibernateTemplate.get(tClass, id);
} @Override
public List<T> queryPage(String hql, int START_INDEX, int PAGE_SIZE, Object... param) throws Exception {
return hibernateTemplate.execute(new HibernateCallback<List<T>>() {
@Override
public List<T> doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(hql);
if(param != null){
for(int i = 0 ; i < param.length ; i ++){
query.setParameter(i, param[i]);
}
}
query.setFirstResult(START_INDEX);
query.setMaxResults(PAGE_SIZE);
return query.list();
}
});
} @Override
public List<Long> queryTotalRows(String hql, Object...param) throws Exception {
return (List<Long>) hibernateTemplate.find(hql,param);
}
}

继承使用

public interface UserDAO extends HibernateBaseDAO<User>{}

public class UserDAOImpl extends HibernateBaseDAOImpl<User> implements UserDAO{}

HibernateBaseDAO的更多相关文章

  1. springboot+druid

    最近项目需要搭建新工程,打算使用微服务的形式搭建便于后期拓展.看了一圈发现springboot易于搭建,配置简单,强化注解功能,"just run". Spring Boot ma ...

  2. Java泛型 T.class的获取

    public interface BaseDao<T> { T get(String id); } import java.lang.reflect.ParameterizedType; ...

  3. java 反射 子类泛型的class

    很早之前写过利用泛型和反射机制抽象DAO ,对其中获取子类泛型的class一直不是很理解.关键的地方是HibernateBaseDao的构造方法中的 Type genType = getClass() ...

  4. JEECMS站群管理系统-- 自定义标签及使用自己创建的表的实现过程

    下面是我自己定义的标签mycontent_list 首先,在数据库里创建了一个jc_mycontent的表,其中有id,title,content三个字段 其次,创建了一个实体类 public cla ...

  5. jeecms v9开发资料

    开发文档 . 系统架构概述 本系统核心架构为 FreeMarker+hibernate+Spirng 的 mvc 分层架构. 1.1 分层架构模型 img 1.2 数据流转模型 (前端) img . ...

  6. JEECMS 自定义标签

    CMS 是”Content Management System” 的缩写,意为” 内容管理系统”. 内容管理系统是企业信息化建设和电子政务的新宠,也是一个相对较新的市场.对于内容管理,业界还没有一个统 ...

  7. jeecms 代码生成 Tools

    本文作者: IIsKei 本文链接: http://www.iskei.cn/posts/50510.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议 ...

  8. 原 jeecms9自定义标签以及使用新创建的数据库表

    转载地址:https://blog.csdn.net/nice_meng/article/details/89179089 本系统使用的是jeecmsv9版本,收集网上知识后,进行个人汇总 首先,自己 ...

  9. jeecms9自定义标签以及使用新创建的数据库表

    转载 https://blog.csdn.net/nice_meng/article/details/89179089 本系统使用的是jeecmsv9版本,收集网上知识后,进行个人汇总 首先,自己创建 ...

随机推荐

  1. Angular——todos案例

    基本介绍 (1)视图绑定两个数组,分别对应未完成和已完成 (2)数组的删除splice(),数组的追加push() 基本使用 <!DOCTYPE html> <html lang=& ...

  2. [Windows Server 2008] SQL Server 2008 数据库还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:还原SQL S ...

  3. linux杀掉某个进程的脚本

    https://www.cnblogs.com/zeng1994/p/13a2c5a28e55dd3abc2c75a4fb80371a.html awk的说明: https://www.cnblogs ...

  4. 06--ubuntu的sqlite安装

    知道学习嵌入式技术,数据库是必须懂的,看的书上嵌入式的教程都在用,看来我是非学不可了,下面就简单的记录一下我在Ubuntu 12.04系统上安装 SQLite 的过程以及使用. 相关阅读: SQLit ...

  5. UICollectionView(一)基本概念

    整体预览 高等级的包含和管理(Top-level containment and management) UICollectionView UICollectionViewController UIC ...

  6. C# Request

    string type = Request["type"]; //值为null; //?type= 值为""; //?type=12 值为"12&qu ...

  7. django-Celery分布式队列简单使用

    介绍: Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具. 它是一个专注于实时处理的任务队列,同时也支持任务调度. worker:是一个独立的进程, ...

  8. js取自定义data属性

    //20170329 原本以为只能attr或者prop来获取属性,但是今天看别人的代码他自定义了一个属性,却取不到他的属性值,我自己在本地又可以取到,难道是phtml的原因,于是我到网上查找,发现了一 ...

  9. mysql中having和where区别?

    having和where有相似之处但也有区别,都是设定条件的语句. 在查询过程中,聚合语句(sum,min,max,avg,count),要比having子句有限执行. 在查询过程中,要先执行wher ...

  10. swift 再识枚举变量

    // Use enum to create an enumeration. Like classes and all other named types, enumerations can have ...