继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Hibernate整合struts2学习项目笔记

qq_Java小潘_0
关注TA
已关注
手记 10
粉丝 67
获赞 941

Struts与Hibernate整合
1.项目简介
答:(1)Struts2+Hibernate4实现简单的CRUD案例;
(2)完成功能:后台登陆、学生表的增删改查;
(3)环境:struts2+Hibernate4+Mysql6.0;
注意:CRUD是指在做计算处理时的增加(Create)、读取查询(Read)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能;
2.Struts与Hibernate整合
答:详见下图:图片描述(1)配置struts2过滤器:图片描述图片描述(2)struts2的核心jar包:图片描述(3)struts2的模板获取及配置:图片描述图片描述(4)Hibernate的核心jar包:图片描述(5)获取Hibernate的模板文件:图片描述(6)配置Hibernate主配置文件:图片描述图片描述(7)连接mysql用到的jar包:图片描述(8)测试jar包:图片描述
3.创建实体类
答:详见下图:图片描述图片描述图片描述
4.生成实体的对象关系映射文件
答:详见下图:图片描述图片描述图片描述图片描述
5.生成表结构
答:详见下图:图片描述图片描述
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ).buildMetadata();
new SchemaExport(metadata).create(true, true);
用junit来测试:图片描述

《模块开发之用户登录模块一》

6.项目的分层结构
答:详见下图:图片描述图片描述
7.创建自定义的SessionFactor类
答:

public class MyhibernateSeeionFactory {

    private static SessionFactory sessionFactory;
    private MyhibernateSeeionFactory(){

    }
    public static SessionFactory getSessionFactory(){
        if(sessionFactory==null){

             final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
             sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
            //Configuration configuration = new Configuration().configure();
            //ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            //sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;

        }else{
            return sessionFactory;
        }
    }
}

8.用户的业务逻辑接口和实现类
答:详见下图:图片描述
这是用户的业务逻辑接口

package service;

import entity.Users;

//用户业务逻辑接口
public interface UsersDAO {

    //用户登入方法
    public boolean usersLogin(Users u);
}

对应的实现类:

package service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyhibernateSeeionFactory;
import entity.Users;
import service.UsersDAO;

public class UserDAOImpl implements UsersDAO{

