DAO層使用泛型的兩種方式

package sanitation.dao;

import java.util.List;
/**
*
*
@param <T>
*/

public interface GenericDAO <T>{
/**
* 通過ID獲得實體對象
*
*
@param id實體對象的標識符
*
@return 該主鍵值對應的實體對象
*/
T findById(int id);

/**
* 將實體對象持久化
*
*
@param entity 需要進行持久化操作的實體對象
*
@return 持久化的實體對象
*/
T makePersitent(T entity);

/**
* 將實體變為瞬態
*
*
@param entity需要轉變為瞬態的實體對象
*/
void makeTransient(T entity);

/**
* 將一系列的實體變為瞬態,使用本地sql
*
*
@param hql
*/
void makeTransientByIds(String sql);

/**
*
* 使用hql語句進行分頁操作
*
*
@param hql
*
@param offset 第一條記錄索引
*
@param pageSize 每頁需要顯示的記錄數
*
@return 查詢的記錄
*/
List<T> findByPage(final String hql,final int offset,final int pageSize);


/**
* 使用hql 語句進行分頁查詢操作
*
*
@param hql 需要查詢的hql語句
*
@param value 如果hql有一個參數需要傳入,value就是傳入的參數
*
@param offset 第一條記錄索引
*
@param pageSize 每頁需要顯示的記錄數
*
@return 當前頁的所有記錄
*/
List<T> findByPage(final String hql , final Object value ,
final int offset, final int pageSize);

/**
* 使用hql 語句進行分頁查詢操作
*
*
@param hql 需要查詢的hql語句
*
@param values 如果hql有一個參數需要傳入,value就是傳入的參數
*
@param offset 第一條記錄索引
*
@param pageSize 每頁需要顯示的記錄數
*
@return 當前頁的所有記錄
*/
List<T> findByPage(final String hql, final Object[] values,
final int offset, final int pageSize);


/**
* 使用sql 語句進行分頁查詢操作
*
*
@param sql
*
@param offset
*
@param pageSize
*
@return
*/
List findByPageSQL(final String sql,
final int offset, final int pageSize);

/**
* 根據語句查找總數
*
@param hql hql語句
*
@return 對應的數目
*/
Integer getCount(String hql);


void updateObj(final String hql,final Object[] values);
}
復制代碼

定義實現類

復制代碼
package sanitation.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import sanitation.dao.GenericDAO;

public class GenericHibernateDAO<T> extends HibernateDaoSupport
implements GenericDAO<T>{

private Class<T> persistentClass;

public GenericHibernateDAO(Class<T> persistentClass){
this.persistentClass=persistentClass;
}

public Class<T> getPersistentClass(){
return persistentClass;
}

@SuppressWarnings("unchecked")
public T findById(int id) {
return (T)getHibernateTemplate().get(getPersistentClass(), id);
}

@SuppressWarnings("unchecked")
public List<T> findByPage(final String hql,
final int offset, final int pageSize){
List<T> list= getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(final Session session)
throws HibernateException, SQLException{
Query query=session.createQuery(hql);
if(!(offset==0 && pageSize==0)){
query.setFirstResult(offset).setMaxResults(pageSize);
}
List<T> result = query.list();
return result;
}
});
return list;
}

@SuppressWarnings("unchecked")
public List findByPageSQL(final String sql,
final int offset, final int pageSize){
List list= getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(final Session session)
throws HibernateException, SQLException{
Query query=session.createSQLQuery(sql);
if(!(offset==0 && pageSize==0)){
query.setFirstResult(offset).setMaxResults(pageSize);
}
List result = query.list();
return result;
}
});
return list;
}


@SuppressWarnings("unchecked")
public List<T> findByPage(final String hql, final Object value,
final int offset, final int pageSize) {
List<T> list = getHibernateTemplate().executeFind(new HibernateCallback()
{
public Object doInHibernate(Session session)
throws HibernateException, SQLException
{
Query query=session.createQuery(hql).setParameter(0, value);
if(!(offset==0 && pageSize==0)){
query.setFirstResult(offset).setMaxResults(pageSize);
}
List<T> result = query.list();
return result;
}
});
return list;
}

@SuppressWarnings("unchecked")
public List<T> findByPage(final String hql, final Object[] values, final int offset,
final int pageSize) {
List<T> list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException{
Query query=session.createQuery(hql);
for (int i = 0 ; i < values.length ; i++){
query.setParameter( i, values[i]);
}
if(!(offset==0 && pageSize==0)){
query.setFirstResult(offset).setMaxResults(pageSize);
}
List<T> result = query.list();
return result;
}
});
return list;
}


public void updateObj(final String hql, final Object[] values) {
getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException{
Query query=session.createQuery(hql);
for(int i=0;i<values.length;i++){
query.setParameter( i, values[i]);
}
query.executeUpdate();
return null;
}
});
}

