基于Struts2+Hibernate5框架实现一个教师信息管理系统。
时间: 2023-06-19 21:07:39 浏览: 365
好的,以下是实现教师信息管理系统的具体步骤:
1. 搭建开发环境:安装JDK、Tomcat、MySQL、Eclipse等开发工具和软件。
2. 创建数据库:创建一个名为teacher_db的数据库,其中包含teacher表,用于存储教师信息。表结构如下:
```sql
CREATE TABLE teacher (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT(11) NOT NULL,
subject VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
```
3. 创建项目:使用Eclipse创建一个基于Struts2+Hibernate5框架的Web项目。
4. 配置数据库连接:在项目的src/main/resources目录下创建hibernate.cfg.xml文件,配置数据库连接信息。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/teacher_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.example.teacher.model.Teacher"/>
</session-factory>
</hibernate-configuration>
```
5. 创建Model类:在src/main/java目录下创建Teacher类,用于映射teacher表。
```java
package com.example.teacher.model;
public class Teacher {
private int id;
private String name;
private String gender;
private int age;
private String subject;
// getter and setter methods
// toString method
}
```
6. 创建DAO类:在src/main/java目录下创建TeacherDAO类,用于操作数据库。
```java
package com.example.teacher.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import com.example.teacher.model.Teacher;
import com.example.teacher.util.HibernateUtil;
public class TeacherDAO {
private SessionFactory sessionFactory;
public TeacherDAO() {
sessionFactory = HibernateUtil.getSessionFactory();
}
public void addTeacher(Teacher teacher) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(teacher);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
public void updateTeacher(Teacher teacher) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(teacher);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
public void deleteTeacher(int id) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Teacher teacher = (Teacher) session.get(Teacher.class, id);
session.delete(teacher);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
public Teacher getTeacherById(int id) {
Session session = sessionFactory.openSession();
Transaction tx = null;
Teacher teacher = null;
try {
tx = session.beginTransaction();
teacher = (Teacher) session.get(Teacher.class, id);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return teacher;
}
public List<Teacher> getAllTeachers() {
Session session = sessionFactory.openSession();
Transaction tx = null;
List<Teacher> teachers = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Teacher");
teachers = query.list();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return teachers;
}
}
```
7. 创建Action类:在src/main/java目录下创建TeacherAction类,用于处理请求和返回响应。
```java
package com.example.teacher.action;
import java.util.List;
import com.example.teacher.dao.TeacherDAO;
import com.example.teacher.model.Teacher;
import com.opensymphony.xwork2.ActionSupport;
public class TeacherAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Teacher teacher;
private List<Teacher> teachers;
private TeacherDAO teacherDAO;
public TeacherAction() {
teacherDAO = new TeacherDAO();
}
public String addTeacher() {
teacherDAO.addTeacher(teacher);
return SUCCESS;
}
public String updateTeacher() {
teacherDAO.updateTeacher(teacher);
return SUCCESS;
}
public String deleteTeacher() {
teacherDAO.deleteTeacher(teacher.getId());
return SUCCESS;
}
public String getTeacherById() {
teacher = teacherDAO.getTeacherById(teacher.getId());
return SUCCESS;
}
public String getAllTeachers() {
teachers = teacherDAO.getAllTeachers();
return SUCCESS;
}
// getter and setter methods
}
```
8. 创建视图:在WebContent目录下创建JSP文件,用于呈现数据和处理用户交互。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师信息管理系统</title>
</head>
<body>
<h1>教师信息管理系统</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>科目</th>
<th>操作</th>
</tr>
<s:iterator value="teachers">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="gender"/></td>
<td><s:property value="age"/></td>
<td><s:property value="subject"/></td>
<td>
<a href="<s:url action='getTeacherById' namespace='/'><s:param name='teacher.id' value='%{id}'/></s:url>">编辑</a>
<a href="<s:url action='deleteTeacher' namespace='/'><s:param name='teacher.id' value='%{id}'/></s:url>">删除</a>
</td>
</tr>
</s:iterator>
<tr>
<td colspan="6"><a href="<s:url action='addTeacher' namespace='/'/>">添加教师</a></td>
</tr>
</table>
<s:if test="teacher != null">
<h2>编辑教师信息</h2>
<s:form action="updateTeacher" namespace="/" method="post">
<s:hidden name="teacher.id" value="%{teacher.id}"/>
<s:textfield label="姓名" name="teacher.name" value="%{teacher.name}"/>
<s:textfield label="性别" name="teacher.gender" value="%{teacher.gender}"/>
<s:textfield label="年龄" name="teacher.age" value="%{teacher.age}"/>
<s:textfield label="科目" name="teacher.subject" value="%{teacher.subject}"/>
<s:submit value="保存"/>
</s:form>
</s:if>
</body>
</html>
```
9. 配置Struts2:在src/main/resources目录下创建struts.xml文件,配置Struts2相关信息。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="getAllTeachers" class="com.example.teacher.action.TeacherAction" method="getAllTeachers">
<result name="success">/teacher.jsp</result>
</action>
<action name="addTeacher" class="com.example.teacher.action.TeacherAction" method="addTeacher">
<result name="success">/teacher.jsp</result>
</action>
<action name="updateTeacher" class="com.example.teacher.action.TeacherAction" method="updateTeacher">
<result name="success">/teacher.jsp</result>
</action>
<action name="deleteTeacher" class="com.example.teacher.action.TeacherAction" method="deleteTeacher">
<result name="success">/teacher.jsp</result>
</action>
<action name="getTeacherById" class="com.example.teacher.action.TeacherAction" method="getTeacherById">
<result name="success">/teacher.jsp</result>
</action>
</package>
</struts>
```
10. 运行项目:将项目部署到Tomcat服务器上,并访问http://localhost:8080/teacher/getAllTeachers,即可查看教师信息列表。
以上就是基于Struts2+Hibernate5框架实现教师信息管理系统的全部内容,希望对你有所帮助。
阅读全文