SSM-spring+springmvc+mybatis實現圖書管理系統登錄和增刪改查以及加入購
物車
在整合之前,首先在MySQL數據庫中創建好用戶表和書籍表
用戶表
CREATE TABLE `tb_users` (
`username` varchar(10) NOT NULL,
`password` varchar(18) NOT NULL,
`email` varchar(18) NOT NULL,
`sex` varchar(2) NOT NULL,
`likes` varchar(100) DEFAULT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
書籍表
CREATE TABLE `tb_books` (
`id` varchar(10) NOT NULL,
`name` varchar(20) NOT NULL,
`author` varchar(20) NOT NULL,
`publish`
varchar(20) NOT NULL,
`price` double NOT NULL,
`des` varchar
(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT
CHARSET=utf8
1、導入jar包
2、創建mybatis核心配置文件SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6 <!-- 設置別名 -->
7 <typeAliases>
8 <typeAlias type="com.lq.model.User" alias="user"/>
9 <typeAlias type="com.lq.model.Book" alias="book"/>
10 </typeAliases>
11
12 <!-- 配置mapper映射文件 -->
13 <mappers>
14 <mapper resource="com/lq/model/UserMapper.xml"/>
15 <mapper resource="com/lq/model/BookMapper.xml"/>
16 </mappers>
17 </configuration>
3、創建spring配置文件applicationContext.xml
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" xmlns:tx="http://www.springframework.org/schema/tx"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-3.2.xsd
10 http://www.springframework.org/schema/tx
11 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
12 http://www.springframework.org/schema/aop
13 http://www.springframework.org/schema/aop/spring-aop.xsd
14 ">
15
16
17 <!-- 數據源 -->
18 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
20 <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
21 <property name="username" value="root"></property>
22 <property name="password" value="root"></property>
23 </bean>
24 <!-- 事務管理器 -->
25 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
26 <property name="dataSource" ref="dataSource"/>
27 </bean>
28
29 <!-- 通知 -->
30 <tx:advice id="txAdvice" transaction-manager="txManager">
31 <!-- 配置事務屬性 隔離界別 傳播機制 -->
32 <tx:attributes>
33 <!-- *代表連接點匹配的所有方法 rollback-for 遇到任何異常都進行事務回滾-->
34 <tx:method name="insert*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
35 <tx:method name="delete*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
36 <tx:method name="update*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
37 <tx:method name="select*" read-only="true" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
38 </tx:attributes>
39 </tx:advice>
40
41 <aop:config>
42 <!-- 連接點表達式 匹配所有的 切入點 -->
43 <aop:pointcut id="fooServiceOperation" expression="execution(* com.lq.dao.*.*(..))"/>
44 <!-- 通知加入到切入點 -->
45 <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
46
47 </aop:config>
48
49
50
51 <!-- 配置sqlSessionFactory -->
52 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
53 <!-- mybatis配置文件路徑 -->
54 <property name="configLocation" value="classpath:config/SqlMapConfig.xml" />
55 <property name="dataSource" ref="dataSource" />
56 </bean>
57
58 <!-- 配置sqlsession 產生這個實例就是通過 sqlsessionTemplate來實現的 -->
59 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
60 <constructor-arg index="0">
61 <ref bean="sqlSessionFactory" />
62 </constructor-arg>
63 </bean>
64
65
66 <!-- <bean id="userDao" class="com.lq.dao.UserDao">
67 <property name="sqlSession" ref="sqlSession"></property>
68 </bean> -->
69
70
71 </beans>
4、創建springMVC配置文件spring-servlet.xml
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:p="http://www.springframework.org/schema/p"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd
12 http://www.springframework.org/schema/mvc
13 http://www.springframework.org/schema/mvc/spring-mvc.xsd
14 ">
15
16
17 <!-- 設置掃描包 -->
18 <context:component-scan base-package="com.lq"/>
19 <!-- 開啟spring mvc的注解功能 -->
20 <mvc:annotation-driven />
21
22 <!-- 視圖解析器 -->
23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24 <!-- 視圖后綴,controller中的方法返回的url字符串會添加該后綴 -->
25 <property name="suffix" value=".jsp"/>
26 <!-- 視圖前綴controller中的方法返回的url字符串會添加該前綴 -->
27 <property name="prefix" value="/WEB-INF/pages/"/>
28 </bean>
29
30
31 </beans>
5、在web.xml中配置spring監聽器、前端控制器,並配置需要啟動就要加載的配
置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
3 <display-name>SSM_case1_library</display-name>
4 <listener>
5 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
6 </listener>
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>
10 classpath:config/applicationContext.xml 11 </param-value>
12 </context-param>
13 <filter>
14 <filter-name>encodingFilter</filter-name>
15 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
16 <init-param>
17 <param-name>encoding</param-name>
18 <param-value>utf-8</param-value>
19 </init-param>
20 <init-param>
21 <param-name>forceEncoding</param-name>
22 <param-value>true</param-value>
23 </init-param>
24 </filter>
25 <filter-mapping>
26 <filter-name>encodingFilter</filter-name>
27 <url-pattern>*.do</url-pattern>
28 </filter-mapping>
29 <servlet>
30 <servlet-name>springmvc</servlet-name>
31 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
32 <init-param>
33 <param-name>contextConfigLocation</param-name>
34 <param-value>classpath:config/spring-servlet.xml</param-value>
35 </init-param>
36 <load-on-startup>1</load-on-startup>
37 </servlet>
38 <servlet-mapping>
39 <servlet-name>springmvc</servlet-name>
40 <url-pattern>*.do</url-pattern>
41 </servlet-mapping>
42 </web-app>
6、在src下導入log4j.properties
前期工作基本完成,下面是主要代碼:
1、用戶登錄
(1)創建模型
User
1 package com.lq.model; 2
3 public class User { 4 private String username; 5 private String password; 6 private String email; 7 private String sex; 8 private String likes; 9 public String getUsername() { 10 return username; 11 } 12 public void setUsername(String username) { 13 this.username = username; 14 } 15 public String getPassword() { 16 return password; 17 } 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 public String getEmail() { 22 return email; 23 } 24 public void setEmail(String email) { 25 this.email = email; 26 } 27
28 public String getSex() { 29 return sex; 30 } 31 public void setSex(String sex) { 32 this.sex = sex; 33 } 34 public String getLikes() { 35 return likes; 36 } 37 public void setLikes(String likes) { 38 this.likes = likes; 39 } 40 public User(String username, String password, String email,String sex, String likes) { 41 super(); 42 this.username = username; 43 this.password = password; 44 this.email = email; 45 this.likes = likes; 46 this.sex = sex; 47 } 48 public User() { 49 super(); 50 } 51
52 }
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.lq.model.UserMapper">
6 <select id="doLogin" parameterType="user" resultType="user">
7 select * from tb_users where username=#{username} and password=#{password} 8 </select>
9
10 </mapper>
(2)創建數據交互層Dao
BaseDao(這里通過BaseDao來實現sqlsessionTemplate)
1 package com.lq.dao; 2
3 import org.mybatis.spring.SqlSessionTemplate; 4 import org.springframework.beans.factory.annotation.Autowired; 5
6 public class BaseDao { 7 @Autowired 8
9 //@Qualifier("sqlSession")
10 protected SqlSessionTemplate sqlSession; 11
12 public SqlSessionTemplate getSqlSession() { 13 return sqlSession; 14 } 15
16 public void setSqlSession(SqlSessionTemplate sqlSession) { 17 this.sqlSession = sqlSession; 18 } 19
20 }
UserDao
1 package com.lq.dao; 2
3 import org.mybatis.spring.SqlSessionTemplate; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Repository; 6
7 import com.lq.model.User; 8
9 @Repository 10 public class UserDao extends BaseDao { 11
12
13 public User doLogin(User user){ 14 return (User)sqlSession.selectOne("com.lq.model.UserMapper.doLogin",user); 15 } 16
17
18
19
20 }
(3)創建業務邏輯層Service
IUService
1 package com.lq.service; 2
3 import java.util.List; 4
5 import com.lq.model.User; 6
7 public interface IUserService { 8 public User doLogin(User user); 9
10
11
12 }
UserServiceImpl
1 package com.lq.service; 2
3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5
6 import com.lq.dao.UserDao; 7 import com.lq.model.User; 8
9 @Service 10 public class UserServiceImpl implements IUserService { 11 @Autowired 12 private UserDao userDao; 13
14 @Override 15 public User doLogin(User user) { 16 return userDao.doLogin(user); 17 } 18
19 public UserDao getUserDao() { 20 return userDao; 21 } 22
23 public void setUserDao(UserDao userDao) { 24 this.userDao = userDao; 25 } 26
27
28
29
30
31 }
(4)創建控制層Controller
UserController
1 package com.lq.controller; 2
3 import java.util.List; 4
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.ModelAttribute; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.servlet.ModelAndView; 11
12 import com.lq.model.User; 13 import com.lq.service.IBookService; 14 import com.lq.service.IUserService; 15
16 @Controller 17 @RequestMapping("/user") 18 public class UserController { 19
20 @Autowired 21 private IUserService userService; 22
23 @Autowired 24 private IBookService bookService; 25
26 //實現用戶登錄功能
27 @RequestMapping("dologin.do") 28 public ModelAndView doLogin(ModelAndView mv,User user){ 29
30 User u=userService.doLogin(user); 31 if(u!=null){ 32 List books=bookService.selectBook(null); 33 mv.addObject("books",books); 34 mv.setViewName("book"); 35 }else{ 36 mv.setViewName("login"); 37 mv.addObject("flag","用戶名或密碼錯誤,請重新輸入!"); 38 } 39 return mv; 40
41 } 42
43 public IBookService getBookService() { 44 return bookService; 45 } 46 public void setBookService(IBookService bookService) { 47 this.bookService = bookService; 48 } 49
50 public IUserService getUserService() { 51 return userService; 52 } 53
54 public void setUserService(IUserService userService) { 55 this.userService = userService; 56 } 57
58 }
前端頁面部分
login.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html lang="zh-CN">
5 <head>
6 <meta charset="UTF-8">
7
8 <link rel="stylesheet" href="css/login.css">
9 <script type="text/javascript" src="js/jquery.min.js"></script>
10 <title>后台登陸</title>
11 </head>
12 <body>
13 <div id="login_top">
14 <div id="welcome">
15 歡迎使用鄭航圖書管理系統 16 </div>
17 <div id="back">
18 <a href="#">返回首頁</a> | 19 <a href="#">幫助</a>
20 </div>
21 </div>
22 <div id="login_center">
23 <div id="login_area">
24 <div id="login_form">
25 <form action="user/dologin.do" method="post">
26 <div id="login_tip">
27 用戶登錄 UserLogin 28 </div>
29 <div><input type="text" class="username" name="username"></div>
30 <div><input type="text" class="pwd" name="password"></div>
31 <div id="btn_area">
32 <input type="hidden" name="method" value="login"/>
33 <input type="submit" name="submit" id="sub_btn" value="登 錄"> 34 <input type="text" class="verify">
35 <img src="images/login/verify.png" alt="" width="80" height="40">
36 </div>
37 </form>
38 </div>
39 </div>
40 </div>
41 <div id="login_bottom">
42 藍旗班版權所有2018-1028
43 </div>
44 </body>
45 </html>
book.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="java.util.*,com.lq.model.*" %>
3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4
5 <%
6 String path = request.getContextPath();
7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
8 %>
9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 <html>
11 <head>
12 <!-- 設置請求服務器的基准路徑 -->
13 <base href="<%=basePath%>">
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15 <title>Insert title here</title>
16 <link rel="stylesheet" href="css/common.css">
17 <link rel="stylesheet" href="css/main.css">
18 <script type="text/javascript" src="js/jquery.min.js"></script>
19 <script type="text/javascript" src="js/colResizable-1.3.min.js"></script>
20 <script type="text/javascript" src="js/common.js"></script>
21 <style type="text/css">
22 .main{ 23 width: 800px; 24 height:780px; 25 margin: 0 auto; 26 background: url('img/1.jpg')no-repeat; 27 } 28 .top{ 29 height:100px; 30 background-color: skyblue; 31 font-size: 90px; 32 font-family: "隸書"; 33 line-height: 100px ; 34 } 35 </style>
36 </head>
37 <body>
38 <div class="main" >
39
40 <div class="top">
41 鄭航圖書管理系統 42 </div>
43 <div class="box_top"><b class="pl15">搜索圖書</b></div>
44 <!-- 搜索開始 -->
45 <div class="box_center pt10 pb10">
46 <form action="book/selectBook.do" method="post">
47 <table class="form_table" border="0" cellpadding="0" cellspacing="0">
48 <tbody>
49 <tr>
50 <td>圖書名稱</td>
51 <td><input type="text" name="name" value="${book.name }" class="input-text lh25" size="20"></td>
52
53 <td>圖書編號</td>
54 <td><input type="text" name="id" value="${book.id }" class="input-text lh25" size="20"></td>
55 <td>
56 <input type="hidden" name="method" value="query">
57 <input type="submit" name="button" class="btn btn82 btn_search" value="查詢">
58 <input type="button" name="button" class="btn btn82 btn_add" value="返回">
59 </td>
60 </tr>
61 </tbody>
62 </table>
63 </form>
64 </div>
65 <div class="box_top">
66 <input id="add" type="button" name="button" class="btn btn82 btn_add" value="新增">
67 </div>
68
69 </div>
70 <!-- 列表開始 -->
71 <div id="table" class="mt10">
72 <div class="box span10 oh">
73 <div class="CRC" style="width: 1007px;">
74 <div class="CRG" style="left: 32px; height: 326px;">
75 <div class="grip"></div>
76 <div class="CRZ" style="cursor:e-resize"></div>
77 </div><div class="CRG" style="left: 134px; height: 326px;">
78 <div class="grip"></div>
79 <div class="CRZ" style="cursor:e-resize"></div>
80 </div>
81 <div class="CRG" style="left: 235px; height: 326px;">
82 <div class="grip"></div>
83 <div class="CRZ" style="cursor:e-resize"></div>
84 </div>
85 <div class="CRL" style="left: 1008px; height: 326px;"></div>
86 </div>
87 <table style="text-align: center" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table CRZ" id="CRZ0">
88 <tbody><tr>
89
90 <th style="width: 50%;">圖書編號</th>
91 <th style="width: 50%;">圖書名稱</th>
92 <th style="width: 50%;">圖書作者</th>
93 <th style="width: 50%;">圖書出版社</th>
94 <th style="width: 50%;">圖書價格</th>
95 <th style="width: 90%;">操作</th>
96 </tr>
97
98 <c:forEach items="${books }" var="book" begin="0" step="1" varStatus="stat">
99 <tr class="tr" style="background-color: rgb(255, 255, 255);">
100 <td>${book.id }</td>
101 <td>${book.name }</td>
102 <td>${book.author}</td>
103 <td>${book.publish}</td>
104 <td> ${book.price}</td>
105 <td>
106 <a href="book/${book.id}/delete.do" class="ext_btn ext_btn_submit"> 刪除</a>
107 <a href="book/${book.id}/toUpdate.do" class="ext_btn ext_btn_submit"> 修改</a>
108 <a href="book/${book.id}/addCart.do" class="ext_btn ext_btn_submit"> 加入購物車</a>
109 </td>
110 </tr>
111 </c:forEach>
112 </tbody></table>
113
114 <!-- 分頁 -->
115 <div class="page mt10">
116 <div class="pagination">
117
118 <ul>
119 <c:if test="${pageindex!=null && pageindex!=1 }">
120 <li class="first-child"><a href="BookServlet?method=queryBook&pageindex=1">首頁</a></li>
121 <li ><a href="BookServlet?method=queryBook&pageindex=${pageindex-1 }">上一頁</a></li>
122 </c:if>
123 <c:if test="${pageindex!=null && pageindex!=totalPage}">
124 <li><a href="BookServlet?method=queryBook&pageindex=${pageindex+1 }">下一頁</a></li>
125 <li class="last-child"><a href="BookServlet?method=queryBook&pageindex=${totalPage }">末頁</a></li>
126 </c:if>
127 </ul>
128 </div>
129
130 </div>
131 </div>
132 </div>
133 </div>
134 </body>
135 <script type="text/javascript">
136 $(function(){ 137 $("#add").click(function(){ 138 window.location.href="book/addBook.do"; 139 }) 140 }) 141
142 $(function(){ 143 $("#btn btn82 btn_add").click(function(){ 144 window.location.href="book.jsp"; 145 }) 146 }) 147 </script>
148
149 </html>
2、實現圖書的增刪改查
(1)創建模型
Book
1 package com.lq.model; 2
3 public class Book { 4 private String id ; 5 private String name ; 6 private String author; 7 private String publish; 8 private Double price; 9 private String des; 10 private Integer count=1; 11 private int pageindex; 12
13 public Book() { 14 super(); 15 } 16 public String getId() { 17 return id; 18 } 19 public void setId(String id) { 20 this.id = id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public String getAuthor() { 29 return author; 30 } 31 public void setAuthor(String author) { 32 this.author = author; 33 } 34 public String getPublish() { 35 return publish; 36 } 37 public void setPublish(String publish) { 38 this.publish = publish; 39 } 40
41 public Double getPrice() { 42 return price; 43 } 44 public void setPrice(Double price) { 45 this.price = price; 46 } 47 public String getDes() { 48 return des; 49 } 50 public void setDes(String des) { 51 this.des = des; 52 } 53
54 public Integer getCount() { 55 return count; 56 } 57 public void setCount(Integer count) { 58 this.count = count; 59 } 60 public int getPageindex() { 61 return pageindex; 62 } 63 public void setPageindex(int pageindex) { 64 this.pageindex = pageindex; 65 } 66
67
68
69 }
BookMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.lq.model.BookMapper">
6 <select id="selectBook" parameterType="book" resultType="book">
7 select * from tb_books 8 <where>
9 <if test="name!=null and name!=''">
10 name like concat('%',#{name},'%') 11 </if>
12 <if test="id!=null and id!=''">
13 id =#{id} 14 </if>
15 </where>
16 </select>
17 <delete id="deleteBookById" parameterType="string">
18 delete from tb_books where id={bookId} 19 </delete>
20 <insert id="insertBook" parameterType="book">
21 insert into tb_books values(#{id},#{name},#{author},#{publish},#{price},#{des}) 22 </insert>
23 <update id="updateBook" parameterType="book">
24 update tb_books set name=#{name},author=#{author},publish=#{publish},price=#{price},des=#{des} where id=#{id} 25 </update>
26 <select id="selectBookById" parameterType="book" resultType="book">
27 select * from tb_books where id=#{bookId} 28
29 </select>
30 </mapper>
(2)創建數據交互層Dao
BookDao
1 package com.lq.dao; 2
3 import java.util.List; 4
5 import org.springframework.stereotype.Repository; 6
7 import com.lq.model.Book; 8
9 @Repository//把dao注入到spring容器中
10 public class BookDao extends BaseDao { 11
12 public List selectBook(Book book){ 13 return sqlSession.selectList("com.lq.model.BookMapper.selectBook",book); 14
15 } 16
17 public void deleteBookById(String bookId) { 18 sqlSession.delete("com.lq.model.BookMapper.deleteBookById",bookId); 19
20 } 21
22 public void insertBook(Book book) { 23 sqlSession.insert("com.lq.model.BookMapper.insertBook",book); 24
25 } 26
27 public void updateBook(Book book) { 28 sqlSession.update("com.lq.model.BookMapper.updateBook",book); 29 } 30
31 public Book selectBookById(String bookId) { 32 return (Book)sqlSession.selectOne("com.lq.model.BookMapper.selectBookById",bookId); 33
34 } 35
36
37 }
(3)創建業務邏輯層Service
IBookService
1 package com.lq.service; 2
3
4 import java.util.List; 5
6 import com.lq.model.Book; 7
8 public interface IBookService { 9 //查詢
10 public List selectBook(Book book); 11 //刪除
12 public void deleteBookById(String bookId); 13 //新增
14 public void insertBook(Book book); 15 //修改
16 public void updateBook(Book book); 17 //購物車查詢圖書
18 public Book selectBookById(String bookId); 19 }
BookService
1 package com.lq.service; 2
3 import java.util.List; 4
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7
8 import com.lq.dao.BookDao; 9 import com.lq.model.Book; 10
11 @Service 12 public class BookService implements IBookService{ 13
14 @Autowired 15 private BookDao bookDao; 16
17 public BookDao getBookDao() { 18 return bookDao; 19 } 20
21 public void setBookDao(BookDao bookDao) { 22 this.bookDao = bookDao; 23 } 24
25 @Override 26 public List selectBook(Book book) { 27
28 return bookDao.selectBook(book); 29 } 30
31 @Override 32 public void deleteBookById(String bookId) { 33 bookDao.deleteBookById(bookId); 34 } 35
36 @Override 37 public void insertBook(Book book) { 38 bookDao.insertBook(book); 39 } 40
41 @Override 42 public void updateBook(Book book) { 43 bookDao.updateBook(book); 44 } 45
46 @Override 47 public Book selectBookById(String bookId) { 48 return bookDao.selectBookById(bookId); 49 } 50
51 }
(4)創建控制層Controller
BookController
1 package com.lq.controller; 2
3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6
7 import javax.servlet.http.HttpSession; 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.web.bind.annotation.PathVariable; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.servlet.ModelAndView; 15
16 import com.lq.model.Book; 17 import com.lq.service.IBookService; 18
19 @Controller 20 @RequestMapping("/book") 21 public class BookController { 22 @Autowired 23 private IBookService bookService; 24
25 @RequestMapping("/selectBook.do") 26 public ModelAndView selectBook(ModelAndView mv,Book book){ 27 List books=bookService.selectBook(book); 28 mv.addObject("books",books); 29 mv.setViewName("book"); 30 return mv; 31 } 32
33 @RequestMapping("/{bookId}/delete.do") 34 public ModelAndView deleteBook(@PathVariable("bookId")String bookId,ModelAndView mv){ 35 //刪除
36 bookService.deleteBookById(bookId); 37 //查詢所有圖書
38 List books=bookService.selectBook(null); 39 mv.addObject("books",books); 40 mv.setViewName("book"); 41 return mv; 42 } 43
44 @RequestMapping("/toAddBook.do") 45 public String toAddBook(){ 46 return "addbook"; 47 } 48 @RequestMapping("/addBook.do") 49 public ModelAndView addBook(ModelAndView mv,Book book){ 50 //新增圖書
51 bookService.insertBook(book); 52 //置空book對象 53
54 //查詢
55 List books=bookService.selectBook(null); 56 mv.addObject("books",books); 57 mv.setViewName("book"); 58 return mv; 59 } 60 @RequestMapping("/{bookId}/toUpdate.do") 61 public String toUpdate(@PathVariable String bookId,Model model){ 62 Book book=new Book(); 63 book.setId(bookId); 64 List<Book>books=bookService.selectBook(book); 65 if(books!=null){ 66 book=books.get(0); 67 } 68 model.addAttribute("book",book); 69 return "updatebook"; 70 } 71
72 @RequestMapping("/update.do") 73 public ModelAndView updateBook(Book book,ModelAndView mv){ 74 //修改
75 bookService.updateBook(book); 76 //置空book對象 77
78 //查詢所有圖書
79 List books=bookService.selectBook(null); 80 mv.addObject("books",books); 81 mv.setViewName("book"); 82 return mv; 83 } 84
85 @RequestMapping("/{bookId}/addCart.do") 86 public ModelAndView addCart(@PathVariable("bookId")String bookId,ModelAndView mv,HttpSession session){ 87 //添加購物車
88 Book book=bookService.selectBookById(bookId); 89 //先從session中獲取購物車
90 Object obj=session.getAttribute("cart"); 91 //第一次加入購物車
92 if(obj==null){ 93 Map<String,Book>cart=new HashMap<String,Book>(); 94 //向購物車添加一本書
95 cart.put(bookId, book); 96 //把購物車放入session
97 session.setAttribute("cart", cart); 98 }else{ 99 Map<String,Book>cart=(Map<String,Book>)obj; 100 Book bk=cart.get(bookId); 101 if(bk==null){ 102 cart.put(bookId, book); 103 }else{ 104 bk.setCount(bk.getCount()+1); 105 } 106 cart.put(bookId, book); 107 } 108 mv.setViewName("cart"); 109 return mv; 110 } 111
112 public IBookService getBookService() { 113 return bookService; 114 } 115
116 public void setBookService(IBookService bookService) { 117 this.bookService = bookService; 118 } 119
120 }
前端頁面部分
book.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="java.util.*,com.lq.model.*" %>
3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4
5 <%
6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %>
9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 <html>
11 <head>
12 <!-- 設置請求服務器的基准路徑 -->
13 <base href="<%=basePath%>">
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15 <title>Insert title here</title>
16 <link rel="stylesheet" href="css/common.css">
17 <link rel="stylesheet" href="css/main.css">
18 <script type="text/javascript" src="js/jquery.min.js"></script>
19 <script type="text/javascript" src="js/colResizable-1.3.min.js"></script>
20 <script type="text/javascript" src="js/common.js"></script>
21 <style type="text/css">
22 .main{ 23 width: 800px; 24 height:780px; 25 margin: 0 auto; 26 background: url('img/1.jpg')no-repeat; 27 } 28 .top{ 29 height:100px; 30 background-color: skyblue; 31 font-size: 90px; 32 font-family: "隸書"; 33 line-height: 100px ; 34 } 35 </style>
36 </head>
37 <body>
38 <div class="main" >
39
40 <div class="top">
41 鄭航圖書管理系統 42 </div>
43 <div class="box_top"><b class="pl15">搜索圖書</b></div>
44 <!-- 搜索開始 -->
45 <div class="box_center pt10 pb10">
46 <form action="book/selectBook.do" method="post">
47 <table class="form_table" border="0" cellpadding="0" cellspacing="0">
48 <tbody>
49 <tr>
50 <td>圖書名稱</td>
51 <td><input type="text" name="name" value="${book.name }" class="input-text lh25" size="20"></td>
52
53 <td>圖書編號</td>
54 <td><input type="text" name="id" value="${book.id }" class="input-text lh25" size="20"></td>
55 <td>
56 <input type="hidden" name="method" value="query">
57 <input type="submit" name="button" class="btn btn82 btn_search" value="查詢">
58 <input type="button" name="button" class="btn btn82 btn_add" value="返回">
59 </td>
60 </tr>
61 </tbody>
62 </table>
63 </form>
64
65 <div class="box_top">
66 <input id="add" type="button" name="button" class="btn btn82 btn_add" value="新增">
67
68
69 </div>
70 <!-- 列表開始 -->
71 <div id="table" class="mt10">
72 <div class="box span10 oh">
73 <div class="CRC" style="width: 1007px;">
74 <div class="CRG" style="left: 32px; height: 326px;">
75 <div class="grip"></div>
76 <div class="CRZ" style="cursor:e-resize"></div>
77 </div><div class="CRG" style="left: 134px; height: 326px;">
78 <div class="grip"></div>
79 <div class="CRZ" style="cursor:e-resize"></div>
80 </div>
81 <div class="CRG" style="left: 235px; height: 326px;">
82 <div class="grip"></div>
83 <div class="CRZ" style="cursor:e-resize"></div>
84 </div>
85 <div class="CRL" style="left: 1008px; height: 326px;"></div>
86 </div>
87 <table style="text-align: center" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table CRZ" id="CRZ0">
88 <tbody><tr>
89
90 <th style="width: 50%;">圖書編號</th>
91 <th style="width: 50%;">圖書名稱</th>
92 <th style="width: 50%;">圖書作者</th>
93 <th style="width: 50%;">圖書出版社</th>
94 <th style="width: 50%;">圖書價格</th>
95 <th style="width: 90%;">操作</th>
96 </tr>
97
98 <c:forEach items="${books }" var="book" begin="0" step="1" varStatus="stat">
99 <tr class="tr" style="background-color: rgb(255, 255, 255);">
100 <td>${book.id }</td>
101 <td>${book.name }</td>
102 <td>${book.author}</td>
103 <td>${book.publish}</td>
104 <td> ${book.price}</td>
105 <td>
106 <a href="book/${book.id}/delete.do" class="ext_btn ext_btn_submit"> 刪除</a>
107 <a href="book/${book.id}/toUpdate.do" class="ext_btn ext_btn_submit"> 修改</a>
108 <a href="book/${book.id}/addCart.do" class="ext_btn ext_btn_submit"> 加入購物車</a>
109 </td>
110 </tr>
111 </c:forEach>
112 </tbody></table>
113
114 <!-- 分頁 -->
115 <div class="page mt10">
116 <div class="pagination">
117
118 <ul>
119 <c:if test="${pageindex!=null && pageindex!=1 }">
120 <li class="first-child"><a href="BookServlet?method=queryBook&pageindex=1">首頁</a></li>
121 <li ><a href="BookServlet?method=queryBook&pageindex=${pageindex-1 }">上一頁</a></li>
122 </c:if>
123 <c:if test="${pageindex!=null && pageindex!=totalPage}">
124 <li><a href="BookServlet?method=queryBook&pageindex=${pageindex+1 }">下一頁</a></li>
125 <li class="last-child"><a href="BookServlet?method=queryBook&pageindex=${totalPage }">末頁</a></li>
126 </c:if>
127 </ul>
128 </div>
129
130 </div>
131 </div>
132 </div>
133 </div>
134 </body>
135 <script type="text/javascript">
136 $(function(){ 137 $("#add").click(function(){ 138 window.location.href="book/addBook.do"; 139 }) 140 }) 141
142 $(function(){ 143 $("#btn btn82 btn_add").click(function(){ 144 window.location.href="book.jsp"; 145 }) 146 }) 147 </script>
148
149 </html>
addbook.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3
4 <%
5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %>
8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
9 <html>
10 <head>
11 <!-- 設置請求服務器的基准路徑 -->
12 <base href="<%=basePath%>">
13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
14 <title>Insert title here</title>
15 <link rel="stylesheet" href="css/common.css">
16 <link rel="stylesheet" href="css/main.css">
17 <script type="text/javascript" src="js/jquery.min.js"></script>
18 <script type="text/javascript" src="js/colResizable-1.3.min.js"></script>
19 <script type="text/javascript" src="js/common.js"></script>
20 <style type="text/css">
21 .main{ 22 width: 800px; 23 height:780px; 24 margin: 0 auto; 25 background-image: url("img/1.jpg"); 26 } 27 .top{ 28 height:100px; 29 background-color: skyblue; 30 font-size: 90px; 31 font-family: "隸書"; 32 line-height: 100px 33 } 34 </style>
35 </head>
36 <body >
37 <div class="main" style="background: url('img/1.jpg')no-repeat;">
38
39 <div class="top">
40 鄭航圖書管理系統 41 </div>
42 <div id="forms" class="mt10">
43 <div class="box">
44 <div class="box_border">
45 <div class="box_top"><b class="pl15">新增圖書</b></div>
46 <div class="box_center">
47 <form action="book/addBook.do" method="post" class="jqtransform">
48 <table class="form_table pt15 pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
49 <tbody>
50 <tr>
51 <td class="td_right">圖書編號:</td>
52 <td class="">
53 <input type="text" name="id" class="input-text lh30" size="50">
54 </td>
55 </tr>
56 <tr>
57 <td class="td_right">圖書名稱:</td>
58 <td>
59 <input type="text" name="name" class="input-text lh30" size="50">
60 </td>
61 </tr>
62 <tr>
63 <td class="td_right">圖書作者:</td>
64 <td>
65 <input type="text" name="author" class="input-text lh30" size="50">
66 </td>
67 </tr>
68 <tr>
69 <td class="td_right">圖書出版社:</td>
70 <td>
71 <input type="text" name="publish" class="input-text lh30" size="50">
72 </td>
73 </tr>
74 <tr>
75 <td class="td_right">圖書價錢:</td>
76 <td>
77 <input type="text" name="price" class="input-text lh30" size="50">
78 </td>
79 </tr>
80 <tr>
81 <td class="td_right">圖書描述:</td>
82 <td class="">
83 <textarea name="des" cols="30" rows="10" class="textarea"></textarea>
84 </td>
85 </tr>
86 <tr>
87 <td class="td_right"> </td>
88 <td class="">
89 <input type="hidden" name="method" value="insertBook">
90 <input id="keep" type="submit" name="button" class="btn btn82 btn_save2" value="保存">
91 <input type="reset" name="reset" class="btn btn82 btn_res" value="重置">
92 </td>
93 </tr>
94 </tbody></table>
95 </form>
96 </div>
97 </div>
98 </div>
99 </div>
100 </div>
101 </body>
102
103
104 </html>
updatebook.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="com.lq.model.*"%>
3
4 <%
5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %>
8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
9 <html>
10 <head>
11 <!-- 設置請求服務器的基准路徑 -->
12 <base href="<%=basePath%>">
13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
14 <title>Insert title here</title>
15 <link rel="stylesheet" href="css/common.css">
16 <link rel="stylesheet" href="css/main.css">
17 <script type="text/javascript" src="js/jquery.min.js"></script>
18 <script type="text/javascript" src="js/colResizable-1.3.min.js"></script>
19 <script type="text/javascript" src="js/common.js"></script>
20 <style type="text/css">
21 .main{ 22 width: 800px; 23 height:780px; 24 margin: 0 auto; 25 background-image: url("img/1.jpg"); 26 } 27 .top{ 28 height:100px; 29 background-color: skyblue; 30 font-size: 90px; 31 font-family: "隸書"; 32 line-height: 100px 33 } 34 </style>
35 </head>
36 <body >
37 <div class="main" style="background: url('img/1.jpg')no-repeat;">
38
39 <div class="top">
40 鄭航圖書管理系統 41 </div>
42 <%
43 Book book=(Book)request.getAttribute("book"); 44 %>
45 <div id="forms" class="mt10">
46 <div class="box">
47 <div class="box_border">
48 <div class="box_top"><b class="pl15">新增圖書</b></div>
49 <div class="box_center">
50 <form action="book/update.do" method="post" class="jqtransform">
51 <table class="form_table pt15 pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
52 <tbody>
53 <tr>
54 <td class="td_right">圖書編號:</td>
55 <td class="">
56 <input type="text" name="id" readonly class="input-text lh30" size="50"value="<%=book.getId()%>">
57 </td>
58 </tr>
59 <tr>
60 <td class="td_right">圖書名稱:</td>
61 <td>
62 <input type="text" name="name" class="input-text lh30" size="50"value="<%=book.getName()%>">
63 </td>
64 </tr>
65 <tr>
66 <td class="td_right">圖書作者:</td>
67 <td>
68 <input type="text" name="author" class="input-text lh30" size="50"value="<%=book.getAuthor()%>">
69 </td>
70 </tr>
71 <tr>
72 <td class="td_right">圖書出版社:</td>
73 <td>
74 <input type="text" name="publish" class="input-text lh30" size="50"value="<%=book.getPublish()%>">
75 </td>
76 </tr>
77 <tr>
78 <td class="td_right">圖書價錢:</td>
79 <td>
80 <input type="text" name="price" class="input-text lh30" size="50"value="<%=book.getPrice()%>">
81 </td>
82 </tr>
83 <tr>
84 <td class="td_right">圖書描述:</td>
85 <td class="">
86 <textarea name="des" cols="30" rows="10" class="textarea"><%=book.getDes()%></textarea>
87 </td>
88 </tr>
89 <tr>
90 <td class="td_right"> </td>
91 <td class="">
92 <input type="hidden" name="method" value="updateBook">
93 <input id="keep" type="submit" name="button" class="btn btn82 btn_save2" value="保存">
94 <input type="reset" name="reset" class="btn btn82 btn_res" value="重置">
95 </td>
96 </tr>
97 </tbody></table>
98 </form>
99 </div>
100 </div>
101 </div>
102 </div>
103 </div>
104 </body>
105
106
107 </html>
cart.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="java.util.*,com.lq.model.*" %>
3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4
5 <%
6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %>
9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 <html>
11 <head>
12 <!-- 設置請求服務器的基准路徑 -->
13 <base href="<%=basePath%>">
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15 <title>Insert title here</title>
16 <link rel="stylesheet" href="css/common.css">
17 <link rel="stylesheet" href="css/main.css">
18 <script type="text/javascript" src="js/jquery.min.js"></script>
19 <script type="text/javascript" src="js/colResizable-1.3.min.js"></script>
20 <script type="text/javascript" src="js/common.js"></script>
21 <style type="text/css">
22 .main{ 23 width: 800px; 24 height:780px; 25 margin: 0 auto; 26 background: url('img/1.jpg')no-repeat; 27 } 28 .top{ 29 height:100px; 30 background-color: skyblue; 31 font-size: 90px; 32 font-family: "隸書"; 33 line-height: 100px ; 34 } 35 </style>
36 </head>
37 <body>
38 <div class="main" >
39
40 <div class="top">
41 鄭航圖書管理系統 42 </div>
43
44 <!-- 列表開始 -->
45 <div id="table" class="mt10">
46 <div class="box span10 oh">
47 <div class="CRC" style="width: 1007px;">
48 <div class="CRG" style="left: 32px; height: 326px;">
49 <div class="grip"></div>
50 <div class="CRZ" style="cursor:e-resize"></div>
51 </div><div class="CRG" style="left: 134px; height: 326px;">
52 <div class="grip"></div>
53 <div class="CRZ" style="cursor:e-resize"></div>
54 </div>
55 <div class="CRG" style="left: 235px; height: 326px;">
56 <div class="grip"></div>
57 <div class="CRZ" style="cursor:e-resize"></div>
58 </div>
59 <div class="CRL" style="left: 1008px; height: 326px;"></div>
60 </div>
61 <table style="text-align: center" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table CRZ" id="CRZ0">
62 <tbody><tr>
63
64 <th style="width: 50%;">圖書編號</th>
65 <th style="width: 50%;">圖書名稱</th>
66 <th style="width: 50%;">圖書作者</th>
67 <th style="width: 50%;">圖書出版社</th>
68 <th style="width: 50%;">圖書價格</th>
69 <th style="width: 50%;">數量</th>
70 <th style="width: 70%;">操作</th>
71 </tr>
72
73 <c:forEach items="${cart }" var="book" begin="0" step="1" varStatus="stat">
74 <tr class="tr" style="background-color: rgb(255, 255, 255);">
75 <td>${book.value.id }</td>
76 <td>${book.value.name }</td>
77 <td>${book.value.author}</td>
78 <td>${book.value.publish}</td>
79 <td> ${book.value.price}</td>
80 <td> ${book.value.count}</td>
81 <td>
82 <a href="book/${book.value.id}/delete.do">刪除</a>
83 </td>
84 </td>
85 </tr>
86 </c:forEach>
87 </tbody></table>
88
89
90 </div>
91 </div>
92 </div>
93 </body>
94
95
96 </html>
所呈現的頁面展示圖
至此,圖書管理系統的增刪改查就完成了,新手不足,還有很多地方有待改善。。。。。