HibernateBaseDAO

本文介绍了HibernateBaseDAO接口的设计及其实现类HibernateBaseDAOImpl,包括添加、删除、修改、查询等基本操作,并展示了如何通过继承来具体应用到UserDAO接口及其实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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{}

 

转载于:https://www.cnblogs.com/BobXie85/p/6699210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值