使用struts2+hibernate的增删改查学生管理系统

工程环境:MyEclipse8.5

其他配置:Hibernate框架+jtds链接数据库驱动+Sql2008数据库+MyEclipse serevr+JDK1.7

开发环境:Win7x64

这个项目用到的jtds数据库链接驱动需要导入jdts-1.2.jar包

1.先创建数据库:

两张表cls(班级班)、stu(学生表)

create database student --创建学生数据库
use student
create table cls (
cls_id char(10) primary key,--主键
cls_name char(15)
); 
create table stu (
stu_id int primary key,--主键
stu_name char(15),
stu_age int,
id char(10) foreign key(id) references class (id)--外键
);

2.student工程

2.1创建web工程项目student

2.2加载struts2

项目名右键>MyEclipse>Add struts

2.2加载hibernate3.3

项目名右键>MyEclipse>Add hibernate

2.3映射数据库

打开数据库DB Browser>new...>...>打开数据库>选择两张表>映射>OK

2.4写增删改查方法

2.41查询(主界面就是一个查询,点击班级标签显示这个班级的所有学生也是查询,详细 是根据学生ID进行查询输出学生信息,所以不一一举出)

主界面ok_n.jsp,由于主界面的数据在数据库中取出,所以要先访问fianall.action的test方法在数据库中取出数据,再在页面遍历出来

index.jsp跳转临时网页

<script language="javascript">
    window.location.href="fianall.action";
</script>

到test.java中查询数据,返回到ok_n.jsp主页面中

<action name="fianall" class="work.test">
    <result name="0">/ok.jsp</result>
    <result name="1">/ok_n.jsp</result>
</action>

test.java

package work;

import java.util.ArrayList;
import java.util.List;

import com.Stu;
import com.StuDAO;

public class test {

    public List<Stu> i = new ArrayList<Stu>();
    
    public String execute()
     { 
        //System.out.println("1");
        
        StuDAO  In=new StuDAO();
        i=In.findAll();
        return "1";
     }
}

ok_n.jsp

<table class="bordered">
          <tr>
                  <td><a href="add_news.action">增加学生</a></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td><a href="fianall.action">刷新</a></td>    
          </tr>
          <tr>
                  <th>学生编号</th>
                  <th>学生姓名</th>
                  <th>学生年级</th>
                  <th>学生班级</th>
                  <th>操作</th>
          </tr>
         <s:iterator value="i">
             <tr>
                <td><s:property value="stuId"/></td>
                <td><s:property value="stuName"/></td>
                <td><s:property value="age"/></td>
                <td><a href="only.action?cls_id=${cls.clsId }"><s:property value="cls.clsName"/></a></td>
                <td>
                    <a href="del.action?stu_id=${stuId }&age=${age}&stu_name=${stuName}&cls_id=${cls.clsId }">删除</a>
                    <a href="up.action?stu_id=${stuId }">修改</a>
                    <a href="news.action?stu_id=${stuId }">详细</a>
                </td>
            </tr>
        </s:iterator>
    </table>

3个查询效果,主页、班级所有学生、学生信息

2.42增加学生

<td><a href="add_news.action">增加学生</a></td>

点击 增加学生 页面标签到add_news.action,在add_news.java中取出班级数据,在add.jsp页面遍历出班级

<action name="add_news" class="work.add_news">
    <result name="1">/add.jsp</result>
</action>

add_news.java

package work;

import java.util.ArrayList;
import java.util.List;

import com.Cls;
import com.ClsDAO;

public class add_news {

    public List<Cls> i = new ArrayList<Cls>();
    
    public String execute()
     { 
        //System.out.println("1");
        
        ClsDAO  In=new ClsDAO();
        i=In.findAll();
        return "1";
     }
}

add.jsp

<form action="add.action" method="post">
      <table class="bordered">
          <tr>
            <th><p>增加学生<s:property value="news"/></p></th>
        </tr>
        <tr>
            <td><p>输入学生姓名:<input type="text" name="name"></p></td>
        </tr>
        <tr>
            <td><p>输入学生年龄:<input type="text" name="age"></p></td>
        </tr>
        <tr>
            <td><p>选择所属班级:
                <select name="cls_name">

                    <s:iterator value="i">
                        <option value="${clsId }">${clsName }</option>
                    </s:iterator>

                </select>
                <input type="submit" value="增加">
              </p></td>
          </tr>
          <tr>
            <td><a href="fianall.action">返回</a></td>
        </tr>
     </table>
      </form>

点击增加按钮提交表单到add.action,在add.java中save数据,返回到增加学生页面

<action name="add" class="work.add">
    <result name="1">/add.jsp</result>
</action>

add.java

package work;

