图书管理系统 Struts+hibernate

下载链接

目录结构如下

com.action>>
                    BookAction.java
                    LoginAction.java
com.bean>>
                    Admin.java
                    Book.java
                    Admin.hbm.xml
                    Book.hbm.xml
com.dao>>
                    AdminDao.java
                    BookDao.java
com.util>>    
                   HibernateSessionFactory.java
hibernate.cfg.xml
struts.xml
WebRoot>>
                  WEB-INF>>
                                    web.xml
                   book_add.jsp
                   borrow.jsp
                   index.jsp
                   login.jsp
                   main.jsp

BookAction.java>>

package com.action;

import java.util.List;

import com.bean.Book;
import com.dao.BookDao;
import com.opensymphony.xwork2.ActionContext;

public class BookAction {
    private Book book;
    private int id;
    private String name;

    //查询图书
    public void query(){
        BookDao bookDao=new BookDao();
        Book book1=new Book();
        if (!name.isEmpty()) {
            book1=bookDao.findBookByName(name);
        }
        ActionContext.getContext().getSession().put("book", book1);
    }
    
    public String addBook(){
        System.out.println("进入了addBook方法");
        BookDao bookDao=new BookDao();
        if(book.getId() == null){
            bookDao.addBook(book);
            System.out.println("新增书名:"+book.getName());
        }else{
            bookDao.updateBook(book);
            System.out.println("修改书id:"+book.getId());
        }
        
        return bookList();
    }
    public String bookList(){
        BookDao bookDao=new BookDao();
        List<Book> books=bookDao.findBookAll();
        ActionContext.getContext().getSession().put("b", books);
        System.out.println("进入了booklist方法");
        return "main";
    } 
    public String bookEdit(){
        BookDao bookDao=new BookDao();
        Book book=bookDao.findBookById(id);
        ActionContext.getContext().getSession().put("bo", book);
        System.out.println("进入了bookEdit方法,id:"+id);
        return "book_edit";
    }
    //get请求 返回user_add页面
    public String bookAdd(){
        if (ActionContext.getContext().getSession().containsKey("bo")) {
            ActionContext.getContext().getSession().remove("bo");
        }
        
        return "book_add";
    }
    public String delete(){
        BookDao bookDao=new BookDao();
        bookDao.deleteBookById(id);
        System.out.println("进入了delet方法,id:"+id);
        return bookList();
    }
    
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }





    public String getName() {
        return name;
    }





    public void setName(String name) {
        this.name = name;
    }
    
}

LoginAction.java>>

package com.action;

import com.bean.Admin;
import com.dao.AdminDao;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction {
    private Admin admin;

    public String login(){
        
        if (!admin.getName().isEmpty()&&!admin.getPassword().isEmpty()) {
            AdminDao adminDao=new AdminDao();
            Admin user=adminDao.findAdminByName(admin.getName());
            if (user.getPassword().equals(admin.getPassword())) {
                ActionContext.getContext().getSession().put("flag", "0");
                ActionContext.getContext().getSession().put("user", user);
                System.out.println("成功登录!用户名:"+user.getName());
                return "main";
            }
        }
        ActionContext.getContext().getSession().put("flag", "1");
        System.out.println("成功失败!用户名:"+admin.getName());
        return "login";
    }
    
    public String loginOut(){
        boolean flag=ActionContext.getContext().getSession().containsKey("user");
        if (flag) {
            ActionContext.getContext().getSession().remove("user");
            return "loginOut";
        }
        System.out.println(flag);
        return "loginOut";
    }
    public String main(){
        boolean flag=ActionContext.getContext().getSession().containsKey("user");
        if (flag) {
            return "main";
        }else {
            return "login";
        }
        
    }
    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }
}

Admin.java>>

 
  

package com.bean;

 
  

import java.sql.Timestamp;

 
  

/**
* Admin entity. @author MyEclipse Persistence Tools
*/

 
  

