首页 > 代码库 > 基于spring+springmvc+mybatis的图书管理系统

基于spring+springmvc+mybatis的图书管理系统


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加图书</title>
 <script type="text/javascript" src=http://www.mamicode.com/"statics/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src=http://www.mamicode.com/"statics/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
    function check() { 
        var btype = $("#btype").val();    
        /*var bname = $("#bname").val();
        var bauthor = $("#bauthor").val();
        var btime = $("#btime").val();
            
        if (bname == "") {
            alert("图书名称不能为空");
            return false;
        }
        if (bauthor == "") {
            alert("作者姓名不能为空");
            return false;
        }
        if (btime == "") {
            alert("购买日期不能为空");
            return false;
        }    
        */
        /*
        var regBtime=/^(19|20)\d{2}-(1[0-2])|0?[1-9]-(3[0-1]|[1-2]\d|0?[1-9])$/;

        if (regBtime.test(btime) == false) {
            alert("购买日期无效");
            return false;
        }
        */
        if (btype == -1) {
            alert("请选择图书类别");
            return false;
        }
        return true;
    }
    

</script>
</head>
<body>
    <form:form action="book" commandName="book" method="post" onsubmit="return check();">
    <form:errors path="*" ></form:errors>
        <table width="500" border="1">
            <caption>新增图书信息</caption>
            <tr>
                <td width="91">图书名称:</td>
                <td width="216"><input type="text" name="bname" id="bname" /></td>
                <td width="171">&nbsp;</td>
            </tr>
            <tr>
                <td>图书作者:</td>
                <td><input type="text" name="bauthor" id="bauthor" /></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>购买日期:</td>
                <td> <input type="text" class="Wdate" id="btime" name="btime"
  onClick="WdatePicker({dateFmt:‘yyyy-MM-dd‘})" size="21"
  value="" /></td>
                <td>yyyy-MM-dd格式</td>
            </tr>
            <tr>
                <td>图书类别:</td>
                <td><select id="btype" name="btype">
                        <option value=http://www.mamicode.com/"-1">--选择所属分类--</option>
                        <option value=http://www.mamicode.com/"0">计算机/软件</option>
                        <option value=http://www.mamicode.com/"1">小说/文摘</option>
                        <option value=http://www.mamicode.com/"2">杂项</option>
                </select></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td colspan="3" align="center"><input type="submit"
                    value="新增图书" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>图书详细信息</title>
 9 
10 </head>
11 <body>
12     <h2>图书详细信息</h2>
13     <Br /> 图书名称:${requestScope.book.bname }
14     <Br /> 图书作者:${requestScope.book.bauthor }
15     <Br /> 出版时间:${requestScope.book.btime }
16     <Br /> 图书类别:
17     <c:choose>
18         <c:when test="${book.btype eq 0}">计算机/软件</c:when>
19         <c:when test="${book.btype eq 1}">小说/文摘</c:when>
20         <c:when test="${book.btype eq 2}">杂项</c:when>
21     </c:choose>
22 </body>
23 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 出错了!!!
11 </body>
12 </html>
  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5 <html>
  6 <head>
  7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8 <title>查询图书</title>
  9 <style type="text/css">
 10 td {
 11     text-align: center;
 12 }
 13 
 14 table, tr, td {
 15     border: 1px solid black;
 16     border-collapse: collapse;
 17 }
 18 
 19 a.add {
 20     text-decoration: none;
 21     color: red;
 22 }
 23 
 24 div {
 25 width:900px;
 26     text-align: center;
 27 }
 28 </style>
 29 <script type="text/javascript" src="http://www.mamicode.com/statics/js/jquery-1.8.3.js"></script>
 30 <script type="text/javascript">
 31     $(document).ready(function() {
 32         $("#all").click(function() {
 33             var $bids = $("input[name=bids]");
 34             if ($(this).is(":checked")) {
 35                 $bids.prop("checked", true);
 36             } else {
 37                 $bids.prop("checked", false);
 38             }
 39         });
 40 
 41         $("#delBtn").click(function() {
 42             var $bids = $("input[name=bids]");
 43             var flag=false;            
 44             $bids.each(function(index,i){
 45                 if($(i).is(":checked")){
 46                     flag=true;
 47                     return;
 48                 }
 49             });
 50             if(flag){
 51                 if(confirm("确认删除吗?")){
 52                     var bids="";
 53                     for(var i=0;i<$bids.length;i++){
 54                         if($bids[i].checked){
 55                             if(i==$bids.length-1){
 56                                 bids+="bids="+$bids[i].value;
 57                             }else{
 58                                 bids+="bids="+$bids[i].value+"&";
 59                             }                        
 60                         }
 61                     }                
 62                     location.href="http://www.mamicode.com/deleteBooks?"+bids;
 63                 }
 64             }
 65             else{
 66                 alert("请选择要删除的图书");
 67                 return false;
 68             }
 69         });
 70 
 71         $("#btnSearch").click(function() {            
 72             $("#f2").submit();
 73 
 74         });
 75     });    
 76     function turnPage(pageIndex){       
 77         location.href="http://www.mamicode.com/book?pageIndex="+pageIndex;
 78     } 
 79 </script>
 80 </head>
 81 <body>
 82     <c:if test="${not empty msg }">
 83         <script type="text/javascript">
 84             alert("${msg}");
 85         </script>
 86     </c:if>
 87     
 88         <table width="900" border="1">
 89             <caption>
 90                 <h1>图书信息</h1>
 91             </caption>
 92 
 93             <tr class="bg">
 94                 <td>选择</td>
 95                 <td>图书名称</td>
 96                 <td>图书作者</td>
 97                 <td>购买时间</td>
 98                 <td>图书分类</td>
 99                 <td>删除</td>
