• 项目第六天


    11-图书归还

    1-实现查询归还信息功能
    1. RecordDao.java添加此方法

      public int modify(double deposit, long userId, long id) throws SQLException {
          Connection conn = DBHelper.getConnection();
          String sql = "update record set backDate = CURRENT_DATE, deposit = ?, userId = ? where id = ?";
          int count = runner.update(conn, sql, deposit, userId, id);
          DBHelper.close(conn);
          return count;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    2. 修改RecordBiz.java文件的getRecordsByMemberId方法

      public List<Record> getRecordsByMemberId(long memberId) {
          List<Record> records = null;
          try {
              records = recordDao.getRecordsByMemberId(memberId);
      
              Member member = memberBiz.getById(memberId);
              for (Record record: records) {
                  long bookId = record.getBookId();
                  Book book = bookDao.getById(bookId);
                  record.setBook(book);
                  record.setMember(member);
                  long day = member.getType().getKeepDay();
                  java.sql.Date rentDate = record.getRentDate();
                  java.sql.Date backDate = DateHelper.getNewDate(rentDate, day);
                  record.setBackDate(backDate);
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return records;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
    3. RecordServlet的switch添加如下代码

      case "queryback":
          String idn = req.getParameter("idn");
          Member member = memberBiz.getByIdNumber(idn);
          List<Record> records = recordBiz.getRecordsByMemberId(member.getId());
          req.setAttribute("member",member);
          req.setAttribute("records", records);
          req.getRequestDispatcher("return_list.jsp").forward(req, resp);
          break;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    4. return_list.html文件转换成return_list.jsp文件

    5. 修改return_list.jsp文件下面显示的代码

      <%@ page import="com.wang.book.bean.Record" %>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
              <script src="Js/jquery-3.3.1.min.js">script>
              <script>
                  $(function() {
                      $("#btnQuery").click(function() {
                          var idn = $("#idNumber").val();
                          location.href = "record.let?type=queryback&idn=" + idn;
                      })
                      $("#ckAll").click(function() {
                      	$(".ck").prop("checked", $(this).prop("checked"));
                  	})
                      $("#btnReturn").click(function() {
                          if (!$("#memberId").val()) {
                              alert("请输入会员信息");
                              return;
                          }
                          var idList = new Array();
                          $(".ck").each(function() {
                              if ($(this).prop("checked")) {
                                  idList.push($(this).val());
                              }
                          })
                          if (idList.length == 0) {
                              alert("请选择需要归还的书籍信息");
                              return;
                          }
                          var idStr = idList.join("_");
                          var path = "record.let?type=back&mid=" + $("#memberId").val() + "&ids=" + idStr;
                          location.href = path;
                      })
                  })
              script>
      
              <fieldset>
                  <legend>查询会员legend>
                  <c:if test="${member==null}">
                      <table width="100%"  class="cont"  >
                          <tr>
                              <td width="8%" class="run-right"> 会员编号td>
                              <td colspan="7"><input class="text" type="text" id="idNumber"/>
                                  <input type="button" id="btnQuery" value="确定" style="width: 80px;"/>
                                  <input type="button" id="btnReturn" value="批量归还" style="width: 80px;"/>
                              td>
      
                          tr>
                          <tr>
                              <td width="8%" class="run-right">会员名称td>
                              <td width="17%"><input class="text" type="text"  disabled/>
                                  <input type="hidden" id="memberId" value="">
                              td>
                              <td width="8%" class="run-right">会员类型:td>
                              <td width="17%"><input class="text" type="text"  disabled />td>
                              <td width="8%" class="run-right">可借数量td>
                              <td width="17%"><input class="text" type="text"  disabled />td>
                              <td width="8%" class="run-right">账户余额td>
                              <td width="17%"><input class="text" type="text"  disabled />td>
                          tr>
                      table>
                  c:if>
                  <c:if test="${member!=null}">
                      <table width="100%"  class="cont"  >
                          <tr>
                              <td width="8%" class="run-right"> 会员编号td>
                              <td colspan="7"><input class="text" type="text" id="idNumber" value="${member.idNumber}"/>
                                  <input type="button" id="btnQuery" value="确定" disabled style="width: 80px;"/>
                                  <input type="button" id="btnReturn" value="批量归还" style="width: 80px;"/>
                              td>
                          tr>
                          <tr>
                              <td width="8%" class="run-right">会员名称td>
                              <td width="17%"><input class="text" type="text" name="memberId" value="${member.name}"  disabled/>
                                  <input type="hidden" id="memberId" value="${member.id}">
                              td>
                              <td width="8%" class="run-right">会员类型:td>
                              <td width="17%"><input class="text" type="text" name="memberId" value="${member.type.name}"  disabled />td>
                              <td width="8%" class="run-right">可借数量td>
                              <% List<Record> records = (List<Record>) request.getAttribute("records"); %>
                              <td width="17%"><input class="text" type="text" name="memberId" value="<%=records.size()%>" disabled />td>
                              <td width="8%" class="run-right">账户余额td>
                              <td width="17%"><input class="text" type="text" name="memberId" value="${member.balance}" disabled />td>
                              tr>
                      table>
                  c:if>
              fieldset>
          
              <form action="" method="">
                  <table width="100%"  class="cont tr_color">
                      <tr>
                          <th><input id="ckAll" type="checkbox" value="" checkbox id="ckAll" />全选/全不选th>
                          <th>书籍名th>
                          <th>借阅时间th>
                          <th>应还时间th>
                          <th>出版社th>
                          <th>书架th>
                          <th>押金(元)th>
                          <th>操作th>
                      tr>
                      <c:if test="${records == null}" >
                          <tr align="center" class="d">
                              <td colspan="8" align="center"><h2>暂无数据展示h2>td>
                          tr>
                      c:if>
                  	<c:if test="${records != null}" >
                      	<c:forEach items="${records}" var="r">
                                 <% 
                                    Record record = (Record)pageContext.getAttribute("r");
                                    java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
                                    if (record.getBackDate().before(date)) {
                                 %>
                                 <tr align="center" class="d" style="background-color: #f08080">
                                     <td><input type="checkbox" value="${r.id}"  class="ck" checked />td>
                                     <td>${r.book.name}td>
                                     <td>${r.rentDate}td>
                                     <td>${r.backDate}td>
                                     <td>${r.book.publish}td>
                                     <td>${r.book.address}td>
                                     <td>${r.deposit}td> 
                                     <td><a href="record.let?type=keep&id=${r.id}">续借a>td>
                                 tr>
                                 <%
                                    } else {    
                                 %>
                                 <tr align="center" class="d">
                                     <td><input type="checkbox" value="${r.id}"  class="ck" checked />td>
                                     <td>${r.book.name}td>
                                     <td>${r.rentDate}td>
                                     <td>${r.backDate}td>
                                     <td>${r.book.publish}td>
                                     <td>${r.book.address}td>
                                     <td>${r.deposit}td>
                                     <td><a href="record.let?type=keep&id=${r.id}">续借a>td>
                                 tr>
                             	   <% } %>
                      	c:forEach>
                  	c:if>
                  table>
              form>                             
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
    2-实现批量归还功能
    1. RecordServlet的switch添加如下代码

      case "back":
          long memberId2 = Long.parseLong(req.getParameter("mid"));
          String idStr = req.getParameter("ids");
          String []idStrs = idStr.split("_");
          List<Long> recordIds = new ArrayList<Long>();
          for (String s: idStrs) {
              recordIds.add(Long.parseLong(s));
          }
          long userId2 = user.getId();
          int count2 = recordBiz.modify(memberId2, recordIds, userId2);
          if (count2 > 0) {
              out.println("");
          } else {
              out.println("");
          }
          break;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    2. RecordBiz.java类中添加如下方法。

      public int modify(long memberId, List<Long> recordIds, long userId) {
          try {
              DBHelper.beginTransaction();
              double total = 0;
              Member member = memberBiz.getById(memberId);
              for (long recordId: recordIds) {
                  Record record = recordDao.getById(recordId);
                  java.sql.Date backDate = DateHelper.getNewDate(record.getRentDate(), member.getType().getKeepDay());
                  java.util.Date currentDate = new java.util.Date();
                  int day = 0;
                  if (currentDate.after(backDate)) {
                      day = DateHelper.getSpan(currentDate, backDate);
                  }
                  total += record.getDeposit() - day;
                  recordDao.modify(day, userId, recordId);
                  bookDao.modify(record.getBookId(), 1);
              }
              memberDao.modifyBalance(memberId, total);
              DBHelper.commitTransaction();
          } catch (SQLException e) {
              e.printStackTrace();
              try {
                  DBHelper.rollbackTransaction();
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
              return 0;
          }
          return 1;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
    3. RecordDao.java类中添加如下方法。

      public Record getById(long recordId) throws SQLException {
          Connection conn = DBHelper.getConnection();
          String sql = "select * from record where id = ?";
          Record record = runner.query(conn, sql, new BeanHandler<Record>(Record.class), recordId);
          DBHelper.close(conn);
          return record;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    4. util包下的DateHelper.java添加如下方法。

      public static int getSpan(Date date01, Date date02) {
          long span = date01.getTime() - date02.getTime();
          int day = (int) (span/1000/60/60/24);
          return Math.abs(day);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    3-实现续借功能
    1. RecordServlet的switch添加如下代码

       case "keep":
          long recordId = Long.parseLong(req.getParameter("id"));
          int count3 = recordBiz.modify(recordId);
          if (count3 > 0) {
              out.println("");
          } else {
              out.println("");
          }
          break;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. RecordBiz.java类中添加如下方法。

      public int modify(long id) {
          int count  = 0;
          try {
              count = recordDao.modify(id);
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return count;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3. RecordDao.java类中添加如下方法。

      public int modify(long id) throws SQLException {
          Connection conn = DBHelper.getConnection();
          String sql = "update record set backDate = CURRENT_DATE where id = ?";
          int count = runner.update(conn, sql, id);
          DBHelper.close(conn);
          return count;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    12-历史查询

    1-历史查询Dao层
    1. RecordDao.java类中添加如下方法。

      public List<Map<String, Object>> query(int typeId, String keyWord) throws SQLException {
          Connection conn = DBHelper.getConnection();
          StringBuilder sb = new StringBuilder("select * from recordView where 1 = 1 ");
          switch (typeId) {
              case 0:
                  break;
              case 1:
                  sb.append("and backDate is not null");
                  break;
              case 2:
                  sb.append("and backDate is null");
                  break;
              case 3:
                  sb.append("and backDate is null and returnDate < date_add(CURRENT_DATE, interval 7 DAY) ");
                  break;
          }
      
          if (keyWord != null) {
               sb.append(" and bookName like '%" + keyWord + "%' or memberName like '%" + keyWord + "%' or concat(rentDate, '') like '%" + keyWord + "%'");
          }
      
          List<Map<String, Object>> data = runner.query(conn, sb.toString(), new MapListHandler());
          DBHelper.close(conn);
          return data;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
    2-历史查询Server层
    1. RecordBiz.java类中添加如下方法。

      public List<Map<String, Object>> query(int typeId, String keyword) {
          List<Map<String, Object>> rows = null;
          try {
              rows = recordDao.query(typeId, keyword);
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return rows;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3-历史查询Servlet层
    1. RecordServlet的switch添加如下代码

      case "doajax":
          int typeId = Integer.parseInt(req.getParameter("typeId"));
          String keyword = req.getParameter("keyword");
          keyword = keyword.isEmpty() ? null : keyword;
          List<Map<String, Object>> rows = recordBiz.query(typeId, keyword);
          out.print(JSON.toJSONString(rows));
          break;
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    4-历史查询界面实现
    1. rent_list.html文件转换成rent_list.jsp文件

    2. 修改rent_list.jsp文件下面显示的代码

      <script src="Js/jquery-3.3.1.min.js" >script>
      <script>
          $(function() {
              $("#btnQuery").click(function() {
                  $("#tbRecord").find("tbody").html("");
                  var typeId = $(":radio:checked").prop("value");
                  var keyword = $("#keyword").val();
                  var url = "record.let?type=doajax&typeId=" + typeId + "&keyword=" + keyword;
                  $.get(url, function(data) {
                      console.log(data);
                      if (data === "[]") {
                          alert("没有信息展示");
                          return;
                      }
                      var records = JSON.parse(data);
                      for (var i = 0; i < records.length; i++) {
                          var record = records[i];
                          var tr = $("");
                          var tdMName = $("" + record.memberName + "");
                          var tdBName = $("" + record.bookName + "");
                          var tdRentDate = $("" + record.rentDate + "");
                          var tdBackDate = $("" + (record.backDate === undefined ? "" : record.backDate) + "");
                          var tdDeposit = $("" + record.deposit + "");
                          tr.append(tdMName);
                          tr.append(tdBName);
                          tr.append(tdRentDate);
                          tr.append(tdBackDate);
                          tr.append(tdDeposit);
                          $("#tbRecord").find("tbody").append(tr);
                      }
                  })
              })
          })
      script>
      <form action="" method="">
          <table width="100%"  class="cont tr_color" id="tbRecord">
              <thead>
                  <tr>
                      <th>会员名称th>
                      <th>书籍名称th>
                      <th>借阅时间th>
                      <th>归还时间th>
                      <th>押金(元)th>
                  tr>
              thead>
              <tbody>tbody>
          table>
      form>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
  • 相关阅读:
    OSINT技术情报精选·2024年6月第2周
    TCP的三次握手与四次挥手
    【Java基础】Java8新特性
    MySQL:基础操作(1)
    使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
    [deeplearning]pytorch实现softmax多分类问题预测训练
    目标检测YOLO实战应用案例100讲-基于无人机航拍图像的目标检测
    “数字赋能、智创未来”第三届中国(宁波)软件峰会暨程序员节即将开启
    c++对接CAT1400
    【Java 进阶篇】使用 SQL 进行排序查询
  • 原文地址:https://blog.csdn.net/wcy_0522/article/details/126796664