public class Admin implements java.io.Serializable {

 
  

// Fields

 
  

private Integer id;
private String name;
private String password;
private Short sex;
private Short type;
private Timestamp createDate;

 
  

// Constructors

 
  

/** default constructor */
public Admin() {
}

 
  

/** minimal constructor */
public Admin(String name) {
this.name = name;
}

 
  

/** full constructor */
public Admin(String name, String password, Short sex, Short type, Timestamp createDate) {
this.name = name;
this.password = password;
this.sex = sex;
this.type = type;
this.createDate = createDate;
}

 
  

// Property accessors

 
  

public Integer getId() {
return this.id;
}

 
  

public void setId(Integer id) {
this.id = id;
}

 
  

public String getName() {
return this.name;
}

 
  

public void setName(String name) {
this.name = name;
}

 
  

public String getPassword() {
return this.password;
}

 
  

public void setPassword(String password) {
this.password = password;
}

 
  

public Short getSex() {
return this.sex;
}

 
  

public void setSex(Short sex) {
this.sex = sex;
}

 
  

public Short getType() {
return this.type;
}

 
  

public void setType(Short type) {
this.type = type;
}

 
  

public Timestamp getCreateDate() {
return this.createDate;
}

 
  

public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}

 
  

}

Book.java>>

package com.bean;

import java.sql.Timestamp;

/**
 * Book entity. @author MyEclipse Persistence Tools
 */

public class Book implements java.io.Serializable {

    // Fields

    private Integer id;
    private String name;
    private String author;
    private String publisher;
    private Short type;
    private Short state;
    private Integer time;
    private String category;
    private Timestamp createDate;

    // Constructors

    /** default constructor */
    public Book() {
    }

    /** minimal constructor */
    public Book(String name) {
        this.name = name;
    }

