使用HibernateDaoSupport抽取BaseDao
一、BaseDao接口及实现的代码
package com.tax.core.dao;
import java.io.Serializable;
import java.util.List;
/**
* BaseDao
* @author ZENG.XIAO.YAN
* @date 2017年6月29日 上午10:36:57
* @version v1.0
*/
public interface BaseDao<T> {
/**
* 新增
* @param entity
*/
public void save(T entity);
/**
* 更新
* @param entity
*/
public void update(T entity);
/**
* 根据id删除
* @param id
*/
public void deleteById(Serializable id);
/**
* 通过id查找
* @param id
* @return 实体
*/
public T findById(Serializable id);
/**
* 查找所有
* @return List集合
*/
public List<T> findAll();
}
package com.tax.core.dao;
import java.io.Serializable;
import java.util.List;
/**
* BaseDao
* @author ZENG.XIAO.YAN
* @date 2017年6月29日 上午10:36:57
* @version v1.0
*/
public interface BaseDao<T> {
/**
* 新增
* @param entity
*/
public void save(T entity);
/**
* 更新
* @param entity
*/
public void update(T entity);
/**
* 根据id删除
* @param id
*/
public void deleteById(Serializable id);
/**
* 通过id查找
* @param id
* @return 实体
*/
public T findById(Serializable id);
/**
* 查找所有
* @return List集合
*/
public List<T> findAll();
}
package com.tax.core.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.tax.core.dao.BaseDao;
/**
* BaseDaoImpl
* @author ZENG.XIAO.YAN
* @date 2017年6月29日 下午12:23:16
* @version v1.0
*/
public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
private Class<T> clazz;
// 按照类型自动注入SessionFactory; 在实例化的时候,Spring按照形参的类型自动注入
@Autowired
public void setMySessionFactory(SessionFactory sessionFactory){
setSessionFactory(sessionFactory);
}
public BaseDaoImpl() {
//this表示当前被实例化的对象;如UserDaoImpl
ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();//BaseDaoImpl<User>
clazz = (Class<T>)pt.getActualTypeArguments()[0];
}
/**
* 获取session
* @return session
*/
public Session getCurrentSession(){
Session session = null;
try {
session = getSessionFactory().getCurrentSession();
} catch (HibernateException e) {
throw new RuntimeException("getCurrentSession error", e);
//session = getSessionFactory().openSession();
}
return session;
}
@Override
public void save(T entity) {
getHibernateTemplate().save(entity);
}
@Override
public void update(T entity) {
getHibernateTemplate().update(entity);
}
@Override
public void deleteById(Serializable id) {
getHibernateTemplate().delete(findById(id));
}
@Override
public T findById(Serializable id) {
return getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll() {
Session session = getCurrentSession();
Query query = session.createQuery("FROM "+ clazz.getSimpleName());
return query.list();
}
}
package com.tax.core.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.tax.core.dao.BaseDao;
/**
* BaseDaoImpl
* @author ZENG.XIAO.YAN
* @date 2017年6月29日 下午12:23:16
* @version v1.0
*/
public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
private Class<T> clazz;
// 按照类型自动注入SessionFactory; 在实例化的时候,Spring按照形参的类型自动注入
@Autowired
public void setMySessionFactory(SessionFactory sessionFactory){
setSessionFactory(sessionFactory);
}
public BaseDaoImpl() {
//this表示当前被实例化的对象;如UserDaoImpl
ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();//BaseDaoImpl<User>
clazz = (Class<T>)pt.getActualTypeArguments()[0];
}
/**
* 获取session
* @return session
*/
public Session getCurrentSession(){
Session session = null;
try {
session = getSessionFactory().getCurrentSession();
} catch (HibernateException e) {
throw new RuntimeException("getCurrentSession error", e);
//session = getSessionFactory().openSession();
}
return session;
}
@Override
public void save(T entity) {
getHibernateTemplate().save(entity);
}
@Override
public void update(T entity) {
getHibernateTemplate().update(entity);
}
@Override
public void deleteById(Serializable id) {
getHibernateTemplate().delete(findById(id));
}
@Override
public T findById(Serializable id) {
return getHibernateTemplate().get(clazz, id);
}
@Override
public List<T> findAll() {
Session session = getCurrentSession();
Query query = session.createQuery("FROM "+ clazz.getSimpleName());
return query.list();
}
}



