效果图
部分代码
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(); Listlist = 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 ListqueryAllBooks() { 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的项目。