Struts2+Hibernate实现图书管理系统

效果图

登录页面
学生列表
书籍列表

部分代码

Books.java

package entity;

import java.util.Date;

public class Books {
    //书籍编号
    private String sid;
    //书名
    private String sname;
    //借书日期
    private Date loandate;
    //书籍剩余数量
    private String total;


    public Books() {

    }

    public Books(String sid, String sname, Date loandate, String total) {
        //super();
        this.sid = sid;
        this.sname = sname;
        this.loandate = loandate;
        this.total = total;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Date getLoandate() {
        return loandate;
    }

    public void setLoandate(Date loandate) {
        this.loandate = loandate;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "Books [sid=" + sid + ", sname=" + sname + ", loandate="
                + loandate + ", total=" + total + "]";
    }


}

Books.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">
<hibernate-mapping>
    <class name="entity.Books" table="BOOKS">
        <!-- 主键 -->
        <id name="sid" type="java.lang.String" length="8">
            <!-- 主键生成方式:自动生成 -->
            <generator class="assigned"></generator>
        </id>
        <property name="sname" type="java.lang.String"></property>
        <property name="loandate" type="date"></property>
        <property name="total" type="java.lang.String"></property>
    </class>
</hibernate-mapping>

BooksAction.java

package action;

import java.text.SimpleDateFormat;
import java.util.List;

import service.BooksDAO;
import service.impl.BooksDAOImpl;
import entity.Books;

public class BooksAction extends SuperAction {
    private static final long serialVersionUID = 1L;

    // 查询所有书籍
    public String query() {
        BooksDAO sdao = new BooksDAOImpl();
        List<Books> list = sdao.queryAllBooks();
        // 放进session中
        if (list != null && list.size() > 0) {
            session.setAttribute("books_list", list);// !!!!
        }
        return "query_success";
    }

    // 删除书籍
    public String delete() {
        BooksDAO sdao = new BooksDAOImpl();
        String sid = request.getParameter("sid");
        sdao.deleteBook(sid);// 调用删除方法
        return "delete_success";
    }