import com.Cls;
import com.Stu;
import com.StuDAO;

    public class add {

        public String name;//姓名
        public Integer age;//年龄
        public String cls_name;//班级号
        //public String news;
    
        public String execute ()
        {
            
            try{
                //System.out.println(name);
                //System.out.println(age);
                //System.out.println(cls_name);
                
                Cls s=new Cls();
                s.setClsId(cls_name);
                
                Stu Stu_add = new Stu(s,name,age);
                
                StuDAO add = new StuDAO();
                add.save(Stu_add);//调用dao里面自带的seve方法(修改后),下面是修改后的
                //System.out.println(s.getClsId());
            }
            
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        //news="添加成功";
        return "1"; 
        }
}

hibernate DAO里面的save增加了事务

public void save(Stu transientInstance) {
        log.debug("saving PersonInf instance");
        try {
            
            Session s=getSession();//事务
            Transaction t=s.beginTransaction();
            
            getSession().save(transientInstance);
            
            t.commit();
            
            log.debug("save succes敏感词ul");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

2.43删除学生

<a href="del.action?stu_id=${stuId }&age=${age}&stu_name=${stuName}&cls_id=${cls.clsId }">删除</a>

点击 删除 标签到del.action,在del.java中删除数据

<action name="del" class="work.del">
    <result name="1">/index.jsp</result>
</action>

del.java

package work;

import java.util.ArrayList;
import java.util.List;

import com.Stu;
import com.StuDAO;

public class del {
    
    public String stu_id;//得到学生编号
    public List<Stu> i = new ArrayList<Stu>();
    
    public String execute()
     { 
      StuDAO  In=new StuDAO();
       i=In.findstuid(stu_id);//用学生编号查询出学生信息LIST

       In.delete(i.get(0));//用GET(0)到学生,用DAO里面的Ddelete方法del学生,DAO 里面的 delete方法也要添加事务方可正常执行
        return "1";
     }
}

hibernate DAO里面的delete增加了事务

public void delete(Stu persistentInstance) {
        log.debug("deleting Stu instance");
        try {
            //----------------事务
            Session s=getSession();
            Transaction t=s.beginTransaction();
            
            getSession().delete(persistentInstance);
            
            t.commit();
            log.debug("delete succes敏感词ul");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

2.44修改学生

<a href="up.action?stu_id=${stuId }">修改</a>

点击 修改 标签到up.action,在up.java中查到数据,并在up.jap中遍历出来

<action name="up" class="work.up">
    <result name="1">/up.jsp</result>
</action>

up.jsp

<s:iterator value="i">    
     <form action="updat.action?stuId=${stuId }&cls_id=${cls.clsId}" method="post">
    <table class="bordered">
            <tr><th><P>修改</th></tr>
             <s:iterator value="i">
                <tr><td><P>学生编号:<s:property value="stuId"/></P></td></tr>
                <tr><td><P>学生名称:<input type="text" name="stu_name" value="${stuName }"></P></td></tr>
                <tr><td><P>学生年龄:<input type="text" name="age" value="${age }"></P></td></tr>
                <tr><td><P>学生班级:<s:property value="cls.clsName"/></P></td></tr>
            </s:iterator>
            <tr><td><input type="submit" value="更新"></td></tr>
            <tr><td><a href="fianall.action">返回</a></td></tr>
            
    </table>
        </form>
</s:iterator>

修改好数据,点击更新按钮提交到updat.action,到updat.java中修改,成功后返回主界面

<action name="updat" class="work.updat">
    <result name="1">/index.jsp</result>
</action>

updat.java

package work;

import java.util.List;

import com.Cls;
import com.ClsDAO;
import com.Stu;
import com.StuDAO;

public class updat {
    
    public Integer stuId;
    public String cls_id;
    public String stu_name;
    public Integer age;
    
    public String execute() throws Exception
    {
       
      Stu students=new Stu();
      StuDAO studentsDAO=new StuDAO();
       students.setStuId(stuId);
       students.setAge(age);
       students.setStuName(stu_name);

       Cls classes=new Cls();
       classes.setClsId(cls_id);
       ClsDAO classesDAO=new ClsDAO();
       List list= classesDAO.findByExample(classes);
       for(Object object:list)
       {
           classes=(Cls)object;
       }
        students.setCls(classes);
        studentsDAO.merge(students);//更新需要添加事务
        return "1";
    }
}

hibernate DAO里面的merge增加了事务

public Stu merge(Stu detachedInstance) {
        log.debug("merging Students instance");
        try {
            //事务
            Session session=getSession();
            Transaction transaction=session.beginTransaction(); 
            Stu result = (Stu) getSession().merge(detachedInstance);
            transaction.commit();
            log.debug("merge succes敏感词ul");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

最终结构

全部评论
你的only.action是什么
点赞 回复 分享
发布于 2017-12-03 14:58

相关推荐

双非坐过牢:非佬,可以啊10.28笔试,11.06评估11.11,11.12两面,11.19oc➕offer
点赞 评论 收藏
分享
评论
点赞
4
分享
牛客网
牛客企业服务