public Integer getCount(String hql) {
Integer count;
//iterate方法與list方法的區別是list取出全部,iterator取出主鍵,迭代的時候才取出數據
count = ((Long)getHibernateTemplate().iterate(hql).next()).intValue();
return count;
}

public T makePersitent(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
return entity;
}

public void makeTransient(T entity) {
getHibernateTemplate().delete(entity);
}

public void makeTransientByIds(final String sql) {
getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException{
Query query=session.createQuery(sql);
query.executeUpdate();
return null;
}
});
}

}
復制代碼

2.而有時我們為了方便起見,對于一些簡單的項目,DAO的操作很單一,不會有很復雜的操作,那么我們直接用泛型方法類代替泛型類,主要就不需要寫其他的DAO來繼承,

整個DAO層就一個DAO類。

接口:

復制代碼
package com.xidian.dao;

import java.util.List;

import com.xidian.bean.Admin;
import com.xidian.bean.HostIntroduce;
import com.xidian.bean.Reply;

public interface CommonDAO {
public <T> void sava(T entity); //保存用戶,無返回值;
public <T> void remove(T entity); //刪除用戶
public <T> void update(T entity); //更新用戶
public <T> T findById(Class<T> entityClass, Integer id); //通過id來查找某一個用戶;
public <T> List<T> findAll(Class<T> entityclass); //使用范型List<>,查詢所有的用戶信息

}
復制代碼

實現類:

復制代碼
package com.xidian.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xidian.bean.Admin;
import com.xidian.bean.HostIntroduce;
import com.xidian.bean.Reply;
import com.xidian.dao.CommonDAO;

public class CommonDAOImpl extends HibernateDaoSupport implements CommonDAO {
@SuppressWarnings("unchecked")
@Override
public <T> List<T> findAll(Class<T> entityclass) {
//String name = entity.getClass().getName();
String hql = "from "+entityclass.getName()+" as aaa order by aaa.id desc";
return this.getHibernateTemplate().find(hql);
}

@SuppressWarnings("unchecked")
@Override
public <T> T findById(Class<T> entityClass, Integer id) {
return (T) this.getHibernateTemplate().get(entityClass, id);
}

@Override
public <T> void remove(T entity) {
this.getHibernateTemplate().delete(entity);
}

@Override
public <T> void sava(T entity) {
this.getHibernateTemplate().save(entity);
}

@Override
public <T> void update(T entity) {
this.getHibernateTemplate().update(entity);
}

}

轉載于:https://www.cnblogs.com/sailormoon/archive/2012/12/20/2827017.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274417.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274417.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274417.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將是驚心動魄的決戰~

大家好&#xff0c;我是若川。一個和大家一起學源碼的普通小前端。眾所周知&#xff0c;我參加了掘金人氣創作者評選活動&#xff08;投票&#xff09;&#xff0c;具體操作見此文&#xff1a;一個普通小前端&#xff0c;將如何再戰掘金年度創作者人氣榜單~。最后再簡單拉拉票吧…

圖書漂流系統的設計和研究_研究在設計系統中的作用

圖書漂流系統的設計和研究Having spent the past 8 months of my academic career working co-ops and internships in marketing & communication roles, my roots actually stem from arts & design. Although I would best describe myself as an early 2000s child…

黑馬-程序員C#泛型簡介

---------------------- Windows Phone 7手機開發、.Net培訓、期待與您交流&#xff01; ---------------------- 泛型&#xff1a;通過參數化類型來實現在同一份代碼上操作多種數據類型。利用“參數化類型”將類型抽象化&#xff0c;從而實現靈活的復用。 例子代碼&#xff1a…

西里爾字符_如何設計西里爾字母?(Nje),?(Lje),?(Tshe)和?(Dje)

西里爾字符This article is about how to design Cyrillic characters ?, ?, ?, and ? (upright caps and lowercase; italics are not covered here). They are often problematic since they are Cyrillic, but not found in the Russian alphabet, so there is no much …

學習 vuex 源碼整體架構,打造屬于自己的狀態管理庫

前言這是學習源碼整體架構第五篇。整體架構這詞語好像有點大&#xff0c;姑且就算是源碼整體結構吧&#xff0c;主要就是學習是代碼整體結構&#xff0c;不深究其他不是主線的具體函數的實現。本篇文章學習的是實際倉庫的代碼。其余四篇分別是&#xff1a;學習 jQuery 源碼整體…

VMware workstation 8.0上安裝VMware ESXI5.0

首先&#xff0c;在VMware的官網上注冊&#xff0c;下載VMware ESXI的安裝包vmware&#xff0d;vmvisor&#xff0d;installer&#xff0d;5.0.0&#xff0d;469512.x86_64.iso&#xff0c;它是iso文件&#xff0c;刻盤進行安裝&#xff0c;安裝過程中&#xff0c;會將硬盤全部…

最新ui設計趨勢_10個最新且有希望的UI設計趨勢