二、使用写好的BaseDao和BaseImpl


三、结束语
使用HibernateDaoSupport抽取BaseDao的更多相关文章
- 案例50-crm练习dao层的抽取BaseDao
1 抽取BaseDao 2 BaseDao设计思路 3 BaseDao接口书写 package www.test.dao; import java.io.Serializable; import ja ...
- Dao层抽取BaseDao公共方法
设计IBseDao接口,定义公共的CRUD方法. // IBaseDao 接口,定义公共的CRUD方法 public interface IBaseDao<T> { public void ...
- Hibernate抽取BaseDao
package com.cky.dao; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate. ...
- ssm框架整合抽取BaseDao接口
import java.io.Serializable; import java.util.List; /** * DAO基础操作模板 * * @param <T> 泛型 */ publi ...
- JAVAEE——SSH项目实战02:客户列表和BaseDao封装
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7129152.html 该项目在SSH三大框架整合基础上进行开发:http://www.c ...
- JavaEE笔记——BaseDao的使用
在Hibernate框架中使用BaseDao主要的作用是减少冗余代码,在对Dao的操作中CRUD可以说是最普通最常见的操作了,基本上面对不同的数据表都会有类似的CRUD操作,BaseDao的思想就是把 ...
- (六)编写基类BaseDao
在action中继承了ActionSupport和其它一些公共属性,如selectedRow等:可能以后还会产生更多公共的内容,所以应该把这些共有的抽取出来,放入到一个基本action中,我们命名为B ...
- 公共dao的抽取
package cn.sxx.dao; import java.util.List; import cn.sxx.model.Dep; import cn.sxx.query.DepQuery; pu ...
- 利用泛型抽取Dao层,加事务注解问题(java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType)
想利用泛型抽取BaseDao层,简化操作时出现故障: @Transactional这个注解是能够继承的.于是就想写在抽取的BaseDao层上,让实现的类能够不用写@Transactional,就可开启 ...
随机推荐
- postman和接口自动化测试
1.postman测试接口 (1)首先安装postman 下载地址:https://www.getpostman.com/apps 选择对应版本下载,然后安装即可 (2)使用postman发送请求 比 ...
- Android中的设计模式之观察者模式
参考 <设计模式:可复用面向对象软件的基础 >5.7 Observer 观察者 对象行为型模式 <设计模式解析> 18.4 Observer模式 <Android源码设计 ...
- SublimeText3常用插件及快捷键总结
SublimeText可谓是前端工程师的代码编辑神器,自从用上它以后一直爱不释手,特别是它强大的插件功能,简直要逆天了.网上也有很多关于SublimeText3的各种插件介绍,其插件功能之多,让人眼花 ...
- http的断点续传
要实现断点续传的功能,通常都需要客户端记录下当前的下载进度,并在需要续传的时候通知服务端本次需要下载的内容片段. HTTP1.1协议(RFC2616)中定义了断点续传相关的HTTP头 Range和Co ...
- Javaweb学习(三):Servlet程序
好了,既然开发环境已经配置好了.那么我们首先要搞定得便是servlet了,至于为什么不先去研究jsp,这是因为jsp与servlet本就是一体两面,jsp其本身经过编译.载入.转化等步骤最终会成为se ...
- Alpha冲刺报告(1/12)(麻瓜制造者)
任务分配 这是我们在leangoo上的任务分配: 具体分工如下: 登录界面的编码:邓弘立 肖小强 浏览.检索商品:杜宏庆 汪志彬 待出售的商品: 李佳铭 江郑 数据库建表: 符天愉 刘双玉 图书捐赠模 ...
- Geth命令用法-参数详解 and 以太坊源码文件目录
本文是对以太坊客户端geth命令的解析 命令用法 geth [选项] 命令 [命令选项] [参数-] 版本 1.7.3-stable 命令 account 管理账户 attach 启动交互式JavaS ...
- [Python]运算符的优先级顺序
运算符 描述 ** 指数 (最高优先级) ~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) * / % // 乘,除,取模和取整除 + - 加法减法 >> & ...
- java如何对map进行排序详解(map集合的使用)
今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中就需要对map进行排序. 一.简单介绍Map 在讲解Map排 ...
- Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换(转载)
第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 1.函数介绍在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两 ...