100                 <td>更新</td>
101             </tr>
102             <c:forEach items="${p.list}" var="book">
103                 <tr>
104                     <td><input type="checkbox" name="bids" value="http://www.mamicode.com/${book.bid}" /></td>
105                     <td><a href="http://www.mamicode.com/book/${book.bid}">${book.bname }</a></td>
106                     <td>${book.bauthor }</td>
107                     <td>${book.btime }</td>
108                     <td><c:choose>
109                             <c:when test="${book.btype eq 0}">计算机/软件</c:when>
110                             <c:when test="${book.btype eq 1}">小说/文摘</c:when>
111                             <c:when test="${book.btype eq 2}">杂项</c:when>
112                         </c:choose></td>
113                     <td>
114                         <%-- <a href="http://www.mamicode.com/deleteBook?bid=${book.bid}" onclick="return confirm(‘确认删除吗?‘)">删除</a> --%>
115                         <form action="book/${book.bid}" method="post">
116                             <input type="hidden" name="_method" value="http://www.mamicode.com/delete" /> <input
117                                 type="submit" value="http://www.mamicode.com/删除" />
118                         </form>                        
119                     </td>
120                     <td>
121                         <a href="http://www.mamicode.com/book/${book.bid}/edit" >更新</a>                        
122                     </td>
123                 </tr>
124             </c:forEach>
125         </table>
126         <br /> 
127         <form id="f1" action="deleteBooks">
128         <input type="checkbox" id="all" />全选&nbsp;&nbsp;
129          <input id="delBtn"
130             type="button" value="http://www.mamicode.com/删除所选" />
131         </form>    
132             
133     <div>
134     <a onclick="turnPage(1)">首页</a>&nbsp;    
135     <a onclick="turnPage(${requestScope.p.prevPage})">上一页</a>&nbsp;
136     <a onclick="turnPage(${requestScope.p.nextPage})">下一页</a>&nbsp;
137     <a onclick="turnPage(${requestScope.p.totalPage})">末页</a>&nbsp;
138     &nbsp;第 ${requestScope.p.currPage }/共 ${requestScope.p.totalPage }页
139 
140     转到第:
141      <select id="pageIndex" name="pageIndex" onchange="turnPage(this.value)">
142      <c:forEach var="i" begin="1" end="${requestScope.p.totalPage }">
143      <option value="http://www.mamicode.com/${i}">${i}</option>
144      </c:forEach>
145      </select>
146     </div>    
147     <br />
148     <br />
149     <a class="add" href="http://www.mamicode.com/book/new">新增图书信息</a>
150 </body>
151 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 <script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.8.3.js"></script>
 9 <script type="text/javascript">
