三层架构如下:
pom.xml文件导入相应jar包:
4.0.0
war
studentMsg
com.accp
studentMsg
1.0-SNAPSHOT
4.1.12.Final
2.1.6
org.aspectj
aspectjweaver
1.8.8
org.hibernate
hibernate-core
${hibernate-version}
org.hibernate.common
hibernate-commons-annotations
4.0.5.Final
org.apache.struts
struts2-core
${struts.version}
org.apache.struts.xwork
xwork-core
2.3.34
com.oracle
ojdbc6
11.2.0.1.0
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.2
连接数据库(我用的是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 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 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 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 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 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 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 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 queryClasses(Classes classes) {
return classesDao.queryClasses(classes);
}
}
学生hibernate的映射配置文件(Student.hbm.xml)代码:
seq_student_id
班级hibernate的映射配置文件(Classes.hbm.xml)代码:
seq_classes_id
连接数据库配置文件(hibernate.cfg.xml)代码:
org.hibernate.dialect.Oracle10gDialect
jdbc:oracle:thin:@localhost:1521:orcl
oracle.jdbc.driver.OracleDriver
scott
orcl
true
thread
//映射文件
struts.xml配置代码:
add.jsp
sStudent.action
sClasses.action
show.jsp
web.xml配置代码:
openSessionInView
com.accp.util.MyFilter
openSessionInView
*.action
strut2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
strut2
/*
login.jsp
控制层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 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 getStudentList() {
return studentList;
}
public void setStudentList(List 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 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 getClassesList() {
return classesList;
}
public void setClassesList(List classesList) {
this.classesList = classesList;
}
}
显示学生信息页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
Title
学员信息列表
添加学员
编号
姓名
性别
年龄
电话
email
班级
${id}
${name}
${gender}
${age}
${telephone}
${email}
${classes.name}
插入学生信息页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
Title
配置tomcat启动项目:
运行项目: