基于Struts2+Hibernate的学生信息管理系统实例

目录结构如下:


在mysql数据库中,建立一个数据库 stu,建立一张表stuinfo,如下图所示:



1.新建一个web project,命名为ch15,然后导入struts 2核心包和hibernate核心包,导入方法是:右击ch15这个项目,选择倒数第二项:,myeclipse,选择Add Struts 2...,导入hibernate的核心包也是一样。

2.首先要导入struts 2 核心包和hibernate核心包,但是struts 2 的核心包内的 antlr-2.x.x.jar包要删掉,删除方法是:window——>preferences——>Myeclipse——>Project  Capabilitie——>Struts 2. 然后选中右边的antlr-2.x.x.jar包,选择右边的remove,就OK啦

3.在src下建立四个包,分别为addHibernateFile, Dao,  PO,  studentAction

4.addHibernateFile下的代码(HibernateSessionFactory.java):

package addHibernateFile;

import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
    private static SessionFactory sessionFactory;
    private static Configuration configuration=new Configuration();
    public HibernateSessionFactory(){
    }
    static{
        try{
            Configuration configure = configuration.configure("hibernate.cfg.xml");
            sessionFactory=configure.buildSessionFactory();
        }catch(Exception e){
            message("生成SessionFactoyr失败:"+e);
        }
    }
    public static Session getSession(){
        return sessionFactory.openSession();
    }
    public static void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

5.Dao包下面的代码(StudentDao.java):

package Dao;