10     function check() {
11         var bname = $("#bname").val();
12         var bauthor = $("#bauthor").val();
13         var btime = $("#btime").val();
14         var btype = $("#btype").val();        
15         if (bname == "") {
16             alert("图书名称不能为空");
17             return false;
18         }
19         if (bauthor == "") {
20             alert("作者姓名不能为空");
21             return false;
22         }
23         if (btime == "") {
24             alert("购买日期不能为空");
25             return false;
26         }    
27         var regBtime=/^(19|20)\d{2}-(1[0-2])|0?[1-9]-(3[0-1]|[1-2]\d|0?[1-9])$/;
28 
29         if (regBtime.test(btime) == false) {
30             alert("购买日期无效");
31             return false;
32         }
33         if (btype == -1) {
34             alert("请选择图书类别");
35             return false;
36         }
37         return true;
38     }
39     $(document).ready(function(){
40         $("#bname").val("${requestScope.book.bname}");
41         $("#bauthor").val("${requestScope.book.bauthor}");
42         $("#btime").val("${requestScope.book.btime}");
43         $("#btype").val(["${requestScope.book.btype}"]);
44     });
45 </script>
46 </head>
47 <body>
48     <form action="update" method="post" onsubmit="return check();">
49         <input type="hidden"  name="bid" value="http://www.mamicode.com/${requestScope.book.bid }"/>
50         <table width="500" border="1">
51             <caption>更新图书信息</caption>
52             <tr>
53                 <td width="91">图书名称:</td>
54                 <td width="216"><input type="text" name="bname" id="bname" /></td>
55                 <td width="171">&nbsp;</td>
56             </tr>
57             <tr>
58                 <td>图书作者:</td>
59                 <td><input type="text" name="bauthor" id="bauthor" /></td>
60                 <td>&nbsp;</td>
61             </tr>
62             <tr>
63                 <td>购买日期:</td>
64                 <td><input type="text" name="btime" id="btime" /></td>
65                 <td>yyyy-MM-dd格式</td>
66             </tr>
67             <tr>
68                 <td>图书类别:</td>
69                 <td><select id="btype" name="btype">
70                         <option value="http://www.mamicode.com/-1">--选择所属分类--</option>
71                         <option value="http://www.mamicode.com/0">计算机/软件</option>
72                         <option value="http://www.mamicode.com/1">小说/文摘</option>
73                         <option value="http://www.mamicode.com/2">杂项</option>
74                 </select></td>
75                 <td>&nbsp;</td>
76             </tr>
77             <tr>
78                 <td colspan="3" align="center"><input type="submit"
79                     value="http://www.mamicode.com/更新图书" /></td>
80             </tr>
81         </table>
82     </form>
83 </body>
84 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="upload" method="post" enctype="multipart/form-data">
11 上传者:<input type="text" name="name"/><br/>
12 上传文件:<input type="file" name="f1"/>
13 <input type="submit" value="http://www.mamicode.com/上传"/>
14 </form>
15 </body>
16 </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
上传成功!!!
</body>
</html>

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
10         
11         
12 <context:component-scan base-package="cn.jbit.ssm"></context:component-scan>
13 <!-- 连接数据库的 -->
14 <context:property-override location="classpath:db.properties"/>
15 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
16 p:driverClassName="" p:url="" p:username="" p:password=""></bean>
17 
18 <!-- dbcp数据源连接数据库
19 <context:property-placeholder location="classpath:db.properties"/>
20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
21 ></bean> -->
22 
23 <!-- 配置sqlSessionFactory -->
24 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
25  p:dataSource-ref="dataSource" 
26  p:mapperLocations="classpath:cn/jbit/ssm/dao/*.xml"
27  p:configLocation="classpath:mybatis-config.xml">
28  </bean>
29  
30 <!--  扫描接口 -->
31  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
32  p:sqlSessionFactoryBeanName="sqlSessionFactory" p:basePackage="cn.jbit.ssm.dao"></bean>
33 
34 </beans>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <typeAliases>
 6         <typeAlias alias="Book" type="cn.jbit.ssm.entity.Book"/>
 7     </typeAliases>
 8     
 9     
10 </configuration>
1 dataSource.driverClassName=com.mysql.jdbc.Driver
2 dataSource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf-8
3 dataSource.username=root
4 dataSource.password=123456
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xmlns:p="http://www.springframework.org/schema/p"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
11         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
12 
13     <context:component-scan base-package="cn.jbit"></context:component-scan>
14     <mvc:annotation-driven></mvc:annotation-driven>
15     <!-- 对静态资源的解析 -->
16     <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
17     <!-- 视图解析器 -->
18 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
19 p:suffix=".jsp"
20 p:prefix="/WEB-INF/jsp/"
21 p:viewClass="org.springframework.web.servlet.view.JstlView"
22 />
23     <!-- 配置全局异常
24     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
25         <property name="exceptionMappings">
26         <props>
27             <prop key="java.lang.Exception">error</prop>
28         </props>
29         </property>
30     </bean> -->
31     <!-- 配置文件上传的解析器 -->
32     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
33     <property name="defaultEncoding" value="utf-8"></property>
34     <property name="maxUploadSize" value="50000"></property>
35     </bean>
36 </beans>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3 
 4 
 5 
 6   <display-name>ch_ssm_bookmanage</display-name>
 7   <welcome-file-list>
 8     <welcome-file>index.html</welcome-file>
 9     <welcome-file>index.htm</welcome-file>
10     <welcome-file>index.jsp</welcome-file>
11     <welcome-file>default.html</welcome-file>
12     <welcome-file>default.htm</welcome-file>
13     <welcome-file>default.jsp</welcome-file>
14   </welcome-file-list>
15   
16   
17  <context-param>
18 <param-name>contextConfigLocation</param-name>
19 <param-value>classpath:applicationContext.xml</param-value>
20 </context-param>
21 <!-- 不配置默认WEB-INF下面的applicationContext.xml -->
22   <listener>
23   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24   </listener>
25   
26   
27  <servlet>
28  <servlet-name>springmvc</servlet-name>
29  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
30  <load-on-startup>1</load-on-startup>
31  </servlet>
32  <servlet-mapping>
33  <servlet-name>springmvc</servlet-name>
34  <url-pattern>/</url-pattern>
35  </servlet-mapping>
36  
37 <!--  配置过滤器 -->
38  <filter>
39  <filter-name>HiddenHttpMethodFilter</filter-name>
40  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
41  </filter>
42  <filter-mapping>
43  <filter-name>HiddenHttpMethodFilter</filter-name>
44  <url-pattern>/*</url-pattern>
45  </filter-mapping>
46  
47  
48 </web-app>
 1 aopalliance.jar
 2 asm-4.2.jar
 3 aspectjweaver.jar
 4 cglib-3.1.jar
 5 classmate-1.1.0.jar
 6 commons-dbcp-1.4.jar
 7 commons-fileupload-1.2.2.jar
 8 commons-io-2.4.jar
 9 commons-lang3-3.1.jar
10 commons-logging-1.2.jar
11 commons-pool-1.5.6.jar
12 commons-pool-1.6.jar
13 hibernate-validator-5.2.4.Final.jar
14 hibernate-validator-annotation-processor-5.2.4.Final.jar
15 hibernate-validator-cdi-5.2.4.Final.jar
16 jackson-annotations-2.5.0.jar
17 jackson-core-2.5.0.jar
18 jackson-databind-2.5.0.jar
19 javax.el-2.2.4.jar
20 javax.el-api-2.2.4.jar
21 jboss-logging-3.2.1.Final.jar
22 jstl.jar
23 junit-4.12.jar
24 log4j-1.2.17.jar
25 log4j-api-2.2.jar
26 log4j-core-2.2.jar
27 mybatis-3.3.0.jar
28 mybatis-spring-1.2.0.jar
29 mysql-connector-java-5.1.38-bin.jar
30 org.hamcrest.core_1.3.0.v201303031735.jar
31 slf4j-api-1.7.12.jar
32 slf4j-api-1.7.5.jar
33 slf4j-log4j12-1.7.12.jar
34 slf4j-log4j12-1.7.5.jar
35 spring-aop-4.3.0.RELEASE.jar
36 spring-aspects-4.3.0.RELEASE.jar
37 spring-beans-4.3.0.RELEASE.jar
38 spring-context-4.3.0.RELEASE.jar
39 spring-context-support-4.3.0.RELEASE.jar
40 spring-core-4.3.0.RELEASE.jar
41 spring-expression-4.3.0.RELEASE.jar
42 spring-jdbc-4.3.0.RELEASE.jar
43 spring-orm-4.3.0.RELEASE.jar
44 spring-test-4.3.0.RELEASE.jar
45 spring-tx-4.3.0.RELEASE.jar
46 spring-web-4.3.0.RELEASE.jar
47 spring-webmvc-4.3.0.RELEASE.jar
48 standard.jar
49 validation-api-1.1.0.Final.jar

 

package cn.jbit.ssm.biz;

import java.util.List;
import java.util.Map;

import cn.jbit.ssm.entity.Book;

public interface BookBiz {
    /*添加图书*/
    public int addBook(Book book) throws Exception;
    /*查询当前页所有图书*/    
    public List<Book> getAllBooksByPage(Map<String,Object> map) throws Exception;
    /*根据bid删除一本图书*/
    public int deleteBookByBid(int bid) throws Exception;
    /*更新图书*/
    public int updateBook(Book book) throws Exception;
    /*根据bid查询图书*/
    public Book getBookByBid(int bid) throws Exception;    
    /*删除多本图书*/
    public int deleteBookByBids(int [] bids) throws Exception;
    /*根据条件查询图书总记录数*/
    public int countAll(Book book) throws Exception;
    /*根据条件分页查询图书*/
    public List<Book> searchBookByPage(Map<String,Object> map) throws Exception;
    /*根据图书名称和图书分类查询图书*/
    public List<Book> searchBook(Book book) throws Exception;
}

