⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinfohibernatedao.java

📁 一个jsp写的bbs
💻 JAVA
字号:
package com.laoer.bbscs.dao.hibernate;

import org.springframework.orm.hibernate3.support.*;
import com.laoer.bbscs.dao.UserInfoDAO;
import com.laoer.bbscs.bean.UserInfo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateCallback;
import java.sql.SQLException;
import org.hibernate.criterion.Order;
import com.laoer.bbscs.bean.Forum;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;
import com.laoer.bbscs.comm.Constant;
import org.hibernate.criterion.Projections;

/**
 * <p>Title: 天乙社区</p>
 *
 * <p>Description: BBSCS</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Laoer.com</p>
 *
 * @author Gong Tianyi
 * @version 7.0
 */
public class UserInfoHibernateDAO
    extends HibernateDaoSupport implements UserInfoDAO {

  private static final String LOAD_BY_USERNAME = "from UserInfo where userName = ?";

  private static final String LOAD_BY_ID = "from UserInfo where id = ?";

  private static final String LOAD_BY_EMAIL = "from UserInfo where email = ?";

  private static final String LOAD_ALL_COUNT = "select count(*) from UserInfo";

  private static final String GET_NUM_BY_GROUPID = "select count(id) from UserInfo where groupID = ?";

  public UserInfoHibernateDAO() {
    super();
  }

  /**
   * 保存或更新UserInfo对象
   *
   * @param userInfo UserInfo
   * @return UserInfo
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public UserInfo saveUserInfo(UserInfo userInfo) {
    this.getHibernateTemplate().saveOrUpdate(userInfo);
    return userInfo;
  }

  /**
   * 根据主键查找UserInfo对象
   *
   * @param id String
   * @return UserInfo
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public UserInfo findUserInfoById(String id) {
    //return (UserInfo)this.getHibernateTemplate().get(UserInfo.class, id);
    List l = this.getHibernateTemplate().find(LOAD_BY_ID, id);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (UserInfo) l.get(0);
    }
  }

  /**
   * 根据用户名查找UserInfo对象
   *
   * @param userName String
   * @return UserInfo
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public UserInfo findUserInfoByUserName(String userName) {
    List l = this.getHibernateTemplate().find(LOAD_BY_USERNAME, userName);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (UserInfo) l.get(0);
    }
  }

  /**
   * 根据Email查找UserInfo对象
   *
   * @param email String
   * @return UserInfo
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public UserInfo findUserInfoByEmail(String email) {
    List l = this.getHibernateTemplate().find(LOAD_BY_EMAIL, email);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (UserInfo) l.get(0);
    }
  }

  /**
   * 取得UserInfo分页列表
   *
   * @param orderby String
   * @param ascordesc String
   * @param firstResult int
   * @param maxResults int
   * @return List
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public List findUserInfoList(final String orderby, final String ascordesc, final int firstResult,
                               final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
        String q = "from UserInfo order by " + orderby + " " + ascordesc;
        Query query = s.createQuery(q);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List list = query.list();
        return list;
      }
    });

  }

  /**
   * 取得所有用户数量
   *
   * @return int
   * @todo Implement this com.laoer.bbscs.dao.UserInfoDAO method
   */
  public int getAllUserNum() {
    List l = this.getHibernateTemplate().find(LOAD_ALL_COUNT);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  public int getUserNumByGroupID(final int groupID) {
    List l = getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException {
        Criteria c = s.createCriteria(UserInfo.class);
        c.setProjection(Projections.count("id"));
        if (groupID != 0) {
          c.add(Restrictions.eq("groupID", new Integer(groupID)));
        }
        return c.list();
      }
    });

    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  public List findUserInfosByGroupID(final int groupID, final String orderby, final int ascOrDesc,
                                     final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException {
        Criteria c = s.createCriteria(UserInfo.class);
        if (groupID != 0) {
          c.add(Restrictions.eq("groupID", new Integer(groupID)));
        }

        if (StringUtils.isNotBlank(orderby)) {
          if (ascOrDesc == Constant.ORDER_ASC) {
            c.addOrder(Order.asc(orderby));
          }
          if (ascOrDesc == Constant.ORDER_DESC) {
            c.addOrder(Order.desc(orderby));
          }
        }
        c.setFirstResult(firstResult);
        c.setMaxResults(maxResults);
        return c.list();
      }
    });
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -