日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HibernateBaseDAO

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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{}

?

轉載于:https://www.cnblogs.com/BobXie85/p/6699210.html

總結

以上是生活随笔為你收集整理的HibernateBaseDAO的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。