package cn.jbit.ssm.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.jbit.ssm.biz.BookBiz;
import cn.jbit.ssm.dao.BookDao;
import cn.jbit.ssm.entity.Book;

@Service("bookBiz")
@Transactional(propagation=Propagation.REQUIRED)
public class BookBizImpl implements BookBiz {
    
    @Autowired
    private BookDao bookDao;
    @Override
    public int addBook(Book book) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.addBook(book);
    }

    @Override
    public List<Book> getAllBooksByPage(Map<String, Object> map) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.getAllBooksByPage(map);
    }

    @Override
    public int deleteBookByBid(int bid) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.deleteBookByBid(bid);
    }

    @Override
    public int updateBook(Book book) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.updateBook(book);
    }

    @Override
    public Book getBookByBid(int bid) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.getBookByBid(bid);
    }

    @Override
    public int deleteBookByBids(int[] bids) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.deleteBookByBids(bids);
    }

    @Override
    public int countAll(Book book) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.countAll(book);
    }

    @Override
    public List<Book> searchBookByPage(Map<String, Object> map) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.searchBookByPage(map);
    }

    @Override
    public List<Book> searchBook(Book book) throws Exception {
        // TODO Auto-generated method stub
        return bookDao.searchBook(book);
    }
    
}
package cn.jbit.ssm.entity;

import java.io.Serializable;
import java.sql.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.apache.ibatis.type.Alias;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;

@Alias("Book")
public class Book  implements Serializable{

    
    private static final long serialVersionUID = 1L;

    public Book(String bname, int btype) {
        super();
        this.btype = btype;
    }
    private int bid;//图书编号
    @NotNull
    private String bname;//图书名称
    @NotNull
    @Pattern(regexp="[\u4e00-\u9fe5a-zA-Z0-9_]{2,}")
    private String bauthor;//图书作者
    @Override
    public String toString() {
        return "Book [bid=" + bid + ", bname=" + bname + ", bauthor=" + bauthor
                + ", btime=" + btime + ", btype=" + btype + ", bstartTime="
                + bstartTime + ", bendTime=" + bendTime + "]";
    }
    //@DateTimeFormat(pattern="yyyy/MM/dd")
    @NotNull
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date btime;//出版时间    
    @NotNull    
    private int btype=-1;//图书类别
    