    /** full constructor */
    public Book(String name, String author, String publisher, Short type, Short state, Integer time, String category,
            Timestamp createDate) {
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.type = type;
        this.state = state;
        this.time = time;
        this.category = category;
        this.createDate = createDate;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return this.publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Short getType() {
        return this.type;
    }

    public void setType(Short type) {
        this.type = type;
    }

    public Short getState() {
        return this.state;
    }

    public void setState(Short state) {
        this.state = state;
    }

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

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

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Timestamp getCreateDate() {
        return this.createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

}

Admin.hbm.xml 自动生成

Book.hbm.xml 自动生成

AdminDao.java>>

package com.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bean.Admin;
import com.util.HibernateSessionFactory;

public class AdminDao {
    //查询一条
        public Admin findAdminByName(String name){
            Session session=HibernateSessionFactory.getSession();
            String sql="from Admin ad where ad.name=\'"+name+"\'";
            Query query = session.createQuery(sql);
//            query.setString(1, name);
            Admin admin = (Admin) query.list().get(0);
            HibernateSessionFactory.closeSession();
            return admin;
        }
}

BookDao.java>>

package com.dao;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bean.Admin;
import com.bean.Book;
import com.util.HibernateSessionFactory;

public class BookDao {
    //增加一条
    public boolean addBook(Book book){
        Session session=HibernateSessionFactory.getSession();
        Transaction transaction=session.beginTransaction();
        session.save(book);
        System.out.println("保存成功!!");
        transaction.commit();
        HibernateSessionFactory.closeSession();
        return true;
    } 
    //获取所有
    public List<Book> findBookAll(){
        Session session=HibernateSessionFactory.getSession();
        Transaction transaction=session.beginTransaction();
        String hql="from Book";
        Query query=session.createQuery(hql);
        List<Book> list =query.list();
        transaction.commit();
        HibernateSessionFactory.closeSession();
        return list;
    }
    //查询一条
    public Book findBookById(int id){
        Session session=HibernateSessionFactory.getSession();
        Book book=session.get(Book.class,id);
        HibernateSessionFactory.closeSession();
        return book;
    }
    //查询一条
    public Book findBookByName(String name){
        Session session=HibernateSessionFactory.getSession();
        String sql="from Book where name like \'"+name+"%\'";
        Query query = session.createQuery(sql);
//                query.setString(1, name);
        Book book=new Book();
        if (query.list().size()>0) {
            book = (Book) query.list().get(0);
        }
        HibernateSessionFactory.closeSession();
        return book;
    }
    //删除一条
    public boolean deleteBookById(int id){
        Session session=HibernateSessionFactory.getSession();
        Transaction trans=session.beginTransaction();
        Book book=session.get(Book.class,id);
        session.delete(book);
        trans.commit();
        HibernateSessionFactory.closeSession();
        return true;
    }
    //更新
    public boolean updateBook(Book book){
        Session session=HibernateSessionFactory.getSession();
        Transaction trans=session.beginTransaction();
        session.update(book);
        trans.commit();
        HibernateSessionFactory.closeSession();
        return true;
    }
}

HibernateSessionFactory.java 自动生成

hibernate.cfg.xml 自动生成

hibernate.cfg.xml>>

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

    <package name="default" extends="struts-default">
        <!--book相关  -->
        <action name="add_book" class="com.action.BookAction" method="addBook">
            <result name="main" type="redirect">/main.jsp</result>
        </action>
        
    
        <action name="delete_book" class="com.action.BookAction" method="delete">
            <result name="book" type="redirect">/book.jsp</result>
        </action>
        
        <action name="edit_book" class="com.action.BookAction" method="bookEdit">
            <result name="book_edit" type="redirect">/book_add.jsp</result>
        </action>
        <action name="book_add" class="com.action.BookAction" method="bookAdd">
            <result name="book_add" type="redirect">/book_add.jsp</result>
        </action>
    
        
        
        <!--登录相关  -->
        <action name="login" class="com.action.LoginAction" method="login">
            <result name="login" type="redirect">/login.jsp</result>
            <!-- 错误可忽略 -->
            <result name="main" type="redirect">/main.action</result>
        </action>
        <action name="loginOut" class="com.action.LoginAction" method="loginOut">
            <result name="loginOut" type="redirect">/login.jsp</result>
            
        </action>
        <action name="main" class="com.action.LoginAction" method="main">
            <result name="main">/main.jsp</result>
            <result name="login">/login.jsp</result>
        </action>
    </package>
</struts>    

book_add.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>图书修改新增页面</title>
    <link rel="stylesheet" type="text/css" href="<%=basePath%>static/layui/css/layui.css">
      <script src="<%=basePath%>static/layui/layui.js" charset="utf-8"></script>
  </head>
  <body>
    <h1>书详情页</h1>
    <div class="layui-container">  
      <div class="layui-row">
    <form class="layui-form" action="add_book.action" method="post">
        <input name="book.id" type="hidden" value='<s:property value="#session.bo.id"/>'/>
    <div class="layui-form" lay-filter="layuiadmin-form-useradmin" id="layuiadmin-form-useradmin" style="padding: 20px 0 0 0;">
    <div class="layui-form-item">
      <label class="layui-form-label">书名*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.name" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input" value='<s:property value="#session.bo.name"/>'>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">作者*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.author" lay-verify="required" placeholder="请输入作者名" autocomplete="off" class="layui-input" value='<s:property value="#session.bo.author"/>'>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">出版社*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.publisher" lay-verify="required" placeholder="请输入出版社" autocomplete="off" class="layui-input" value='<s:property value="#session.bo.publisher"/>'>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">所属类别*</label>
      <div class="layui-input-inline">
        <input type="text" name="book.category" lay-verify="required" placeholder="请输入类别" autocomplete="off" class="layui-input" value='<s:property value="#session.bo.category"/>'>
      </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">是否可借</label>
    <div class="layui-input-block">
      <input type="radio" name="book.state" value="1" title="可借" <s:if test="#session.bo==null||#session.bo.state==1">checked</s:if>>
      <input type="radio" name="book.state" value="0" title="不可借" <s:if test="#session.bo.state!=1">checked</s:if>>
    </div>
  </div>
   <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </form>
  </div>
  </div>
<script>
    layui.use('form',function(){
       var form = layui.form;
           //刷新界面 所有元素

           form.render()
    });
</script>
  </body>
</html>

borrow.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>借书</title>
  </head>
<body class="layui-layout-body">
<h1>借书</h1>
<div class="layui-container">  
    <form action="borrowQuery.action" method="post">
             <div class="layui-form-item">
              <label class="layui-form-label">请输入书名</label>
              <div class="layui-input-block layui-col-md5">
                <input type="text" name="name" required="true" lay-verify="required" placeholder="请输入书名" autocomplete="off" class="layui-input">
              </div>
            </div>
        <button type="submit" class="layui-btn layui-col-md3 layui-col-md-offset3">查询</button>
    </form>
<s:if test="#session.book!=null&&#session.book.id">
    <table class="layui-table" style="margin-top:30px">
      <thead>
        <tr>
          <th>ID</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>类别</th>
          <th>是否可借</th>
        </tr> 
      </thead>
      <tbody>
        <tr>
          <td><s:property value="#session.book.id" /></td>
          <td><s:property value="#session.book.name" /></td>
          <td><s:property value="#session.book.author" /></td>
          <td><s:property value="#session.book.publisher" /></td>
          <td><s:property value="#session.book.category" /></td>
          <td>
              <s:if test="#session.book.state==1">可借</s:if>
              <s:if test="#session.book.state!=1">不可借</s:if>
          </td>
        </tr>
      </tbody>
    </table>
    <a onClick="layer.msg('当前书已被借走!')" href="<s:if test="#session.book.state!=1">javascript:volid(0);</s:if><s:if test="#session.book.state==1">borrowBook.action?id=<s:property value="#session.book.id"/></s:if>" 
    class="layui-btn layui-btn-normal layui-col-md3 layui-col-md-offset3 <s:if test="#session.book.state!=1">layui-btn-disabled</s:if>">确认借书</a>
    </s:if>    
</div>
<script>
</script>
</body>
</html>

login.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
  </head>
  
  <body>
     
  <div class="layadmin-user-login-main" style="margin-top:100px">
      <div class="layadmin-user-login-box layadmin-user-login-header">
        <h2>图书馆管理系统</h2>
        <p>图书馆后台管理系统</p>
      </div>
      <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <form action="login.action" method="post">
    
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
          <input type="text" name="admin.name" value="admin" id="LAY-user-login-username" lay-verify="required" placeholder="用户名" class="layui-input">
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
          <input type="password" name="admin.password" value="123" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
        </div>
   
        
        <div class="layui-form-item">
          <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="LAY-user-login-submit">登 入</button>
        </div>
      
      </form>
      </div>
    </div>
  </body>
</html>

main.jsp>>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>书列表</title>
   
  </head>
  
  <body>
     <h1>书列表</h1>
     
     <div style="margin: 10px;">
          <a href="book_add.action" target="main" class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加图书</a>
        </div>
    <table class="layui-table">
     
      <thead>
    
        <tr>
          <th>ID</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>类别</th>
          <th>是否可借</th>
          <th>操作</th>
        </tr> 
      </thead>
      <tbody>
        <s:iterator value="#session.b" var="i">
        <tr>
          <td><s:property value="#i.id" /></td>
          <td><s:property value="#i.name" /></td>
          <td><s:property value="#i.author" /></td>
          <td><s:property value="#i.publisher" /></td>
          <td><s:property value="#i.category" /></td>
          <td>
              <s:if test="#i.state==1">可借</s:if>
              <s:if test="#i.state!=1">不可借</s:if>
          </td>
          <td>
              <a href="edit_book.action?id=<s:property value='#i.id'/>" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a href="delete_book.action?id=<s:property value='#i.id'/>" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
          </td>
          
        </tr>
      </s:iterator>
      </tbody>
    </table>
  </body>
</html>

web.xml

  <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>*.action</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

完!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

转载于:https://www.cnblogs.com/yangchas/p/11167445.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、付费专栏及课程。

余额充值