博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2+Hibernate实现图书管理系统
阅读量:5232 次
发布时间:2019-06-14

本文共 10758 字,大约阅读时间需要 35 分钟。

效果图

登录页面

学生列表
书籍列表

部分代码

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

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
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```javapackage 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
queryAllBooks(); // 根据书籍编号查询书籍资料 public Books queryBooksBySid(String sid); // 添加书籍资料 public boolean addBooks(Books s); // 修改书籍资料 public boolean updateBook(Books s); //删除书籍资料 public boolean deleteBook(String sid);}

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
queryAllBooks() { Transaction tx = null; List
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; } } }}

struts.xml

/users/Users_login_success.jsp
/users/Users_login.jsp
/users/Users_login.jsp
/users/Users_login.jsp
/students/Students_query_success.jsp
/students/Students_modify.jsp
/students/Students_modify.jsp
/students/Students_add_success.jsp
/students/Students_add_failure.jsp
/students/Students_modify_success.jsp
Students_query
/books/Books_query_success.jsp
/books/Books_modify.jsp
/books/Books_modify.jsp
/books/Books_add_success.jsp
/books/Books_add_failure.jsp
/books/Books_modify_success.jsp
Books_query

项目源码:

(推荐star不要fork,还在继续改动中),后续还会上传SSH,SSM的项目。

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

你可能感兴趣的文章
Python 第四十五章 MySQL 内容回顾
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
监控工具之---Prometheus 安装详解(三)
查看>>
不错的MVC文章
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
路冉的JavaScript学习笔记-2015年1月23日
查看>>
Mysql出现(10061)错误提示的暴力解决办法
查看>>
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Lintcode: Partition Array
查看>>
Maximum Product Subarray
查看>>
C语言小项目-火车票订票系统
查看>>