    // 添加书籍
    public String add() throws Exception {
        Books s = new Books();
        // 获得学生姓名
        s.setSname(request.getParameter("sname"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 判断管理员输入的日期是否为空
        // 不为空则继续后面的工作,为空则跳转到添加书籍失败界面
        if (request.getParameter("loandate") != "") {
            s.setLoandate(sdf.parse(request.getParameter("loandate")));
            s.setTotal(request.getParameter("total"));
            BooksDAO sdao = new BooksDAOImpl();
            sdao.addBooks(s);
            return "add_success";
        } else {
            return "add_failure";
        }

    }

    // 修改书籍
    public String modify() {
        // 获取学生编号
        String sid = request.getParameter("sid");
        BooksDAO sdao = new BooksDAOImpl();
        Books s = sdao.queryBooksBySid(sid);
        // 保存在会话中
        session.setAttribute("modify_books", s);// !!!!!
        return "modify_success";
    }

    // 保存修改后的书籍资料动作
    public String save() throws Exception {
        Books s = new Books();
        s.setSid(request.getParameter("sid"));
        s.setSname(request.getParameter("sname"));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 判断管理员输入的日期是否为空
        // 不为空则继续后面的工作,为空则跳转到添加书籍失败界面
        if (request.getParameter("loandate") != "") {
            s.setLoandate(sdf.parse(request.getParameter("loandate")));
            s.setTotal(request.getParameter("total"));// !!!
            BooksDAO sdao = new BooksDAOImpl();
            sdao.updateBook(s);
            return "save_success";
        } else {
            return "modify";
        }

    }
}
BooksDAO.java
```java
package service;

import java.util.List;

import entity.Books;

/**
 * @ClassName: BooksDAO.java
 * @Description: 书籍业务逻辑接口
 * @version: "1.8.0_131"
 * @author: 寇爽
 * @date: 2017年11月14日 下午8:19:19
 */
public interface BooksDAO {
    //查询所有书籍资料
    public List<Books> queryAllBooks();

    // 根据书籍编号查询书籍资料
    public Books queryBooksBySid(String sid);

    // 添加书籍资料
    public boolean addBooks(Books s);

    // 修改书籍资料
    public boolean updateBook(Books s);

    //删除书籍资料
    public boolean deleteBook(String sid);

}




<div class="se-preview-section-delimiter"></div>

BooksDAOImpl.java

package service.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import db.MyHibernateSessionFactory;
import entity.Books;
import service.BooksDAO;

/**
 * @ClassName: BooksDAOImpl.java
 * @Description: 书籍业务逻辑接口实现类
 * @version: "1.8.0_131"
 * @author: 寇爽
 * @date: 2017年11月14日 下午8:34:05
 */
public class BooksDAOImpl implements BooksDAO {
    /**
     * 查询所有书籍资料
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Books> queryAllBooks() {
        Transaction tx = null;
        List<Books> list = null;
        String hql = "";
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            hql = "from Books";
            Query query = session.createQuery(hql);
            list = query.list();
            tx.commit();
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.commit();
            return list;
        } finally {
            if (tx != null) {
                tx = null;
            }
        }
    }

    /**
     * 根据书籍编号查询书籍资料
     */
    @Override
    public Books queryBooksBySid(String sid) {
        Transaction tx = null;
        Books s = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            s = (Books) session.get(Books.class, sid);
            tx.commit();
            return s;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.commit();
            return s;
        } finally {
            if (tx != null) {
                tx = null;
            }
        }
    }

    /**
     * 添加书籍资料
     */
    @Override
    public boolean addBooks(Books s) {
        // 设置学生学号为getNewSid()生成的学号
        s.setSid(getNewSid());
        Transaction tx = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            session.save(s);
            tx.commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.commit();
            return false;
        } finally {
            if (tx != null) {
                tx = null;
            }
        }
    }

    /**
     * 修改书籍资料
     */
    @Override
    public boolean updateBook(Books s) {
        // TODO Auto-generated method stub
        Transaction tx = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            session.update(s);
            tx.commit();

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

    /**
     * 删除书籍资料
     */
    @Override
    public boolean deleteBook(String sid) {
        // TODO Auto-generated method stub
        Transaction tx = null;
        String hql = "";
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            Books s = (Books) session.get(Books.class, sid);
            session.delete(s);
            tx.commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.commit();
            return false;
        } finally {
            if (tx != null) {
                tx = null;
            }
        }
    }

    /**
     * 生成书籍编号的方法
     * 
     * @return 书籍号
     */
    private String getNewSid() {
        Transaction tx = null;
        String hql = "";
        String sid = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            tx = session.beginTransaction();
            // 获得当前学生的最大编号
            hql = "select max(sid) from Books";
            Query query = session.createQuery(hql);
            sid = (String) query.uniqueResult();
            if (sid == null || "".equals(sid)) {
                // 给一个默认的最大编号
                sid = "B0000001";
            } else {
                // 取后7位
                String temp = sid.substring(1);
                // 转成数字
                int i = Integer.parseInt(temp);
                i++;
                // 还原成字符串
                temp = String.valueOf(i);
                int len = temp.length();
                // 凑够7位
                for (int j = 0; j < 7 - len; j++) {
                    temp = "0" + temp;
                }
                sid = "B" + temp;
            }
            tx.commit();
            return sid;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.commit();
            return null;
        } finally {
            if (tx != null) {
                tx = null;
            }
        }
    }

}




<div class="se-preview-section-delimiter"></div>

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>

    <package name="default" namespace="/" extends="struts-default">

    </package>
    <package name="users" namespace="/users" extends="default">
        <action name="*_*" class="action.{1}Action" method="{2}">
            <result name="login_success">/users/Users_login_success.jsp</result>
            <result name="login_failure">/users/Users_login.jsp</result>
            <result name="logout_success">/users/Users_login.jsp</result>
            <result name="input">/users/Users_login.jsp</result>
        </action>
    </package>
    <package name="students" namespace="/students" extends="default">
          <action name="*_*" class="action.{1}Action" method="{2}">
            <result name="query_success">/students/Students_query_success.jsp</result>
            <result name="modify_success">/students/Students_modify.jsp</result>
            <result name="modify">/students/Students_modify.jsp</result>
            <result name="add_success">/students/Students_add_success.jsp</result>
            <result name="add_failure">/students/Students_add_failure.jsp</result>  
            <result name="save_success">/students/Students_modify_success.jsp</result> 
            <result name="delete_success" type="chain">Students_query</result>      
          </action>
    </package>
    <package name="books" namespace="/books" extends="default">
          <action name="*_*" class="action.{1}Action" method="{2}">
            <result name="query_success">/books/Books_query_success.jsp</result>
            <result name="modify_success">/books/Books_modify.jsp</result>
            <result name="modify">/books/Books_modify.jsp</result>
            <result name="add_success">/books/Books_add_success.jsp</result>
            <result name="add_failure">/books/Books_add_failure.jsp</result>  
            <result name="save_success">/books/Books_modify_success.jsp</result> 
            <result name="delete_success" type="chain">Books_query</result> 

          </action>
    </package>
</struts>

项目源码:
https://github.com/Snailclimb/JavaWebProject/tree/master/immoc_sh (推荐star不要fork,还在继续改动中),后续还会上传SSH,SSM的项目。

转载于:https://www.cnblogs.com/snailclimb/p/9086464.html

一.功能简介 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、付费专栏及课程。

余额充值