import addHibernateFile.HibernateSessionFactory;
import PO.Stuinfo;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class StudentDao {
    private Transaction transaction;
    private Session session;
    private Query query;
    public StudentDao(){
    }
    public boolean  saveInfo(Stuinfo info){
        try{
            session=HibernateSessionFactory.getSession();
            transaction=session.beginTransaction();
            session.save(info);
            transaction.commit();
            session.close();
            return true;
        }catch(Exception e){
            message("saveInfo.error:"+e);
            e.printStackTrace();
            return false;
        }
    }
    public List findInfo(String type,Object value){
        session=HibernateSessionFactory.getSession();
        try{
            transaction=session.beginTransaction();
            String queryString="from Stuinfo as model where model."+type+"=?";
            query=session.createQuery(queryString);
            query.setParameter(0, value);
            List list=query.list();
            transaction.commit();
            session.close();
            return list;
        }catch(Exception e){
            message("findInfo.error:"+e);
            e.printStackTrace();
            return null;
        }
    }
    public List findAllInfo(){
        session=HibernateSessionFactory.getSession();
        try{
            transaction=session.beginTransaction();
            String queryString="from Stuinfo";
            query=session.createQuery(queryString);
            List list=query.list();
            transaction.commit();
            session.close();
            return list;
        }catch(Exception e){
            message("findInfo.error:"+e);
            e.printStackTrace();
            return null;
        }
    }
    public boolean deleteInfo(String id){
        try{
            session=HibernateSessionFactory.getSession();
            transaction=session.beginTransaction();
            Stuinfo info=new Stuinfo();
            info=(Stuinfo)session.get(Stuinfo.class, id);
            session.delete(info);
            transaction.commit();
            session.close();
            return true;
        }catch(Exception e){
            message("deleteInfo.error:"+e);
            e.printStackTrace();
            return false;
        }
    }
    public boolean updateInfo(Stuinfo info){
        try{
            session=HibernateSessionFactory.getSession();
            transaction=session.beginTransaction();
            session.update(info);
            transaction.commit();
            session.close();
            return true;
        }catch(Exception e){
            message("updateInfo.error:"+e);
            e.printStackTrace();
            return false;
        }
    }
    public void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

6.PO包下面的代码:

(1)Stuinfo.java

package PO;
public class Stuinfo  implements java.io.Serializable {
     private String id;
     private String name;
     private String sex;
     private int age;
     private float weight;
     public String getId() {
        return this.id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return this.sex;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public float getWeight() {
        return this.weight;
    }
    
    public void setWeight(float weight) {
        this.weight = weight;
    }
}


(2)Stuinfo.hbm.xml





    
        
            
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    


7.StudentAction包下的代码:

(1)AddMessageAction.java

package studentAction;

import Dao.StudentDao;
import PO.Stuinfo;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.swing.JOptionPane;

public class AddMessageAction extends ActionSupport{
    private String id;
    private String name;
    private String sex;
    private int age;
    private float weight;
    private String message="input";
    public String getId() {
        return id;
    }
    public void setId(String 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 float getWeight() {
        return weight;
    }
    public void setWeight(float weight) {
        this.weight = weight;
    }
    public void validate(){
        if(this.getId()==null||this.getId().length()==0){
            addFieldError("id","学号不允许为空!");
        }else{
            StudentDao dao=new StudentDao();
            List list=dao.findInfo("id", this.getId());
            if(!list.isEmpty()){
                addFieldError("id","学号已存在!");
            }
        }
        if(this.getName()==null||this.getName().length()==0){
            addFieldError("name","姓名不允许为空!");
        }
        if(this.getAge()>130){
            addFieldError("age","请认真核实年龄!");
        }
        if(this.getWeight()>500){
            addFieldError("weight","请认真核实体重!");
        }
    }
    public String execute() throws Exception{
        StudentDao dao=new StudentDao();
        boolean save=dao.saveInfo(info());
        if(save){
            message="success";
        }
        return message;
    }
    public Stuinfo info(){
        Stuinfo info=new Stuinfo();
        info.setId(this.getId());
        info.setName(this.getName());
        info.setSex(this.getSex());
        info.setAge(this.getAge());
        info.setWeight(this.getWeight());
        return info;
    }
    public void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

(2)DeleteMessageAction.java

package studentAction;

import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import javax.swing.JOptionPane;

public class DeleteMessageAction extends ActionSupport{
    private String id;
    private String message;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void validate(){
        if(this.getId().equals("null")){
            message("暂无学生信息!");
            addFieldError("id","暂无学生信息!");
        }
    }
    public String execute() throws Exception{
        StudentDao dao=new StudentDao();
        boolean del=dao.deleteInfo(this.getId());
        if(del){
            message="success";
        }
        return message;
    }
    public void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

(3)FindMessageAction.java

package studentAction;

import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.ServletActionContext;

public class FindMessageAction extends ActionSupport{
    private String id;
    private HttpServletRequest request;
    private String message="input";
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void validate(){
        if(this.getId().equals("null")){
            message("暂无学生信息!");
            addFieldError("id","暂无学生信息!");
        }
    }
    public String execute() throws Exception{
        request=ServletActionContext.getRequest();
        StudentDao dao=new StudentDao();
        List list=dao.findInfo("id", this.getId());
        request.getSession().setAttribute("oneInfo", list);
        message="success";
        return message;
    }
    public void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

(4)LookMessageAction.java

package studentAction;

import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class LookMessageAction extends ActionSupport{
    private HttpServletRequest request;
    private String message="input";
    public String execute() throws Exception{
        request=ServletActionContext.getRequest();
        StudentDao dao=new StudentDao();
        List list=dao.findAllInfo();
        request.getSession().setAttribute("count", list.size());
        request.getSession().setAttribute("allInfo", list);
        message="success";
        return message;
    }
}

(5)UpdateMessageAction.java

package studentAction;

import Dao.StudentDao;
import PO.Stuinfo;
import com.opensymphony.xwork2.ActionSupport;
import javax.swing.JOptionPane;

public class UpdateMessageAction extends ActionSupport{
    private String id;
    private String name;
    private String sex;
    private int age;
    private float weight;
    private String message="input";
    public String getId() {
        return id;
    }
    public void setId(String 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 float getWeight() {
        return weight;
    }
    public void setWeight(float weight) {
        this.weight = weight;
    }
    public void validate(){
        if(this.getName()==null||this.getName().length()==0){
            addFieldError("name","姓名不允许为空!");
        }
        if(this.getAge()>130){
            addFieldError("age","请认真核实年龄!");
        }
        if(this.getWeight()>500){
            addFieldError("weight","请认真核实体重!");
        }
    }
    public String execute() throws Exception{
        StudentDao dao=new StudentDao();
        boolean update=dao.updateInfo(info());
        if(update){
            message="success";
        }
        return message;
    }
    public Stuinfo info(){
        Stuinfo info=new Stuinfo();
        info.setId(this.getId());
        info.setName(this.getName());
        info.setSex(this.getSex());
        info.setAge(this.getAge());
        info.setWeight(this.getWeight());
        return info;
    }
    public void message(String mess){
        int type=JOptionPane.YES_NO_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null, mess, title, type);
    }
}

8.hibernate.cfg.xml




  
    org.hibernate.dialect.MySQLDialect
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/stu	
    root	
    1234	
    
  


9.struts.xml




    
    
     
        
            /student/lookMessage.jsp
            /student/index.jsp
        
        
            lookMessageAction
            /student/addMessage.jsp
        
        
            /student/updateMessage.jsp
            /student/findMessage.jsp
        
        
            lookMessageAction
            /student/updateMessage.jsp
        
        
            lookMessageAction
            /student/deleteMessage.jsp
        
    


10.context.xml




11.WebRoot下的student文件夹下的jsp文件代码:

(1)addMessage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>


    
        
        <s:text name="学生信息管理系统-增加"/>
    
    
        
            

查看学生信息 添加学生信息 修改学生信息 删除学生信息


添加学生信息

(2)deleteMessage.jsp

<%@page import="PO.Stuinfo"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags"%>


    
        
        <s:text name="学生信息管理系统-删除"/>
    
    
        
            

查看学生信息 添加学生信息 修改学生信息 删除学生信息





删除学生信息
请选择要删除学生的学号:

(3)findMessage.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="PO.Stuinfo"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>


    
        
        <s:text name="学生信息管理系统-修改"/>
    
    
        
            

查看学生信息 添加学生信息 修改学生信息 删除学生信息





修改学生信息
请选择要修改学生的学号:

(4)index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>


    
        
        <s:text name="基于SH的学生信息管理-起始页"/>
    
    
        
            




基于Struts2+Hibernate的学生信息管理系统实例,可对学生信息进行增、删、改、查!


点此进入

(5)lookMessage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.ArrayList,PO.Stuinfo"%>
<%@taglib  prefix="s" uri="/struts-tags" %>


    
        
        学生信息管理系统-查看
    
    
        
        

查看学生信息 添加学生信息 修改学生信息 删除学生信息





你要查询的数据表中共有<%=request.getSession().getAttribute("count")%>人
<% ArrayList list=(ArrayList)session.getAttribute("allInfo"); if(list.isEmpty()){ %> <% }else{ for(int i=0;i <% } } %>
记录条数 学号 姓名 性别 年龄 体重
暂无学生信息!
<%=i+1%> <%=info.getId()%> <%=info.getName()%> <%=info.getSex()%> <%=info.getAge()%> <%=info.getWeight()%>

(6)updateMessage.jsp

<%@page import="PO.Stuinfo"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>


    
        
        学生信息管理系统-修改
    
    
        
            

查看学生信息 添加学生信息 修改学生信息 删除学生信息





修改学生信息
<% ArrayList list=(ArrayList)session.getAttribute("oneInfo"); Stuinfo info=(Stuinfo)list.get(0); %>
学号
姓名
性别
年龄
体重
返回

12.web.xml



    
        struts2
        org.apache.struts2.dispatcher.FilterDispatcher
    
    
        struts2
        /*
    
    
        
            30
        
    
    
        index.jsp
    



你可能感兴趣的:(SSH整合系列)