最新ui設計趨勢重點 (Top highlight)Recently, I’ve spent some time observing the directions in which UI design is heading. I’ve stumbled across a few very creative, promising and inspiring trends that, in my opinion, will shape the UI design in the nearest…

Lists

動態數組&#xff0c;可以存儲不同數據類型 >>> a [spam, eggs, 100, 1234] >>> a [spam, eggs, 100, 1234] 和string一樣&#xff0c;支持索引&#xff0c;&#xff0c;* >>> a[0] spam >>> a[3] 1234 >>> a[-2] 100 >>&…

學習 axios 源碼整體架構,打造屬于自己的請求庫

前言這是學習源碼整體架構系列第六篇。整體架構這詞語好像有點大&#xff0c;姑且就算是源碼整體結構吧&#xff0c;主要就是學習是代碼整體結構&#xff0c;不深究其他不是主線的具體函數的實現。本篇文章學習的是實際倉庫的代碼。學習源碼整體架構系列文章如下&#xff1a;1.…

404 錯誤頁面_如何設計404錯誤頁面,以使用戶留在您的網站上

404 錯誤頁面重點 (Top highlight)網站設計 (Website Design) There is a thin line between engaging and enraging when it comes to a site’s 404 error page. They are the most neglected of any website page. The main reason being, visitors are not supposed to end…

宏定義學習

【1】宏定義怎么理解&#xff1f; 關于宏定義&#xff0c;把握住本質&#xff1a;僅僅是一種字符替換&#xff0c;而且是在預處理之前就進行。 【2】宏定義可以包括分號嗎&#xff1f; 可以&#xff0c;示例代碼如下&#xff1a; 1 #include<iostream>2 using namespace…

學習 koa 源碼的整體架構,淺析koa洋蔥模型原理和co原理

前言這是學習源碼整體架構系列第七篇。整體架構這詞語好像有點大&#xff0c;姑且就算是源碼整體結構吧&#xff0c;主要就是學習是代碼整體結構&#xff0c;不深究其他不是主線的具體函數的實現。本篇文章學習的是實際倉庫的代碼。學習源碼整體架構系列文章如下&#xff1a;1.…

公網對講機修改對講機程序_更少的對講機,對講機-更多專心,專心

公網對講機修改對講機程序重點 (Top highlight)I often like to put a stick into the bike wheel of the UX industry as it’s strolling along feeling proud of itself. I believe — strongly — that as designers we should primarily be doers not talkers.我經常喜歡在…

spring配置文件-------通配符

<!-- 這里一定要注意是使用spring的mappingLocations屬性進行通配的 --> <property name"mappingLocations"> <list> <value>classpath:/com/model/domain/*.hbm.xml</value> </list> </proper…

若川知乎問答:2年前端經驗,做的項目沒什么技術含量,怎么辦?

知乎問答&#xff1a;做了兩年前端開發&#xff0c;平時就是拿 Vue 寫寫頁面和組件&#xff0c;簡歷的項目經歷應該怎么寫得好看&#xff1f;以下是我的回答&#xff0c;閱讀量5000&#xff0c;所以發布到公眾號申明原創。題主說的2年經驗做的東西沒什么技術含量&#xff0c;應…

ui設計基礎_我不知道的UI設計的9個重要基礎

ui設計基礎重點 (Top highlight)After listening to Craig Federighi’s talk on how to be a better software engineer I was sold on the idea that it is super important for a software engineer to learn the basic principles of software design.聽了克雷格費德里希(C…

Ubuntu下修改file descriptor

要修改Ubuntu下的file descriptor的話&#xff0c;請參照一下步驟。&#xff08;1&#xff09;修改limits.conf  $sudo vi /etc/security/limits.conf  增加一行  *  -  nofile  10000&#xff08;2&#xff09;修改 common-session  $ sudo vi/etc/pam.d/common…

C# 多線程控制 通訊 和切換

一.多線程的概念   Windows是一個多任務的系統&#xff0c;如果你使用的是windows 2000及其以上版本&#xff0c;你可以通過任務管理器查看當前系統運行的程序和進程。什么是進程呢&#xff1f;當一個程序開始運行時&#xff0c;它就是一個進程&#xff0c;進程所指包括運行中…

vue路由匹配實現包容性_包容性設計:面向老年用戶的數字平等

vue路由匹配實現包容性In Covid world, a lot of older users are getting online for the first time or using technology more than they previously had. For some, help may be needed.在Covid世界中&#xff0c;許多年長用戶首次上網或使用的技術比以前更多。 對于某些人…

IPhone開發 用子類搞定不同的設備(iphone和ipad)

用子類搞定不同的設備 因為要判斷我們的程序正運行在哪個設備上&#xff0c;所以&#xff0c;我們的代碼有些混亂了&#xff0c;IF來ELSE去的&#xff0c;記住&#xff0c;將來你花在維護代碼上的時間要比花在寫代碼上的時間多&#xff0c;如果你的項目比較大&#xff0c;且IF語…