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

📄 staffdaohibernate.java

📁 《JSP网站开发典型模块与实例精讲》一书光盘源码
💻 JAVA
字号:
/*
 * Created on 2006-2-3
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.appfuse.dao.hibernate;

import java.util.*;

import org.springframework.orm.ObjectRetrievalFailureException;

import org.appfuse.dao.StaffDAO;
import org.appfuse.model.Staff;
import org.apache.commons.logging.*;
import org.appfuse.util.exception.AppDAOException;
import org.appfuse.model.StaffRole;

/**
 * @author user
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class StaffDAOHibernate
    extends BaseDAOHibernate
    implements StaffDAO {
    protected final Log log = LogFactory.getLog(StaffDAOHibernate.class);
    public Staff getStaff(int staffno) throws AppDAOException {
        Staff user = (Staff) getHibernateTemplate().get(Staff.class,
            new Integer(staffno));
        if (user == null) {
            throw new ObjectRetrievalFailureException(Staff.class,
                new Integer(staffno));
        }
        return user;
    }

    public Staff getStaff(String staffname, String staffpwd) throws
        AppDAOException {
        List list = getHibernateTemplate().find(
            "from Staff where staffname=? and staffpwd=?",
            new String[] {staffname, staffpwd});
        if (list != null && list.size() >= 1) {
            return (Staff) list.get(0);
        }
        return null;
    }

    public List getStaffs(Staff staff) throws AppDAOException {
        log.debug("in StaffDAOHibernate");
        return getHibernateTemplate().find("from Staff");
    }

    public void saveStaff(final Staff staff) throws AppDAOException {
        getHibernateTemplate().saveOrUpdate(staff);
        getHibernateTemplate().flush();
    }

    //删除员工的时候,还需要删除员工角色
    public void removeStaff(int staffno) throws AppDAOException {
        Staff staff = getStaff(staffno);
        removeStaffRole(staff);
        getHibernateTemplate().delete(staff);
        log.debug("in StaffDAO: delete staff");
    }

    public List getRoles(int staffno) throws AppDAOException {
        //员工已赋予的角色
        List list = getHibernateTemplate().find(
            "select b from StaffRole a, Role b where a.roleid = b.roleid and a.staffno = ?",
            new Integer(staffno));
        return list;
    }

//    private List getStaffRoles(Staff staff) {
//        return getHibernateTemplate().find(
//            "from StaffRole a where a.staffno = " + staff.getStaffno());
//    }
    private void removeStaffRole(Staff staff) {
        List list = getHibernateTemplate().find(
            "from StaffRole a where a.staffno = " + staff.getStaffno());
        for (int i = 0; i < list.size(); i++) {
            StaffRole staffRole = (StaffRole) list.get(i);
            getHibernateTemplate().delete(staffRole);
        }
    }

//    private List getStaffRole(StaffRole staffRole) {
//        return (List) getHibernateTemplate().find(
//            "from StaffRole a where a.staffno = " +
//            staffRole.getStaffno() +
//            " and a.roleid = " + staffRole.getRoleid());
//    }
//

//    public void removeStaffRole(StaffRole staffRole) {
//        List list = getHibernateTemplate().find(
//            "from StaffRole a where a.staffno = " +
//            staffRole.getStaffno() +
//            " and a.roleid = " + staffRole.getRoleid());
//
//        for (int i = 0; i < list.size(); i++) {
//            staffRole = (StaffRole) list.get(i);
//            getHibernateTemplate().delete(staffRole);
//        }
//    }
}

⌨️ 快捷键说明

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