    public Date getBstartTime() {
        return bstartTime;
    }
    public void setBstartTime(Date bstartTime) {
        this.bstartTime = bstartTime;
    }
    public Date getBendTime() {
        return bendTime;
    }
    public void setBendTime(Date bendTime) {
        this.bendTime = bendTime;
    }
    private Date bstartTime;
    private Date bendTime;
    
    
    public Book() {
        super();
    }
    public Book(String bname, String bauthor, Date btime, int btype) {
        super();
        this.bname = bname;
        this.bauthor = bauthor;
        this.btime = btime;
        this.btype = btype;
    }
    public Book(int bid, String bname, String bauthor, Date btime, int btype) {
        super();
        this.bid = bid;
        this.bname = bname;
        this.bauthor = bauthor;
        this.btime = btime;
        this.btype = btype;
    }
    public int getBid() {
        return bid;
    }
    public void setBid(int bid) {
        this.bid = bid;
    }
    public String getBname() {
        return bname;
    }
    public void setBname(String bname) {
        this.bname = bname;
    }
    public String getBauthor() {
        return bauthor;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public Date getBtime() {
        return btime;
    }
    public void setBtime(Date btime) {
        this.btime = btime;
    }
    public int getBtype() {
        return btype;
    }
    public void setBtype(int btype) {
        this.btype = btype;
    }

    
}
package cn.jbit.ssm.tool;

import java.util.List;

public class Page<T> {
private int currPage=1;//当前页
private int prevPage;//上一页
private int nextPage;//下一页
private int totalCount;//总记录数
private int totalPage;//总页数
private int pageSize=5;//每页显示记录数
private List<T> list;//当前页查询结果

public int getCurrPage() {
    return currPage;
}
public void setCurrPage(int currPage) {
    this.currPage = currPage;
}
public int getPrevPage() {
    int prevPage=currPage-1;
    if(prevPage<1)
        prevPage=1;
    return prevPage;
}
public void setPrevPage(int prevPage) {
    this.prevPage = prevPage;
}
public int getNextPage() {
    int nextPage=currPage+1;
    if(nextPage>totalPage)
        nextPage=totalPage;
    return nextPage;
}
public void setNextPage(int nextPage) {
    this.nextPage = nextPage;
}
public int getTotalCount() {
    return totalCount;
}
public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
    if(totalCount>0){
        this.setTotalPage((totalCount-1)/pageSize+1);
    }
}
public int getTotalPage() {
    return totalPage;
}
public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
}
public int getPageSize() {
    return pageSize;
}
public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}
public List<T> getList() {
    return list;
}
public void setList(List<T> list) {
    this.list = list;
}

}
  1 package cn.jbit.ssm.web;
  2 
  3 import java.util.HashMap;
  4 import java.util.List;
  5 import java.util.Map;
  6 
  7 import javax.validation.Valid;
  8 
  9 import org.springframework.beans.factory.annotation.Autowired;
 10 import org.springframework.stereotype.Controller;
 11 import org.springframework.ui.Model;
 12 import org.springframework.validation.BindingResult;
 13 import org.springframework.web.bind.annotation.ModelAttribute;
 14 import org.springframework.web.bind.annotation.PathVariable;
 15 import org.springframework.web.bind.annotation.RequestMapping;
 16 import org.springframework.web.bind.annotation.RequestMethod;
 17 import org.springframework.web.bind.annotation.RequestParam;
 18 
 19 import cn.jbit.ssm.biz.BookBiz;
 20 import cn.jbit.ssm.entity.Book;
 21 import cn.jbit.ssm.tool.Page;
 22 /**
 23  * 
 24  * <p>Title:BookController</p>
 25  * <p>Description: 控制器</p>
 26  * @author  张健
 27  * @blog www.cnblogs.com/zhangjianblog
 28  * @date 2017年1月10日下午2:18:09
 29  * @version Neon Release (4.6.0)
 30  */
 31 @Controller
 32 public class BookController {
 33     
 34     @Autowired
 35     private BookBiz bookBiz;
 36     
 37     
 38     //查询当前页所有图书
 39     @RequestMapping("/book")
 40     public String index(@RequestParam(name="pageIndex",required=false)Integer pageIndex,
 41             Model model) throws Exception{
 42         Map<String,Object> map=new HashMap<String,Object>();
 43         Page<Book> p=new Page<Book>();
 44         p.setTotalCount(bookBiz.countAll(new Book()));
 45         if(null==pageIndex){
 46             pageIndex=1;
 47         }
 48         p.setCurrPage(pageIndex);
 49         map.put("startRow",(pageIndex-1)*p.getPageSize());
 50         map.put("pageSize",p.getPageSize());
 51         List<Book> list=bookBiz.getAllBooksByPage(map);
 52         p.setList(list);
 53         model.addAttribute("p",p);
 54         return "list";
 55     }
 56     
 57     
 58     @RequestMapping("/book/new")
 59     public String _new(){
 60         return "addBook";
 61     }
 62     
 63     @RequestMapping("/book/{bid}")
 64     public String show(@PathVariable("bid") int bid,Model model) throws Exception{
 65         Book book=bookBiz.getBookByBid(bid);
 66         model.addAttribute("book",book);
 67         return "detailBook";
 68     }
 69     
 70     
 71     @RequestMapping(value = "http://www.mamicode.com/book", method = {RequestMethod.POST,RequestMethod.GET})
 72     public String create(@ModelAttribute("book") @Valid Book book,
 73             BindingResult bindingResult, Model model) throws Exception {
 74         if (bindingResult.hasErrors()) {            
 75             return "addBook";
 76         }
 77         int count = bookBiz.addBook(book);
 78         if (count > 0) {
 79             model.addAttribute("msg", "添加成功!!!");
 80         } else {
 81             model.addAttribute("msg", "添加失败");
 82         }
 83         return "redirect:/book";
 84     }
 85     
 86     
 87     
 88     
 89     
 90     
 91     
 92     
 93     
 94     
 95     
 96     
 97     
 98     
 99     
100     
101     
102     
103     /*
104     
105     查询所有图书信息 
106     @Autowired
107     private BookBiz bookBiz;
108     @RequestMapping("/list")
109     public String getAllBooks(Model model) throws Exception{
110         List<Book> list=bookBiz.getAllBooks();
111         model.addAttribute("list",list);
112         return "list";
113     }
114     
115      新增图书信息 
116     @RequestMapping("/toAdd")
117     public String toAdd(){    
118         return "addBook";
119     }
120     @RequestMapping("/add")
121     public String addBook(Book book,Model model) throws Exception{
122         int count=0;
123         try {
124             count = bookBiz.addBook(book);
125         } catch (Exception e) {
126             // TODO Auto-generated catch block
127             e.printStackTrace();
128         }
129         if(count>0){
130             model.addAttribute("msg", "添加成功");
131         }else{
132             model.addAttribute("msg", "添加失败");
133         
134         }
135         return "forward:list";
136     }
137     
138      根据bid删除图书信息 
139     @RequestMapping(value="http://www.mamicode.com/delete/{bid}",method=RequestMethod.DELETE)
140     public String deleteBookByBid(@PathVariable("bid") int bid,RedirectAttributes ra) throws Exception{
141         
142         int count=0;
143         try {
144             count = bookBiz.deleteBookByBid(bid);
145         } catch (Exception e) {
146             // TODO Auto-generated catch block
147             e.printStackTrace();
148         }
149         if(count>0){
150             ra.addFlashAttribute("msg", "删出成功");
151         }else{
152             ra.addFlashAttribute("msg", "删除失败");
153         }
154         return "redirect:/list";
155     }
156     @RequestMapping("/delete")
157     public String deleteBookByBid(@RequestParam("bid") int bid,Model model) throws Exception{
158         int count=0;
159         try {
160             count = bookBiz.deleteBookByBid(bid);
161         } catch (Exception e) {
162             // TODO Auto-generated catch block
163             e.printStackTrace();
164         }
165         if(count>0){
166             model.addAttribute("msg", "删除成功");
167         }else{
168             model.addAttribute("msg", "删除失败");
169         }
170         return "forward:list";
171     }
172     
173     更新图书信息 
174     @RequestMapping(value="http://www.mamicode.com/update",method={RequestMethod.PUT})
175     public String updateBook(Book book,Model model,RedirectAttributes ra) throws Exception{
176         int count=0;
177         try {
178             count = bookBiz.updateBook(book);
179         } catch (Exception e) {
180             // TODO Auto-generated catch block
181             e.printStackTrace();
182         }
183         if(count>0){
184             ra.addFlashAttribute("msg", "更新成功");
185         }else{
186             ra.addFlashAttribute("msg", "更新失败");
187         }
188         return "redirect:/list";
189     }
190     
191     @ExceptionHandler(value=http://www.mamicode.com/Exception.class)>192     public String defaultHandlerException(Exception e,Model m){
193         m.addAttribute("e",e);
194         return "error";
195     }
196     @RequestMapping("/testException") 
197     public String testException() throws Exception{
198         throw new Exception("测试全局异常");
199     }*/
200 }

 

package cn.jbit.ssm.dao;

import java.util.List;
import java.util.Map;

import cn.jbit.ssm.entity.Book;
/**
 * 
 * <p>Title:BookDao</p>
 * <p>Description: </p>
 * @author  张健
 * @blog www.cnblogs.com/zhangjianblog
 * @date 2017年1月10日上午10:45:32
 * @version Neon Release (4.6.0)
 */


public interface BookDao {
    
    /*添加图书*/
    public int addBook(Book book);
    /*查询当前页所有图书*/    
    public List<Book> getAllBooksByPage(Map<String,Object> map);
    /*根据bid删除一本图书*/
    public int deleteBookByBid(int bid);
    /*更新图书*/
    public int updateBook(Book book);
    /*根据bid查询图书*/
    public Book getBookByBid(int bid);    
    /*删除多本图书*/
    public int deleteBookByBids(int [] bids);
    /*根据条件查询图书总记录数*/
    public int countAll(Book book);
    /*根据条件分页查询图书*/
    public List<Book> searchBookByPage(Map<String,Object> map);
    /*根据图书名称和图书分类查询图书*/
    public List<Book> searchBook(Book book);
}




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.jbit.ssm.dao.BookDao">

  <resultMap type="Book" id="BookResultMap">
 <id property="bid" column="b_id"></id>
