天天看點

Hibernate+Spring 對DAO的處理實列

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);

   * 删除記錄

  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;

   * 取記錄集合

   * @param int position, int length

  public Iterator getInfos(int position, int length) throws Exception {

    Query query = getHibernateTemplate().createQuery(getSession(), queryString);

    //設定遊标的起始點

    query.setFirstResult(position);

    //設定遊标的長度

    query.setMaxResults(length);

    //記錄生成

    List list = query.list();

    //把查詢到的結果放入疊代器

   * 取第一條記錄

   * @throws Exception

   * @return Station

  public Info getFirstInfo() throws Exception {

    Info info = null;

    String queryString = "select info from Info as info order by info.iddesc";

    if (iterator.hasNext()) {

      info = (Info) iterator.next();

    }

   * 取最後一條記錄

  public Info getLastInfo() throws Exception {

    String queryString = "select info from Info as info order by info.idasc";

  ///// 以下部份表中要有特定字段才能Õ吩誦袪 牳鋈撕推笠禒    ///

   * 取符合條件記錄總數, [表中要有 isperson 字段]

   * @param int isPerson

  public int getInfosCountByIsperson(int isPerson) throws Exception {

    String queryString =

        "select count(*) from Info as info where info.isperson =" + isPerson;

   * 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]

  public Iterator getAllInfosByIsperson(int isPerson) throws Exception {

    String queryString = " select info from Info as info where info.isperson=" +

                         isPerson + " order by info.id desc";

   * 取符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]

   * @param int isPerson,int position, int length

  public Iterator getInfosByIsperson(int isPerson, int position, int length)throws

      Exception {

    //建立查詢

  ///// 以下部份表中要有特定字段才能Õ吩誦袪  查詢部份      ///

  ///////////////////////////////////////////////////////

   * 取符合條件記錄總數, 模糊查詢條件.[表中要有 title 字段]

   * @param String text

  public int getInfosCount(String text) throws Exception {

    count = ((Integer) getHibernateTemplate().iterate(

        "select count(*) from Info as info where info.title like '%" + text +

        "%'").next()).intValue();

   * 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]

  public Iterator getAllInfos(String text) throws Exception {

        " select info from Info as info where info.title like '%" + text +

        "%' order by info.id desc";

   * 取符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]

   * @param String text,int position, int length

  public Iterator getInfos(String text, int position, int length) throws

  ///// 以下部份表中要有特定字段才能Õ吩誦袪 犠⒉嵯喙貭     ///

   * 取符合條件記錄總數.[ 表中要有 registername 字段]

  public int getInfosCountByRegisterName(String registerName) throwsException {

        "select count(*) from Info as info where info.registername = '" +

        registerName + "'").next()).intValue();

   * 通過注冊名取得一條記錄,如有多條,隻取第一條.[表中要有 registername字段]

   * @param registername String

  public Info getInfoByRegisterName(String registerName) throws Exception {

        " select info from Info as info where info.registername='" +

        registerName + "' order by info.id desc";

   * 通過注冊名取得所有記錄集合.[表中要有 registername字段]

  public Iterator getAllInfosByRegisterName(String registerName) throws

   * 通過注冊名取得記錄清單.[表中要有 registername字段]

  public Iterator getInfosByRegisterName(String registerName, int position,

                                         int length) throws Exception {

  ///// 以下部份表中要有特定字段才能Õ吩誦袪   犑餍桶婵闋    ///

   * 取記錄總數.[ 表中要有 board_id 字段]

   * @param String boardId

  public int getInfosCountByBoard(String boardId) throws Exception {

        "select count(*) from Info as info where info.boardId = '" + boardId +

        "'").next()).intValue();

   * 通過版塊名取得所有記錄集合.[表中要有 board_id字段]

   * @param BoardId String

  public Iterator getAllInfosByBoard(String boardId) throws Exception {

    String queryString = " select info from Info as info where info.boardId='" +

                         boardId + "' order by info.id desc";

   * 通過版塊名取得記錄清單.[表中要有 board_id字段]

  public Iterator getInfosByBoard(String boardId, int position, int length)throws

   * 取符合條件記錄總數.[ 表中要有 board_id 字段,title]  模糊查詢title

   * @param String boardId ,String text

  public int getInfosCountByBoard(String boardId, String text) throwsException {

        "select count(*) from Info as info where info.boardId='" + boardId +

        "' and info.title like '%" + text + "%'").next()).intValue();

   * 通過版塊名取得記錄清單.[表中要有 board_id字段]  模糊查詢title

   * @param String boardID,int position, int length

  public Iterator getInfosByBoard(String boardId, int position, int length,

                                  String text) throws Exception {

                         boardId + "' and info.title like '%" + text +

                         "%' order by info.id desc";

  /////以下部份帶有稽核功能                              ///

   * @param int isAuditing

  public int getInfosCount(int isAuditing) throws Exception {

繼續閱讀