Hibernate+Spring 对DAO的处理实列!
引用"Spring"手册上的话说: Hibernate+Spring显然是天生的结合.
下面是我用spring处理的一个HibernateDAO实例,可以看到,代码量大大减少了.
java代码:
package infoweb.dao;
import java.util.List;
import java.util.Iterator;
import infoweb.pojo.Info;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 段洪杰
* @version 1.0
*/
public class InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {
/**
* 构造函数
*/
public InfoDAOImpl() {
super();
}
/**
* 增加记录
* @param info Info
*/
public void setInfo(Info info) throws Exception {
getHibernateTemplate().save(info);
}
/**
* 通过ID取得记录
* @param id String
* @return Info
*/
public Info getInfoById(String id) throws Exception {
Info info = (Info) getHibernateTemplate().load(Info.class, id);
return info;
}
/**
* 修改记录
* @param Info info
*/
public void modifyInfo(Info info) throws Exception {
getHibernateTemplate().update(info);
}
/**
* 删除记录
* @param Info info
*/
public void removeInfo(Info info) throws Exception {
getHibernateTemplate().delete(info);
}
////////////////////////////////////////////////////////
///// ///
/////以下部份不带审核功能 ///
///// ///
////////////////////////////////////////////////////////
/**
* 取记录总数
* @return int
*/
public int getInfosCount() throws Exception {
int count = 0;
String queryString = "select count(*) from Info";
count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
intValue();
return count;
}
/**
* 取所有记录集合
* @return Iterator
*/
public Iterator getAllInfos() throws Exception {
Iterator iterator = null;
String queryString = " select info from Info as info order by info.iddesc";
List list = getHibernateTemplate().find(queryString);
iterator = list.iterator();
return iterator;
}
/**
* 取记录集合
* @return Iterator
* @param int position, int length
*/
public Iterator getInfos(int position, int length) throws Exception {
Iterator iterator = null;
String queryString = " select info from Info as info order by info.iddesc";
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 取第一条记录
* @throws Exception
* @return Station
*/
public Info getFirstInfo() throws Exception {
Iterator iterator = null;
Info info = null;
String queryString = "select info from Info as info order by info.iddesc";
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
if (iterator.hasNext()) {
info = (Info) iterator.next();
}
return info;
}
/**
* 取最后一条记录
* @throws Exception
* @return Station
*/
public Info getLastInfo() throws Exception {
Iterator iterator = null;
Info info = null;
String queryString = "select info from Info as info order by info.idasc";
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
if (iterator.hasNext()) {
info = (Info) iterator.next();
}
return info;
}
////////////////////////////////////////////////////////
///// ///
///// 以下部份表中要有特定字段才能Õ�确运行� �个人和企业� ///
///// ///
////////////////////////////////////////////////////////
/**
* 取符合条件记录总数, [表中要有 isperson 字段]
* @return int
* @param int isPerson
*/
public int getInfosCountByIsperson(int isPerson) throws Exception {
int count = 0;
String queryString =
"select count(*) from Info as info where info.isperson =" + isPerson;
count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
intValue();
return count;
}
/**
* 取所有符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
* @return Iterator
* @param int isPerson
*/
public Iterator getAllInfosByIsperson(int isPerson) throws Exception {
Iterator iterator = null;
String queryString = " select info from Info as info where info.isperson=" +
isPerson + " order by info.id desc";
List list = getHibernateTemplate().find(queryString);
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 取符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
* @return Iterator
* @param int isPerson,int position, int length
*/
public Iterator getInfosByIsperson(int isPerson, int position, int length)throws
Exception {
Iterator iterator = null;
String queryString = " select info from Info as info where info.isperson=" +
isPerson + " order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
////////////////////////////////////////////////////////
///// ///
///// 以下部份表中要有特定字段才能Õ�确运行� 查询部份 ///
///// ///
///////////////////////////////////////////////////////
/**
* 取符合条件记录总数, 模糊查询条件.[表中要有 title 字段]
* @return int
* @param String text
*/
public int getInfosCount(String text) throws Exception {
int count = 0;
count = ((Integer) getHibernateTemplate().iterate(
"select count(*) from Info as info where info.title like '%" + text +
"%'").next()).intValue();
return count;
}
/**
* 取所有符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
* @return Iterator
* @param String text
*/
public Iterator getAllInfos(String text) throws Exception {
Iterator iterator = null;
String queryString =
" select info from Info as info where info.title like '%" + text +
"%' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 取符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
* @return Iterator
* @param String text,int position, int length
*/
public Iterator getInfos(String text, int position, int length) throws
Exception {
Iterator iterator = null;
String queryString =
" select info from Info as info where info.title like '%" + text +
"%' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
////////////////////////////////////////////////////////
///// ///
///// 以下部份表中要有特定字段才能Õ�确运行� �注册相关� ///
///// ///
////////////////////////////////////////////////////////
/**
* 取符合条件记录总数.[ 表中要有 registername 字段]
* @return int
* @param String text
*/
public int getInfosCountByRegisterName(String registerName) throwsException {
int count = 0;
count = ((Integer) getHibernateTemplate().iterate(
"select count(*) from Info as info where info.registername = '" +
registerName + "'").next()).intValue();
return count;
}
/**
* 通过注册名取得一条记录,如有多条,只取第一条.[表中要有 registername字段]
* @param registername String
* @return Info
*/
public Info getInfoByRegisterName(String registerName) throws Exception {
Iterator iterator = null;
Info info = null;
String queryString =
" select info from Info as info where info.registername='" +
registerName + "' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
if (iterator.hasNext()) {
info = (Info) iterator.next();
}
return info;
}
/**
* 通过注册名取得所有记录集合.[表中要有 registername字段]
* @param registername String
* @return Iterator
*/
public Iterator getAllInfosByRegisterName(String registerName) throws
Exception {
Iterator iterator = null;
String queryString =
" select info from Info as info where info.registername='" +
registerName + "' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 通过注册名取得记录列表.[表中要有 registername字段]
* @param registername String
* @return Iterator
*/
public Iterator getInfosByRegisterName(String registerName, int position,
int length) throws Exception {
Iterator iterator = null;
String queryString =
" select info from Info as info where info.registername='" +
registerName + "' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
////////////////////////////////////////////////////////
///// ///
///// 以下部份表中要有特定字段才能Õ�确运行� �树型版块� ///
///// ///
////////////////////////////////////////////////////////
/**
* 取记录总数.[ 表中要有 board_id 字段]
* @return int
* @param String boardId
*/
public int getInfosCountByBoard(String boardId) throws Exception {
int count = 0;
count = ((Integer) getHibernateTemplate().iterate(
"select count(*) from Info as info where info.boardId = '" + boardId +
"'").next()).intValue();
return count;
}
/**
* 通过版块名取得所有记录集合.[表中要有 board_id字段]
* @param BoardId String
* @return Iterator
*/
public Iterator getAllInfosByBoard(String boardId) throws Exception {
Iterator iterator = null;
String queryString = " select info from Info as info where info.boardId='" +
boardId + "' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 通过版块名取得记录列表.[表中要有 board_id字段]
* @param BoardId String
* @return Iterator
*/
public Iterator getInfosByBoard(String boardId, int position, int length)throws
Exception {
Iterator iterator = null;
String queryString = " select info from Info as info where info.boardId='" +
boardId + "' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
/**
* 取符合条件记录总数.[ 表中要有 board_id 字段,title] 模糊查询title
* @return int
* @param String boardId ,String text
*/
public int getInfosCountByBoard(String boardId, String text) throwsException {
int count = 0;
count = ((Integer) getHibernateTemplate().iterate(
"select count(*) from Info as info where info.boardId='" + boardId +
"' and info.title like '%" + text + "%'").next()).intValue();
return count;
}
/**
* 通过版块名取得记录列表.[表中要有 board_id字段] 模糊查询title
* @param String boardID,int position, int length
* @return Iterator
*/
public Iterator getInfosByBoard(String boardId, int position, int length,
String text) throws Exception {
Iterator iterator = null;
String queryString = " select info from Info as info where info.boardId='" +
boardId + "' and info.title like '%" + text +
"%' order by info.id desc";
//创建查询
Query query = getHibernateTemplate().createQuery(getSession(), queryString);
//设置游标的起始点
query.setFirstResult(position);
//设置游标的长度
query.setMaxResults(length);
//记录生成
List list = query.list();
//把查询到的结果放入迭代器
iterator = list.iterator();
return iterator;
}
////////////////////////////////////////////////////////
///// ///
/////以下部份带有审核功能 ///
///// ///
////////////////////////////////////////////////////////
/**
* 取记录总数
* @return int
* @param int isAuditing
*/
public int getInfosCount(int isAuditing) throws Exception {