三层架构如下:
pom.xml文件导入相应jar包:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>studentMsg</name>
<groupId>com.accp</groupId>
<artifactId>studentMsg</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<hibernate-version>4.1.12.Final</hibernate-version>
<struts.version>2.1.6</struts.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<!--hibernate的jar包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>
<!-- struts2-core的jar包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.34</version>
</dependency>
<!-- oracle 的jar包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
连接数据库(我用的是Oracle):
数据访问层学生实体类代码:
package com.accp.entity;
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
private String telephone;
private String email;
//private Integer classid;
private Classes classes;//班级外键
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
}
数据访问层班级实体类:
package com.accp.entity;
public class Classes {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
会话工厂代码:
package com.accp.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory ;
static {
Configuration conf = new Configuration().configure("hibernate.cfg.xml");
sessionFactory = conf.buildSessionFactory();
}
public static Session openSession(){
return sessionFactory.getCurrentSession();
}
}
过滤器代码:
package com.accp.util;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
Session session = HibernateUtil.openSession();
Transaction transaction = session.beginTransaction();
try {
chain.doFilter(request,response);
transaction.commit();
}catch (Exception e){
transaction.rollback();
}
}
public void destroy() {
}
}
学生Dao层代码:
package com.accp.dao;
import com.accp.entity.Student;
import java.io.Serializable;
import java.util.List;
public interface StudentDao {
List<Student> queryStudent(Student student);
Serializable addStudent(Student student);
}
学生DaoImpl实现代码
package com.accp.dao.impl;
import com.accp.dao.StudentDao;
import com.accp.entity.Student;
import com.accp.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import java.io.Serializable;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
public List<Student> queryStudent(Student student) {
Session session = HibernateUtil.openSession();
Criteria criteria = session.createCriteria(Student.class);
return criteria.list();
}
public Serializable addStudent(Student student) {
Session session = HibernateUtil.openSession();
Serializable save = session.save(student);
return save;
}
}
班级Dao层代码:
package com.accp.dao;
import com.accp.entity.Classes;
import java.util.List;
public interface ClassesDao {
List<Classes> queryClasses(Classes classes);
}
班级DaoImpl实现代码
package com.accp.dao.impl;
import com.accp.dao.ClassesDao;
import com.accp.entity.Classes;
import com.accp.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import java.util.List;
public class ClassesDaoImpl implements ClassesDao {
public List<Classes> queryClasses(Classes classes) {
Session session = HibernateUtil.openSession();
Criteria criteria = session.createCriteria(Classes.class);
return criteria.list();
}
}
学生业务逻辑层代码:
package com.accp.service;
import com.accp.entity.Student;
import java.io.Serializable;
import java.util.List;
public interface StudentService {
List<Student> queryStudent(Student student);
boolean addStudent(Student student);
}
学生业务逻辑层实现代码:
package com.accp.service.impl;
import com.accp.dao.StudentDao;
import com.accp.dao.impl.StudentDaoImpl;
import com.accp.entity.Student;
import com.accp.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
public List<Student> queryStudent(Student student) {
return studentDao.queryStudent(student);
}
public boolean addStudent(Student student) {
if(studentDao.addStudent(student)!=null){
return true;
}
return false;
}
}
班级业务逻辑层代码:
package com.accp.service;
import com.accp.entity.Classes;
import java.util.List;
public interface ClassesService {
List<Classes> queryClasses(Classes classes);
}
班级业务逻辑层实现代码:
package com.accp.service.impl;
import com.accp.dao.ClassesDao;
import com.accp.dao.impl.ClassesDaoImpl;
import com.accp.entity.Classes;
import com.accp.service.ClassesService;
import java.util.List;
public class ClassesServiceImpl implements ClassesService {
private ClassesDao classesDao = new ClassesDaoImpl();
public List<Classes> queryClasses(Classes classes) {
return classesDao.queryClasses(classes);
}
}
学生hibernate的映射配置文件(Student.hbm.xml)代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.accp.entity">
<class name="Student">
<id name="id">
<generator class="sequence">
<param name="sequence">seq_student_id</param>
</generator>
</id>
<property name="name"/>
<property name="age"/>
<property name="gender"/>
<property name="telephone"/>
<property name="email"/>
<many-to-one name="classes" column="classid" class="Classes"/>
</class>
</hibernate-mapping>
班级hibernate的映射配置文件(Classes.hbm.xml)代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.accp.entity">
<class name="Classes">
<id name="id">
<generator class="sequence">
<param name="sequence">seq_classes_id</param>
</generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
连接数据库配置文件(hibernate.cfg.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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">scott</property>
<property name="connection.password">orcl</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
//映射文件
<mapping resource="xml/Classes.hbm.xml"/>
<mapping resource="xml/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
struts.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.ui.theme" value="simple" />
<package name="web" extends="struts-default">
<action name="sClasses" class="com.accp.action.ClassesAction" method="queryClass">
<result name="success">add.jsp</result>
</action>
<action name="add" class="com.accp.action.StudentAction" method="addStudent">
<result name="success" type="redirectAction">sStudent.action</result>
<result name="input" type="redirectAction">sClasses.action</result>
</action>
<action name="sStudent" class="com.accp.action.StudentAction" method="queryStudent">
<result name="success">show.jsp</result>
</action>
</package>
</struts>
web.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>com.accp.util.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>strut2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strut2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
控制层action学生代码:
package com.accp.action;
import com.accp.entity.Student;
import com.accp.service.StudentService;
import com.accp.service.impl.StudentServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
public class StudentAction extends ActionSupport {
private Student student;
private List<Student> studentList;
private StudentService studentService = new StudentServiceImpl();
public String queryStudent(){
studentList = studentService.queryStudent(student);
return SUCCESS;
}
public String addStudent(){
if(studentService.addStudent(student)){
return SUCCESS;
}
return INPUT;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
控制层action班级代码:
package com.accp.action;
import com.accp.entity.Classes;
import com.accp.service.ClassesService;
import com.accp.service.impl.ClassesServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
public class ClassesAction extends ActionSupport {
private Classes classes;
private ClassesService classesService = new ClassesServiceImpl();
private List<Classes> classesList;
public String queryClass(){
classesList = classesService.queryClasses(classes);
return SUCCESS;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public List<Classes> getClassesList() {
return classesList;
}
public void setClassesList(List<Classes> classesList) {
this.classesList = classesList;
}
}
显示学生信息页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="2px" width="100%" align="center">
<h1 align="center">学员信息列表</h1>
<tr align="right">
<td><a href="sClasses.action">添加学员</a></td>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
<td>email</td>
<td>班级</td>
</tr>
<s:iterator value="studentList">
<tr>
<td>${id}</td>
<td>${name}</td>
<td>${gender}</td>
<td>${age}</td>
<td>${telephone}</td>
<td>${email}</td>
<td>${classes.name}</td>
</tr>
</s:iterator>
</table>
</body>
</html>
插入学生信息页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="add.action" method="post">
<table border="2px" width="100%" align="center">
<tr align="center">
<td align="center">
<s:label><h2>学员信息添加</h2></s:label>
</td>
</tr>
<tr>
<td>
姓名:<input type="text" name="student.name"/>
</td>
</tr>
<tr>
<td>
性别:<input type="text" name="student.gender"/>
</td>
</tr>
<tr>
<td>
年龄:<input type="text" name="student.age"/>
</td>
</tr>
<tr>
<td>
电话:<input type="text" name="student.telephone"/>
</td>
</tr>
<tr>
<td>
email:<input type="text" name="student.email"/>
</td>
</tr>
<tr>
<td>
班级:<select name="student.classes.id">
<option value="-1">请选择</option>
<s:iterator value="classesList">
<option value="${id}">${name}</option>
</s:iterator>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="添加"/>
<input type="button" value="返回" onclick="window.history.go(-1)">
</td>
</tr>
</table>
</form>
</body>
</html>
配置tomcat启动项目:
运行项目: