码迷,mamicode.com
首页 > 其他好文 > 详细

SSM学习成果总结,图书管理系统进行三层整合

时间:2019-11-13 12:50:17      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:btn   primary   upd   driver   mvc   scan   var   删除   管理系   

一:先看一下效果图

技术图片

 

 

二:目录结构

 

技术图片

 

 

 

 三:pojo

 

 1 package com.zh.pojo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 @Data
 8 @AllArgsConstructor
 9 @NoArgsConstructor
10 public class Books {
11     private  int bookID;
12     private String bookName;
13     private int bookCounts;
14     private String detail;
15 }

 

 

 

四:dao

 

 1 package com.zh.dao;
 2 
 3 import com.zh.pojo.Books;
 4 import java.util.List;
 5 
 6 public interface BookMapper {
 7     void addBooks(Books books);
 8 
 9     List<Books> queryAllBook();
10 
11     Books queryBookById(int id);
12     void updateBooks(Books books);
13     void deleteBooks(int id);
14 
15 }

 

 

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 
 7 <mapper namespace = "com.zh.dao.BookMapper">
 8     <!--增加一个Book-->
 9     <insert id="addBooks" parameterType="Books">
10         insert into ssmbuild.books(bookName,bookCounts,detail)
11         values (#{bookName}, #{bookCounts}, #{detail})
12     </insert>
13 
14     <!--查询全部Book-->
15     <select id="queryAllBook" resultType="books">
16         SELECT * from ssmbuild.books
17     </select>
18 
19     <!--更新Book信息-->
20     <select id="updateBooks" parameterType="Books">
21        update ssmbuild.books
22         set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
23         where bookID = #{bookID}
24     </select>
25     <!--通过Id查询Book信息-->
26     <select id="queryBookById"  resultType="Books">
27         select  * from books where bookID = #{bookID}
28     </select>
29 
30     <!--删除书籍信息-->
31     <delete id="deleteBooks" parameterType="int">
32         delete from books where bookID=#{bookID}
33     </delete>
34 
35 </mapper>

五:service

 

 1 package com.zh.service;
 2 
 3 import com.zh.pojo.Books;
 4 
 5 import java.util.List;
 6 
 7 public interface BookService {
 8     void addBooks(Books books);
 9     List<Books> queryAllBook();
10 
11     Books queryBookById(int id);
12     void updateBooks(Books books);
13 
14     void deleteBooks(int id);
15 }

 

 

 1  @Service("bookServiceImpl")
 2 public class BookServiceImpl implements BookService {
 3 
 4     @Autowired
 5     @Qualifier("bookMapper")
 6     private BookMapper bookMapper;
 7     public void addBooks(Books books) {
 8         bookMapper.addBooks(books);
 9     }
10 
11     public List<Books> queryAllBook() {
12         return bookMapper.queryAllBook();
13     }
14 
15 
16 
17     public Books queryBookById(int id){
18         Books books = bookMapper.queryBookById(id);
19         return books;
20     }
21 
22     public  void updateBooks(Books books){
23          bookMapper.updateBooks(books);
24     }
25 
26     public void deleteBooks(int id) {
27         bookMapper.deleteBooks(id);
28     }
29 
30 }

 

六:controller

 

 

 

 1 @Controller
 2 @RequestMapping("/book")
 3 public class BookController {
 4 
 5         @Autowired
 6         @Qualifier("bookServiceImpl")
 7         private BookService bookService;
 8 
 9         @RequestMapping("/allBook")
10         public String list(Model model) {
11             List<Books> list = bookService.queryAllBook();
12             model.addAttribute("list", list);
13             return "allbook";
14         }
15 
16 
17 
18     @RequestMapping("/toAddBook")
19     public String toAddPaper() {
20         return "addBook";
21     }
22 
23     @RequestMapping("/addBook")
24     public String addPaper(Books books) {
25         System.out.println(books);
26         bookService.addBooks(books);
27         return "redirect:/book/allBook";
28     }
29 
30 
31     @RequestMapping("/toUpdateBooks")
32     public  String toUpdatePaper(Model model ,  int id){
33         Books books = bookService.queryBookById(id);
34         System.out.println(books);
35         model.addAttribute("books", books);
36         return "updateBooks";
37     }
38 
39     @RequestMapping("/updateBooks")
40     public String updatePaper(Model model , Books books){
41         System.out.println("lalala"+books);
42         bookService.updateBooks(books);
43         Books books1 = bookService.queryBookById(books.getBookID());
44         model.addAttribute("book", books1);
45         return "redirect:/book/allBook";
46     }
47 
48 
49     @RequestMapping("/delete/{bookID}")
50     public String deleteBooks(@PathVariable("bookID") int id ){
51             bookService.deleteBooks(id);
52             return "redirect:/book/allBook";
53     }
54 }

 

七:resources

7.1:整合mybatis

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 5 
 6 <!--&lt;!&ndash;通过原始注解的方式需要对接口实现Impl的编写&ndash;&gt;
 7 <context:component-scan base-package="com.zh.dao"></context:component-scan>-->
 8 
 9     <!--配置数据源-->
10     <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
11     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
12         <property name="driverClass" value="${jdbc.driverClass}"/>
13         <property name="user" value="${jdbc.user}"/>
14         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
15         <property name="password" value="${jdbc.password}"/>
16         <!--定义连接池最大的容量-->
17         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
18         <!--定义连接池初始的大小-->
19         <property name="initialPoolSize" value="${jdbc.initPoolSize}"/>
20         <!-- 关闭连接后不自动commit -->
21         <property name="autoCommitOnClose" value="false"/>
22         <!-- 获取连接超时时间 -->
23         <property name="checkoutTimeout" value="10000"/>
24         <!-- 当获取连接失败重试次数 -->
25         <property name="acquireRetryAttempts" value="2"/>
26     </bean>
27 
28     <!--配置sqlSessionFactory对象-->
29     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
30         <property name="dataSource" ref="dataSource"/>
31         <property name="configLocation" value="classpath:mybatis-config.xml"/>
32     </bean>
33 
34     <!--通过这种方式不用写Impl的实现类-->
35     <!--配置扫描Dao的接口包(用这种方式dao层连注解都不用写)-->
36     <!--解释 : https://www.cnblogs.com/jpfss/p/7799806.html-->
37     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38         <!-- 注入sqlSessionFactory -->
39         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
40         <!-- 给出需要扫描Dao接口包 -->
41         <property name="basePackage" value="com.zh.dao"/>
42     </bean>
43 
44 </beans>

 

 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         <package name="com.zh.pojo"></package>
 9     </typeAliases>
10 
11 
12     <mappers>
13         <mapper resource="com/zh/dao/BookMapper.xml"/>
14     </mappers>
15 </configuration>

 

7.2:整合service

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 5 
 6 
 7 
 8     <context:component-scan base-package="com.zh.service"></context:component-scan>
 9    <!-- <bean id="BookServiceImpl" class="com.zh.service.BookServiceImpl">
10         <property name="bookMapper" ref="bookMapper"/>
11     </bean>-->
12 
13     <!-- 配置事务管理器 -->
14     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
15         <!-- 注入数据库连接池 -->
16         <property name="dataSource" ref="dataSource" />
17     </bean>
18 
19 
20 </beans>

 

7.3整合controller

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 5 
 6     <!-- 配置SpringMVC -->
 7     <!-- 1.开启SpringMVC注解驱动 -->
 8     <mvc:annotation-driven />
 9     <!-- 2.静态资源默认servlet配置-->
10     <mvc:default-servlet-handler/>
11 
12     <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
13     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
15         <property name="prefix" value="/WEB-INF/jsp/" />
16         <property name="suffix" value=".jsp" />
17     </bean>
18 
19     <!-- 4.扫描web相关的bean -->
20     <context:component-scan base-package="com.zh.controller" />
21 
22 
23 </beans>

 

八:三层整合

1 <beans xmlns="http://www.springframework.org/schema/beans"
2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
4 
5     <import resource="spring-dao.xml"/>
6     <import resource="spring-service.xml"/>
7     <import resource="spring-mvc.xml"/>
8 
9 </beans>

 

九:jsp部分

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 <html>
 4 <head>
 5     <title>书籍列表</title>
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <!-- 引入 Bootstrap -->
 8     <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 9 </head>
10 <body>
11 
12 <div class="container">
13 
14     <div class="row clearfix">
15         <div class="col-md-12 column">
16             <div class="page-header">
17                 <h1>
18                     <small>书籍列表 —— 显示所有书籍</small>
19                 </h1>
20             </div>
21         </div>
22     </div>
23 
24     <div class="row">
25         <div class="col-md-4 column">
26             <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
27         </div>
28     </div>
29 
30     <div class="row clearfix">
31         <div class="col-md-12 column">
32             <table class="table table-hover table-striped">
33                 <thead>
34                 <tr>
35                     <th>书籍编号</th>
36                     <th>书籍名</th>
37                     <th>数量</th>
38                     <th>书籍介绍</th>
39                     <th>操作</th>
40                 </tr>
41                 </thead>
42 
43                 <tbody>
44                 <c:forEach var="book" items="${requestScope.get(‘list‘)}">
45                     <tr>
46                         <td>${book.getBookID()}</td>
47                         <td>${book.getBookName()}</td>
48                         <td>${book.getBookCounts()}</td>
49                         <td>${book.getDetail()}</td>
50                         <td>
51                             <a href="${pageContext.request.contextPath}/book/toUpdateBooks?id=${book.getBookID()}">更改</a> |
52                             <a href="${pageContext.request.contextPath}/book/delete/${book.getBookID()}">删除</a>
53                         </td>
54                     </tr>
55                 </c:forEach>
56                 </tbody>
57             </table>
58         </div>
59     </div>
60 </div>
 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 3 
 4 <html>
 5 <head>
 6     <title>新增书籍</title>
 7     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 8     <!-- 引入 Bootstrap -->
 9     <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
10 </head>
11 <body>
12 <div class="container">
13 
14     <div class="row clearfix">
15         <div class="col-md-12 column">
16             <div class="page-header">
17                 <h1>
18                     <small>新增书籍</small>
19                 </h1>
20             </div>
21         </div>
22     </div>
23     <form action="${pageContext.request.contextPath}/book/addBook" method="post">
24         书籍名称:<input type="text" name="bookName"><br><br><br>
25         书籍数量:<input type="text" name="bookCounts"><br><br><br>
26         书籍详情:<input type="text" name="detail"><br><br><br>
27         <input type="submit" value="添加">
28     </form>
29 
30 </div>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改信息</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/book/updateBooks" method="post">
        <input type="hidden" name="bookID" value="${books.getBookID()}"/>
        书籍名称:<input type="text" name="bookName" value="${books.getBookName()}"/>
        书籍数量:<input type="text" name="bookCounts" value="${books.getBookCounts()}"/>
        书籍详情:<input type="text" name="detail" value="${books.getDetail() }"/>
        <input type="submit" value="提交"/>
    </form>

</div>

十:web.xml

 

 1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 4          version="4.0">
 5     <servlet>
 6         <servlet-name>DispatcherServlet</servlet-name>
 7         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 8         <init-param>
 9             <param-name>contextConfigLocation</param-name>
10             <param-value>classpath:applicationContext.xml</param-value>
11         </init-param>
12         <load-on-startup>1</load-on-startup>
13     </servlet>
14     <servlet-mapping>
15         <servlet-name>DispatcherServlet</servlet-name>
16         <url-pattern>/</url-pattern>
17     </servlet-mapping>
18 
19     <!--encodingFilter-->
20     <filter>
21         <filter-name>encodingFilter</filter-name>
22         <filter-class>
23             org.springframework.web.filter.CharacterEncodingFilter
24         </filter-class>
25         <init-param>
26             <param-name>encoding</param-name>
27             <param-value>utf-8</param-value>
28         </init-param>
29     </filter>
30     <filter-mapping>
31         <filter-name>encodingFilter</filter-name>
32         <url-pattern>/*</url-pattern>
33     </filter-mapping>
34 
35     <session-config>
36         <session-timeout>15</session-timeout>
37     </session-config>
38 </web-app>

 

 

 

 

SSM学习成果总结,图书管理系统进行三层整合

标签:btn   primary   upd   driver   mvc   scan   var   删除   管理系   

原文地址:https://www.cnblogs.com/zhang188660586/p/11848022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!