package action; import Bean.*; import org.hibernate.Session; import tools.HibernateTools; import java.util.List; public class Teacher extends HibernateTools { private int teaid; private String name; private int age; private String sex; private String dept; private String course_name; private String tpd; private String tpdcheck; private Session session; private int delkey; private List<ClassTeacherEntity> classteacherEntities; //private List<Object[]> classteacherEntities; private int classid; private List<TeacherEntity> teaEntities; public String addteacher(){ showclassteacher(); if (teaid==0||name==null||sex==null||age==0||dept==null||course_name==null){ addActionError("请输入完整的教师信息"); return INPUT; } if (!tpd.equals(tpdcheck)){ addActionError("两次密码不完全相同"); return INPUT; } if (session==null||!session.isOpen()){ session=init(); } TeacherEntity teaEntity=new TeacherEntity(); teaEntity.setTeacherId(teaid); if (session.get(TeacherEntity.class, teaid)!=null){ addActionError("该老师已经添加,请检查教师号"); return ERROR; } teaEntity.setName(name); teaEntity.setPassword(tpdcheck); teaEntity.setName(name); teaEntity.setAge(age); teaEntity.setSex(sex); teaEntity.setCourseName(course_name); teaEntity.setDept(dept); session.save(teaEntity); destory(); showclassteacher(); addActionError("添加成功"); return SUCCESS; } public String delteacher(){ showclassteacher(); if (session==null||!session.isOpen()){ session=init(); } if (delkey==0){ return INPUT; } TeacherEntity teaEntity=session.get(TeacherEntity.class, delkey); session.delete(teaEntity); destory(); showclassteacher(); return SUCCESS; } public String showteacher(){ if (session==null||!session.isOpen()){ session=init(); } teaEntities=session.createQuery("from TeacherEntity").getResultList(); destory(); return SUCCESS; } public String addteaclass(){//增加教师授课关系 showclassteacher(); if (classid==0||teaid==0){ addActionError("请输入教师id和班级id"); return INPUT; } if (session==null||!session.isOpen()){ session=init(); } if (session.get(ClassEntity.class, classid)==null){ addActionError("输入的班级不存在"); return INPUT; } if (session.get(TeacherEntity.class, teaid)==null){ addActionError("输入的教师不存在"); return INPUT; } List<ClassTeacherEntity> classteacherEntities=session.createQuery("from ClassTeacherEntity where classId=? and teacherId=?") .setParameter(0, classid) .setParameter(1, teaid) .getResultList(); if (classteacherEntities!=null&&classteacherEntities.size()!=0){ addActionError("关系已经建立,请不要重复建立"); return ERROR; } ClassTeacherEntity classteacherEntity=new ClassTeacherEntity(); classteacherEntity.setClassId(classid); classteacherEntity.setTeacherId(teaid); session.saveOrUpdate(classteacherEntity); destory(); showclassteacher();//再次更新数据 addActionError("关系建立成功"); return SUCCESS; } public String showclassteacher(){ if (session==null||!session.isOpen()){ session=init(); } classteacherEntities=session.createQuery("from ClassTeacherEntity " ) .getResultList(); destory(); return SUCCESS; } public String delclassteacher(){ showclassteacher(); if (session==null||!session.isOpen()){ session=init(); } if (delkey==0){ addActionError("出现异常"); return ERROR; } if (session.get(ClassTeacherEntity.class, delkey)==null){ addActionError("该关系不存在"); return ERROR; } session.delete(session.get(ClassTeacherEntity.class, delkey)); destory(); return SUCCESS; } public int getTeaid() { return teaid; } public void setTeaid(int teaid) { this.teaid = teaid; } public String getTeaname() { return name; } public void setTeaname(String name) { this.name = name; } public String getTpd() { return tpd; } public void setTpd(String tpd) { this.tpd = tpd; } public String getTpdcheck() { return tpdcheck; } public void setTpdcheck(String tpdcheck) { this.tpdcheck = tpdcheck; } public int getDelkey() { return delkey; } public void setDelkey(int delkey) { this.delkey = delkey; } public List<TeacherEntity> getTeaEntities() { return teaEntities; } public void setTeaEntities(List<TeacherEntity> teaEntities) { this.teaEntities = teaEntities; } public int getClassid() { return classid; } public void setClassid(int classid) { this.classid = classid; } public List<ClassTeacherEntity> getClassteacherEntities() { return classteacherEntities; } public void setClassteacherEntities(List<ClassTeacherEntity> classteacherEntities) { this.classteacherEntities = classteacherEntities; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getCourse_name() { return course_name; } public void setCourse_name(String course_name) { this.course_name = course_name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }