行业知识
基于Struts2+Hibernate5实现简易教师信息管理系统
用于新手初次尝试
基于Struts2+Hibernate5框架实现一个教师信息管理系统。
具体步骤如下:
1. 设计数据库和表,如图1所示。
图1 teachinfo表
2. 创建Web项目,配置Struts2和Hibernate5框架的开发环境
3. 编写Hibernate5的配置文件hibernate.cfg.xml。
4. 编写加载配置文件的类HibernateSessionFactory,
5. 编写持久化类Teachinfo和映射文件Teachinfo.hbm.xml。
6. 编写一个类名为TeacherDao的JavaBean,该类封装了对教师信息的增删改查操作。
7. 在web.xml中配置核心控制器;
8.编写系统主页面(index.jsp),以及其对应的LookTeacherAction,该业务控制器主要用于查询所有教师信息,然后跳转到lookTeacher.jsp页面显示查询结果。index.jsp页面效果如图2所示。lookTeacher.jsp页面效果如图3所示
9. 编写查看教师信息页面lookTeacher.jsp,该页面显示查询到的所有教师信息,还有四个超链接分别用来跳转到查看教师信息页面,添加教师信息页面,修改教师信息页面和删除教师信息功能页面。
10. 编写添加教师信息页面addTeacher.jsp,以及对应的AddTeacherAction,该业务控制器实现添加学生信息功能,如果添加成功,继续执行LookTeacherAction,在lookTeacher.jsp页面显示添加操作后所有教师信息。
11. 编写修改教师信息页面findTeacher.jsp,以及对应的FindTeacherAction,该业务控制器查询要修改的教师的信息,然后跳转到更新教师信息页面updateTeacher.jsp,显示查询结果,用户修改教师信息后,请求UpdateTeacherAction,该业务控制器实现修改教师信息功能,如果修改成功,继续执行LookTeacherAction,在lookTeacher.jsp页面显示修改操作后所有教师信息。
12. 编写删除教师信息页面deleteTeacher.jsp,以及对应的DeleteTeacherAction,该业务控制器删除某个教师的信息,如果删除成功,继续执行LookTeacherAction,在lookTeacher.jsp页面显示删除操作后所有教师信息。
13. 在struts.xml文件中配置每一个业务控制器Action。
数据库表设计不局限图1,可根据情况自由发挥,
JSP页面内容和效果也不局限图2和图3,可根据情况自由发挥。
关键代码实现
//部分关键代码如下,全码+struts2+hibernate5配置//v:ershiqijianP1.addTeacherActionpackage p1;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;import java.util.List;public class addTeacherAction extends ActionSupport { private HttpServletRequest request; private String message="error"; private int id; private String name; private String sex; private int age; private String department; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String execute()throws Exception{ TeacherDaoBean Dao=new TeacherDaoBean(); boolean res= Dao.addTeacher(info()); if(res) { message = "success"; } else { message="error"; } return message; } public Teachinfo info(){ Teachinfo info=new Teachinfo(); info.setId(this.getId()); info.setName(this.getName()); info.setSex(this.getSex()); info.setAge(this.getAge()); info.setDepartment(this.getDepartment()); return info; }}p1.deleteTeacherActionpackage p1;import com.opensymphony.xwork2.ActionSupport;public class deleteTeacherAction extends ActionSupport { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String execute(){ String message="error"; try{ TeacherDaoBean Dao=new TeacherDaoBean(); boolean res=Dao.deleteTeacher(this.getId()); message="success"; }catch (Exception e){ e.printStackTrace(); message="error"; } return message; }}p1.findTeacherActionpackage p1;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;import java.util.List;public class findTeacherAction extends ActionSupport { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String execute(){ HttpServletRequest request; request= ServletActionContext.getRequest(); TeacherDaoBean Dao=new TeacherDaoBean(); List list=Dao.findTeacher(this.getId()); request.getSession().setAttribute("findinfo",list); return "success"; }}p1.HibernateSessionFactorypackage p1;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateSessionFactory { private static SessionFactory sessionFactory ; public static Session getSession() { try { // 加载 hibernate.cfg.xml 配置文件 return getSessionFactory().openSession(); }catch (Exception e) { e.printStackTrace(); throw new RuntimeException("file+++111",e); } } public static SessionFactory getSessionFactory() { if(sessionFactory==null||sessionFactory.isClosed()){ sessionFactory=new Configuration().configure().buildSessionFactory(); } return sessionFactory; }}p1.LookTeacherActionpackage p1;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateSessionFactory { private static SessionFactory sessionFactory ; public static Session getSession() { try { // 加载 hibernate.cfg.xml 配置文件 return getSessionFactory().openSession(); }catch (Exception e) { e.printStackTrace(); throw new RuntimeException("file+++111",e); } } public static SessionFactory getSessionFactory() { if(sessionFactory==null||sessionFactory.isClosed()){ sessionFactory=new Configuration().configure().buildSessionFactory(); } return sessionFactory; }}p1.TeacherDaoBeanpackage p1;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.query.Query;import java.util.ArrayList;import java.util.List;public class TeacherDaoBean { private Transaction transaction; private Session session; private Query query; public TeacherDaoBean(){ } public boolean addTeacher(Teachinfo teacher){ session=HibernateSessionFactory.getSession(); try{ transaction=session.beginTransaction(); session.save(teacher); transaction.commit(); session.close(); return true; }catch (Exception e) { e.printStackTrace(); return false; } }....
上一篇:和你一样,你现在有打算了吗
下一篇:宝安做网站怎么样?