zoukankan      html  css  js  c++  java
  • 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的项目。

  • 相关阅读:
    Erlang in Delphi 项目发布!
    云计算将如何改变世界
    win2000server IIS和tomcat5多站点配置
    实践中整理出tomcat集群和负载均衡
    VCL已死,RAD已死(6) 结语与预测
    window下tomcat集群和负载均衡
    团队开发经验:如何带领一个项目团队并做好项目总结 !!
    Install latest R for ubuntu
    P1297 单选错位
    P5322 排兵布阵
  • 原文地址:https://www.cnblogs.com/snailclimb/p/9086464.html
Copyright © 2011-2022 走看看