hibernate与struts框架实现增删改查

  这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下:

hibernate.hbm.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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zhenzai?characterEncoding=GBK</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/model/News.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

com.model.News.java配置:

package com.model;
// Generated 2017-3-14 10:57:00 by Hibernate Tools 5.2.0.CR1

import java.util.Date;

/**
 * News generated by hbm2java
 */
public class News implements java.io.Serializable {

    private Integer ids;
    private String title;
    private Date time;
    private String content;

    public News() {
    }

    public News(String title, Date time, String content) {
        this.title = title;
        this.time = time;
        this.content = content;
    }

    public Integer getIds() {
        return this.ids;
    }

    public void setIds(Integer ids) {
        this.ids = ids;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getTime() {
        return this.time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

com.model.News..hbm.xml配置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-14 10:57:01 by Hibernate Tools 5.2.0.CR1 -->
<hibernate-mapping>
    <class name="com.model.News" table="news" catalog="zhenzai" optimistic-lock="version">
        <id name="ids" type="java.lang.Integer">
            <column name="ids" />
            <generator class="identity" />
        </id>
        <property name="title" type="string">
            <column name="title" />
        </property>
        <property name="time" type="timestamp">
            <column name="time" length="19" />
        </property>
        <property name="content" type="string">
            <column name="content" />
        </property>
    </class>
</hibernate-mapping>

com.dao.HibernateUtil.java配置:

package com.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javafx.util.BuilderFactory;

public class HibernateUtil {
    private static final SessionFactory factory = BuilderFactory();
    private static final ThreadLocal<Session> lock = new ThreadLocal<Session>();
    private static SessionFactory BuilderFactory() {
        Configuration config = new Configuration().configure();
        return config.buildSessionFactory();
    }
    public static Session getsession(){
        Session session=lock.get();
        if(session==null){
            session=factory.openSession();
            lock.set(session);
        }
        return session;
    }
    public static void closeSession(){
        Session session=lock.get();
        if(session !=null){
            session.close();
            lock.set(null);
        }
    }
}

com.dao.NewsDao.java配置:

package com.dao;

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

import org.hibernate.Session;

import com.model.News;

public class NewsDao {
    private Session session=null;
    public NewsDao(){
        session=HibernateUtil.getsession();
    }
    public List<News> select(){
        List<News> list = new ArrayList<News>();
        try{
         list = session.createQuery("from News").getResultList();
    
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            HibernateUtil.closeSession();
        }
        return list;
    }
    public News select(int ids){
        News list = new News();
        try{
         list = (News)session.createQuery("from News where ids=?")
                 .setParameter(0,ids)
                 .getSingleResult();
    
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{
            HibernateUtil.closeSession();
        }
        return list;
    }
    public void insert(News news){
        
        try{
             session.beginTransaction();
             session.save(news);
             session.getTransaction().commit();
        
            }catch(Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }
            finally{
                HibernateUtil.closeSession();
            }
    }
    public void update(News news){
        try{
             session.beginTransaction();
             News n=session.get(News.class, news.getIds());
             n.setTitle(news.getTitle());
             n.setTime(news.getTime());
             n.setContent(news.getContent());
             session.update(n);
             session.getTransaction().commit();
        
            }catch(Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }
            finally{
                HibernateUtil.closeSession();
            }
    }
    public void delete(int ids){
        try{
             session.beginTransaction();
             News n = session.get(News.class, ids);
             session.delete(n);
             session.getTransaction().commit();
        
            }catch(Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }
            finally{
                HibernateUtil.closeSession();
            }
    }
    
    
}

接下来就是配置struts的内容:

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    

</web-app>

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="*_*" class="com.controller.{1}Action" method="{2}">
            <result>
                {1}_{2}.jsp
            </result>
        </action>
    </package>

    

</struts>

com.controller.NewsAction.java配置:

package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.dao.NewsDao;
import com.model.News;
import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport {
    private int ids;//创建一个ids字段,供前后端调用时使用
    private News news;//创建一个news对象,供前端调用时直接使用news.xxxx
    public int getIds() {
        return ids;
    }
    public void setIds(int ids) {
        this.ids = ids;
    }
    public News getNews() {
        return news;
    }
    public void setNews(News news) {
        this.news = news;
    }
    public String get(){//对应News_get.jsp界面
        news = new NewsDao().select(ids);
        return SUCCESS;
        
    }
    public String getAll(){//对应News_getAll.jsp界面
        List<News> list = new NewsDao().select();
        HttpServletRequest req = ServletActionContext.getRequest();//后端直接使用request搭建前后端连接
        req.setAttribute("list", list);
        return SUCCESS;
        
    }
    public String add(){//对应News_add.jsp界面
        
        return SUCCESS;
    }
    public String insert(){//对应News_insert.jsp界面
        new NewsDao().insert(news);
        return SUCCESS;
    }
    public String edit(){//对应News_edit.jsp界面
        news = new NewsDao().select(ids);
        return SUCCESS;
    }
    public String update(){//对应News_update.jsp界面
        new NewsDao().update(news);
        return SUCCESS;
    }
    public String delete(){//对应News_delete.jsp界面
        new NewsDao().delete(ids);
        return SUCCESS;
    }
}

News_getAll.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>新闻显示</h1>
<div>
    <c:forEach var="l" items="${list }">//获取getAll方法中request提供的list
        <div>
            <a href="News_get?ids=${l.ids }">${l.ids }&nbsp;&nbsp;${l.title }</a>&nbsp;&nbsp;<a href="News_edit?ids=${l.ids }">修改</a><a href="News_delete?ids=${l.ids }">删除</a>
        </div>
    </c:forEach>
</div>
<div>
<a href="News_add">添加</a>
</div>
</body>
</html>

News_get.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
${news.title }<br><br>//使用的是NewsAction方法中的private News news;
${news.time }<br><br> ${news.content }<br><br> </body> </html>

News_add.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>新闻添加</h1>
<form action="News_insert" method="post">
标题:<input type="text" name="news.title"><br>//使用的是NewsAction方法中的private News news;
时间:<input type="text" name="news.time"><br>
内容:<textarea rows="10" cols="12" name="news.content"></textarea>
<input type="submit" value="添加">
</form>
</body>
</html>

News_insert.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
添加成功
</body>
</html>

News_edit.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="News_update">
<input type="hidden" name="news.ids" value="${news.ids }"><br>//使用的是NewsAction方法中的private News news;
标题:<input type="text" name="news.title" value="${news.title }"><br>
时间:<input type="text" name="news.time" value="${news.time }"><br>
内容:<textarea rows="10" cols="10" name="news.content" >${news.content }</textarea>
<input type="submit" value="修改">
</form>
</body>
</html>

News_update.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
修改成功
</body>
</html>

News_delete.jsp配置:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
删除成功
</body>
</html>

 

转载于:https://www.cnblogs.com/claricre/p/6548923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一.功能简介 1. 实现一个图书管理系统。图书信息存放到一个数据库中。图书包含信息:图书号、图书名、作者、价格、备注字段。 2. 系统要实现如下的基本管理功能: (1)用户分为两类:系统管理员,一般用户。 (2)提供用户注册和用户登录验证功能;其中登录用户的信息有:登录用户名,登录密码等。 (3)管理员可以实现对注册用户的管理(删除),并实现对图书的创建、查询、修改和删除等有关的操作。 (4)一般用户,只能查询图书,并进行借书、还书操作,每个用户最多借阅8本,即当目前借书已经是8本,则不能再借书了,只有还书后,才可以再借阅。 二.涉及技术 Struts2框架Hibernate框架、MySQL数据库、C3P0数据池、Jsp、HTML、CSS、JavaScript等技术。 三.设计思路 1. 基于Struts2框架Hibernate框架进行编程设计,连接MySQL数据库实现数据的增删查改,应用Jsp、HTML、CSS、JavaScript对访问页面进行编写和美化。 2. 分别创建book表和user表,用以存放图书信息和用户数据。其中user表中,设有flag以区分管理员和普通用户。 3. 分别创建Book类和User类,与数据表相对应。每本书和每个用户都有唯一的id与之对应。 4. 创建映射文件User.hbm.xml和Book.hbm.xml。 5. 创建数据库配置文件hibernate.cfg.xml。 6. 创建数据库连接工具类。 7. 设计数据库操作类:UserDao类和BookDao类。UserDao用于实现所有对user表的操作,BookDao用于实现所有对book表的操作。 8. 创建分别对应UserDao类和BookDao类的Action:UserAction和BookAction。采用基于注解的方式进行Action配置。 9. 用户账号分为管理员账号和普通用户账号,注册时加以区分,登录时即可自动判断进入对应的操作主页面。 10. 管理员可实现对用户的查询显示,模糊查询,删除,批量删除,全选和取消全选等功能;可实现对图书的查询显示,模糊查询,添加,删除,批量删除,全选和取消全选等功能。 11. 普通用户可实现借书和还书功能,借书功能通过对book表的查询,将未借出的图书按照id顺序排列显示,点击表格后方的“借阅”按钮,进行确认借阅,将book表中本书的borrowperson列的值改为本用户账号。对于借阅成功的图书可以在“当前借阅”中进行查看。还书功能通过在“当前借阅”中点击“还书”按钮,进行确认还书,将book表中本书的borrowperson列的值改为“空”,本书信息将可以在“借书”界面查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值