    @Override
    public boolean usersLogin(Users u) {
        Transaction tx =  null;
        String hql = "";
        try {
            Session session = MyhibernateSeeionFactory.getSessionFactory().getCurrentSession();
            hql = "from Users u where username=? and password=?  ";
            tx = session.beginTransaction();
            Query query = session.createQuery(hql);
            query.setParameter(0, u.getUsername());
            query.setParameter(1, u.getPassword());
            List list = query.list();
            tx.commit();
            if(list.size()>0){
                return true;
            }else{
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally{
            if(tx != null){
                tx = null;
            }
        }
    }

}

9.设计所有的Action的父类
答:图片描述

package action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class SuperAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware, ServletContextAware {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected ServletContext application;

    @Override
    public void setServletContext(ServletContext application) {
        this.application = application;

    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;

    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        this.session = request.getSession();

    }

}

10.设计用户Action类
答:详见下图:图片描述将用户action注册到struts2(第一个星号表示要找到动作的类,第二个表示表示这个类下的那个方法,表单的设计要和他匹配):图片描述
9.页面调用
答:

<form name="loginForm" action="<%=path%>/users/Users_login.action" method="post">
                    <!-- start of login form -->
                    <div id="welcome">
                        <span id="welcome-text">管 理 登 录</span>
                    </div>
                    <div id="user-name">
                        <span class="item">用户名:</span>
                        <span><input type="text" name="username" class="form-input"></span>
                    </div>
                    <div id="user-password">
                        <span class="item">密   码:</span>
                        <span class="input"><input type="password" name="password" class="form-input"></span>
                    </div>
                    <div id="button-group">
                        <input type="submit" class="btn" value="登录"/>
                        <input type="reset" class="btn" value="重置"/>
                    </div>
                    <div>
                      <s:fielderror/> <!-- 显示表单验证的出错信息 -->
                    </div>
                    <!-- end of form -->
                    </form>

11.完成显示登陆用户名和注销功能
答:
(1)显示登陆用户名:

public String login(){

        session.setAttribute("loginUserName", user.getUsername());

        UsersDAO udao = new UserDAOImpl();
        if(udao.usersLogin(user)){
            return "login_success";
        }else{
            return "login_failure";
        }
    }

在jsp页面上用el表达式:<div id="welcome">欢迎${sessionScope.loginUserName}使用本系统</div>
(2)注销功能:

public String logout(){

        if(session.getAttribute("loginUserName")!=null){
            session.removeAttribute("loginUserName");
        }
        return "logout_success";
    }

在jsp页面上:<div id="logout"><a href="<%=path%>/users/Users_logout.action">安全退出</a></div>
12.完成表单验证功能
答:主要在登陆表单上用来显示报错的信息(可以交给客户端,这里是服务器端):

@Override
public void validate() {
    if("".equals(user.getUsername().trim())){
        this.addFieldError("usernameError", "用户名不能为空!");
    }
    if(user.getPassword().length()<6){
        this.addFieldError("passwordError", "密码不能小于6位!");
    }
}

由于上面这个方法对所有的方法进行验证所以可以用@SkipValidation跳出验证
但是要在jsp页面上显示信息就先引入struts2的表现库:<%@ taglib prefix="s" uri="/struts-tags"%> <s:fielderror/> <!-- 显示表单验证的出错信息 -->
《模块开发之学生管理模块二》
13.显示学生资料-设计学生业务逻辑接口
答:详见下图:图片描述
(1)添加测试数据

@Test
public void testSaveStudents(){
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
    SessionFactory sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();     
    Students s1 = new Students("S0000001","张三丰1","男",new Date(),"武当山");
    Students s2 = new Students("S0000002","郭静","男",new Date(),"台湾岛");
    Students s3 = new Students("S0000003","黄蓉","女",new Date(),"桃花岛");
    session.save(s1);
    session.save(s2);
    session.save(s3);
    session.getTransaction().commit();
//  session.close();

}

(2)设计学生业务逻辑接口

package service;

import java.util.List;

import entity.Students;

//学生的业务逻辑借口

public interface StudentsDAO {

    //查询所有学生信息
    public List<Students> queryAllStudents();

    //学生编号查询学生信息
    public Students queryStudentsById(String sid);

    //添加学生资料
    public boolean addStudents(Students s);

    //删除学生资料
    public boolean deleteStudents(String sid);

    //修改学生资料
    public boolean updateStudents(Students s);
}

(2)设计学生业务逻辑接口实现类并实现查询方法:

@Override
    public List<Students> queryAllStudents() {
        List<Students> list = null;
        String hql = "";
        Session session  = MyhibernateSeeionFactory.getSessionFactory().openSession();
        try {

            hql = " from Students ";
            Query query = session.createQuery(hql);
            session.beginTransaction();
            list = query.list();
            session.getTransaction().commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().commit();
            return list;
        }finally{
            session.close();
        }

    }

测试上面的方法是否有问题:

@Test
public void testQueryAllStudents(){

    StudentsDAO sdao = new StudentsDAOImpl();
    List<Students> list = sdao.queryAllStudents();
    for(int i = 0; i<list.size();i++){
        System.out.println(list.get(i));
    }
}

(4)设计学生Action类:

package action;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;

import entity.Students;
import service.StudentsDAO;
import service.impl.StudentsDAOImpl;

public class StudentsAction extends SuperAction implements ModelDriven<Students>{

    private static final long serialVersionUID = 1L;
    private Students students = new Students();
    //查询所有学生资料的Action
    public String query(){
        StudentsDAO sdao = new StudentsDAOImpl();
        List<Students> list = sdao.queryAllStudents();
        if(list!=null&&list.size()>0){
            session.setAttribute("students_list", list);
        }else{

            session.setAttribute("students_list",null);

        }
        return "query_success";
    }
    //删除学生的Action
    public String delete(){
        StudentsDAO sdao = new StudentsDAOImpl();
        String sid = request.getParameter("sid");
        sdao.deleteStudents(sid);
        return "delete_success";
    }
    //添加学生的Action
    public String add(){
        StudentsDAO sdao = new StudentsDAOImpl();
        sdao.addStudents(students);
        return "add_success";
    }
    //修改学生资料的Action
    public String modify(){
        String sid = request.getParameter("sid");
        StudentsDAO sdao = new StudentsDAOImpl();
        Students s= sdao.queryStudentsById(sid);

        session.setAttribute("modify_students", s);
        return "modify_success";
    }
    //保存修改后的学生Action
    public String save(){
        StudentsDAO sdao = new StudentsDAOImpl();
        sdao.updateStudents(students);
        return "save_success";
    }

    @Override
    public Students getModel() {
        // TODO Auto-generated method stub
        return students;
    }

}

记得这个Action要在struts2的配置文档注册:

<package name="students" namespace="/students" extends="struts-default">
    <action name="*_*" class="action.{1}Action" method="{2}">
        <result name="query_success">/students/Students_query_success.jsp</result>
        <result name="delete_success" type="chain">Students_query</result>
        <result name="add_success">/students/Students_add_success.jsp</result>
        <result name="modify_success">/students/Students_modify.jsp</result>
        <result name="save_success">/students/Students_modify_success.jsp</result>
    </action>
</package>

(5)页面调用:
当点击这里时执行遍历所有学生资料
{ level:2, name:"学生列表", ico:"images/icon_default.gif",link:"students/Students_query.action"}
这里记得假如struts2的标签库:<%@ taglib prefix="s" uri="/struts-tags"%>

<table class="default" width="100%">
    <col width="10%">
    <col width="20%">
    <col width="5%">
    <col width="20%">
    <col width="30%">
    <col width="15%">
    <tr class="title">
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生日期</td>
        <td>地址</td>
        <td>操作</td>
    </tr>

    <!-- 遍历开始 -->
<s:iterator value="#session.students_list" var="stu">
    <tr class="list">
        <td><s:property value="#stu.sid"/></td>
        <td><a href="<%=path%>/students/Students_modify.action?sid=<s:property value="#stu.sid"/>"><s:property value="#stu.sname"/></a></td>
        <td><s:property value="#stu.gender"/></td>
        <td><s:date name="#stu.birthday" format="yyyy年MM月dd日"/></td>
        <td><s:property value="#stu.address"/></td>
        <td><a href="<%=path%>/students/Students_delete.action?sid=<s:property value="#stu.sid"/>" onclick="javascript: return confirm('真的要删除吗?');">删除</a></td>
    </tr>
    </s:iterator> 
    <!-- 遍历结束 -->
</table>

14.删除学生资料
答:详见下图:图片描述
(1)页面调用:<td><a href="<%=path%>/students/Students_delete.action?sid=<s:property value="#stu.sid"/>" onclick="javascript: return confirm('真的要删除吗?');">删除</a></td>
(2)实现删除学生业务逻辑代码:

@Override
    public boolean deleteStudents(String sid) {
        Session session  = MyhibernateSeeionFactory.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            Students s = session.get(Students.class, sid);
            session.delete(s);
            session.getTransaction().commit();
            return true;

        } catch (Exception e) {

            e.printStackTrace();
            session.getTransaction().commit();
            return false;

        }finally{
            session.close();
        }
    }

(3)编写学生Action:

//删除学生的Action
    public String delete(){
        StudentsDAO sdao = new StudentsDAOImpl();
        String sid = request.getParameter("sid");
        sdao.deleteStudents(sid);
        return "delete_success";
    }

记得注册chain表示连接一个动作,也就是说删除成功后再查询所有学生信息,相等页面刷新:
<result name="delete_success" type="chain">Students_query</result>

15.添加学生资料-实现添加学生资料业务逻辑
答:学生学号生成 方法
编写添加学生业务逻辑代码
编写添加学生action
页面调用
(1)学生学号生成 方法(很强大哦!):

private String getNewSid(){
    Session session  = MyhibernateSeeionFactory.getSessionFactory().openSession();
    String hql = null;
    String sid = null;
    try {
        session.beginTransaction();
        hql = "select max(sid) from Students";
        Query query = session.createQuery(hql);
        sid = (String) query.uniqueResult();
        if(sid==null"".equals(sid)){
            sid = "S0000001";
        }else{
            String temp = sid.substring(1);
            int i = Integer.parseInt(temp);
            i++;
            temp = String.valueOf(i);
            int len = temp.length();
            for(int j=0;j<7-len;j++){
                temp = "0"+temp;
            }
            sid = "S"+temp;

        }
        session.getTransaction().commit();
        return sid;
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().commit();
        return null;
    }finally{
        session.close();
    }
}

(2)编写添加学生业务逻辑代码:

@Override
    public boolean addStudents(Students s) {
        Session session  = MyhibernateSeeionFactory.getSessionFactory().openSession();
        s.setSid(getNewSid());
        try {
            session.beginTransaction();
            session.save(s);
            session.getTransaction().commit();
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().commit();
            return false;
        }finally{
            session.close();
        }
    }

(3)编写添加学生action:

//添加学生的Action
    public String add(){
        StudentsDAO sdao = new StudentsDAOImpl();
        sdao.addStudents(students);
        return "add_success";
    }

(4)页面调用 在struts2中注册:

<result name="add_success">/students/Students_add_success.jsp</result>

16.修改学生资料-实现页面显示要修改的学生资料
答:学生学号生成 方法
编写添加学生业务逻辑代码
编写添加学生action
编写修改jsp页面
图片描述
页面调用 :
<td><a href="<%=path%>/students/Students_modify.action?sid=<s:property value="#stu.sid"/>"><s:property value="#stu.sname"/></a></td>

图片描述图片描述
下面这个方法时页面显示学生资料

private String getNewSid(){
        Session session  = MyhibernateSeeionFactory.getSessionFactory().openSession();
        String hql = null;
        String sid = null;
        try {
            session.beginTransaction();
            hql = "select max(sid) from Students";
            Query query = session.createQuery(hql);
            sid = (String) query.uniqueResult();
            if(sid==null"".equals(sid)){
                sid = "S0000001";
            }else{
                String temp = sid.substring(1);
                int i = Integer.parseInt(temp);
                i++;
                temp = String.valueOf(i);
                int len = temp.length();
                for(int j=0;j<7-len;j++){
                    temp = "0"+temp;
                }
                sid = "S"+temp;

            }
            session.getTransaction().commit();
            return sid;
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().commit();
            return null;
        }finally{
            session.close();
        }
    }
//修改学生资料的Action
public String modify(){
    String sid = request.getParameter("sid");
    StudentsDAO sdao = new StudentsDAOImpl();
    Students s= sdao.queryStudentsById(sid);

    session.setAttribute("modify_students", s);
    return "modify_success";
}

在struts.xml中注册:
<result name="modify_success">/students/Students_modify.jsp</result>

jsp页面显示form表单:

<form name="modifyForm" action="<%=path%>/students/Students_save.action" method="post">
<table width="400" >
  <tr>
    <td width="30%">学号:</td>
    <td><input type="text" name="sid" value='<s:property value="#session.modify_students.sid"/>'  readonly="readonly"/></td>
  </tr>
  <tr>
    <td width="30%">姓名:</td>
    <td><input type="text" name="sname" value='<s:property value="#session.modify_students.sname"/>'/></td>
  </tr>
  <tr>
    <td>性别:</td>
    <td>
      <s:if test='%{#session.modify_students.gender=="男"}'>
         <input type="radio" name="gender" value="男" checked="checked"/>男
         <input type="radio" name="gender" value="女"/>女
      </s:if>
      <s:else>
         <input type="radio" name="gender" value="男" />男
         <input type="radio" name="gender" value="女" checked="checked"/>女
      </s:else>
      </td>
  </tr>
  <tr>
    <td>出生日期:</td>
    <td><input name="birthday" type="text" id="control_date" size="20"
      maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" 
      value="<s:date name="#session.modify_students.birthday" format="yyyy-MM-dd"/>"
      />
    </td>
  </tr>
  <tr>
    <td>地址:</td>
    <td><input type="text" name="address" value='<s:property value="#session.modify_students.address"/>'/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input class="button" type="submit" value="修改"></td>
  </tr>
</table>
</form>
打开App,阅读手记
14人推荐
发表评论
随时随地看视频慕课网APP

热门评论

能给项目看看嘛,需要项目验证


写得非常详细,棒极了!

今天正好看在学这个,赞一个!

查看全部评论