<result property="bname" column="b_name"/>
<result property="bauthor" column="b_author"/>
<result property="btime" column="b_time"/>
<result property="btype" column="b_type"/>
  </resultMap>
  
  
  <select id="getAllBooksByPage" parameterType="hashmap" resultMap="BookResultMap">
          select *from book limit #{startRow},#{pageSize}
  </select>
  <select id="searchBookByPage" parameterType="hashmap" resultMap="BookResultMap">
        select * from book  
        <trim prefix="where" prefixOverrides="and">
        <if test="bname!=null">
            and b_name like concat(‘%‘,#{bname},‘%‘)
        </if>
        <if test="bauthor!=null">
            and b_author like concat(‘%‘,#{bauthor},‘%‘)
        </if>
        <if test="bstartTime!=null">
            and b_time >=#{bstartTime}
        </if>
        <if test="bendTime!=null">
            and b_time <![CDATA[<]]>=#{bendTime}
        </if>
            <if test="btype!=-1">
            and b_type=#{btype}
        </if>
        </trim>
        limit #{startRow},#{pageSize}
    </select>
    <select id="countAll" resultType="int" parameterType="Book">
        SELECT count(1) FROM book
        <trim prefix="where" prefixOverrides="and">
        <if test="bname!=null">
            and b_name like concat(‘%‘,#{bname},‘%‘)
        </if>
        <if test="bauthor!=null">
            and b_author like concat(‘%‘,#{bauthor},‘%‘)
        </if>
        <if test="bstartTime!=null">
            and b_time >=#{bstartTime}
        </if>
        <if test="bendTime!=null">
            and b_time <![CDATA[<]]>=#{bendTime}
        </if>
            <if test="btype!=-1">
            and b_type=#{btype}
        </if>
        </trim>
    </select>
  
  <select id="getAllBooks" resultMap="BookResultMap">
  select *from book
  </select>
  
  
  <delete id="deleteBookByBid" parameterType="int">
delete from book where b_id=#{bid}
</delete>


<insert id="addBook" parameterType="Book">
insert into book(b_name,b_author,b_time,b_type) values (#{bname},#{bauthor},#{btime},#{btype})
</insert>


<update id="updateBook" parameterType="Book">
<!-- update book set b_name=#{bname},b_author=#{bauthor},b_time=#{btime},b_type=#{btype} where b_id=#{bid} -->
update Book
    <set>
            <if test="bname!=null">b_name=#{bname}</if>
            <if test="bauthor!=null">b_author=#{bauthor}</if>
            <if test="btime!=null">b_time=#{btime}</if>
            <if test="btype!=null">b_type=#{btype}</if>
    </set>
</update>


<select id="getBookByBid" parameterType="int" resultMap="BookResultMap">
select *from book where b_id=#{bid}
</select>


<select id="searchBook" parameterType="Book" resultMap="BookResultMap">
select *from book where b_name like b_name=#{bname} and b_type=#{btype}
</select>

</mapper>

 

基于spring+springmvc+mybatis的图书管理系统