0


图书管理(SSM)

一:前言

这仅仅是个小demo,体验了一把SSM配置地狱,就简单的实现CRUD

二:相关技术

Mybatis+Spring+SpringMVC+Mysql+Maven+Tomcat

三:功能展示

1:查询

在这里插入图片描述

2:增加

在这里插入图片描述

3:删除

在这里插入图片描述

4:修改

在这里插入图片描述

四:上码

1:导入依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssmbuild</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--Junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!-- 数据库连接池 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!--Servlet- JSP --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--Mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.2</version></dependency><!--Spring--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency></dependencies></project>

2:Maven的资源过滤

zaipom.xml中进行添加

<!--资源过滤--><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include>
                    <include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include>
                    <include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>

3:基本结构框架

在这里插入图片描述

(1):Dao层

a:BookMapper

packagecom.wyj.Dao;importcom.wyj.Pojo.Books;importjava.util.List;publicinterfaceBookMapper{//增加一本书intaddBook(Books books);//根据id删除一个bookintdeleteBook(int id);//更新bookintupdateBook(Books books);//根据id查询 返回一个BookBooksqueryBookById(int id);//查询全部Book 返回list集合List<Books>queryBooks();List<Books>queryBooksById(int id);}

b:BookMapper.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.wyj.Dao.BookMapper"><insert id="addBook" parameterType="books">
        insert into books
        values(#{id},#{bookName},#{bookCounts},#{detail})</insert><delete id="deleteBook" parameterType="_int">
        delete from books
        where id = #{id}</delete><update id="updateBook" parameterType="books">
        update books
        <set><if test="bookName != null">
                bookName = #{bookName},</if><if test="bookCounts != null">
                bookCounts = #{bookCounts},</if><if test="detail != null">
                detail = #{detail},</if></set>
        where id = #{id}</update><select id="queryBookById" parameterType="_int" resultType="books">
        select * from books where id = #{id}</select><select id="queryBooks" resultType="books">
        select * from books
    </select><select id="queryBooksById" parameterType="_int" resultType="books">
        select * from books where id = #{id}</select></mapper>

(2):PoJo

packagecom.wyj.Pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublicclassBooks{privateint id;privateString bookName;privateint bookCounts;privateString detail;}

(3):Service层

业务层 调用dao层完成实际的业务

a:bookService

packagecom.wyj.Service;importcom.wyj.Pojo.Books;importjava.util.List;publicinterfaceBookService{//增加一本书intaddBook(Books books);//根据id删除一个bookintdeleteBook(int id);//更新bookintupdateBook(Books books);//根据id查询 返回一个BookBooksqueryBookById(int id);//查询全部Book 返回list集合List<Books>queryBooks();List<Books>queryBooksById(int id);}

b:bookServiceImpl

packagecom.wyj.Service;importcom.wyj.Dao.BookMapper;importcom.wyj.Pojo.Books;importjava.util.List;publicclassBookServiceImpimplementsBookService{privateBookMapper bookMapper;publicvoidsetBookMapper(BookMapper bookMapper){this.bookMapper = bookMapper;}@OverridepublicintaddBook(Books books){return bookMapper.addBook(books);}@OverridepublicintdeleteBook(int id){return bookMapper.deleteBook(id);}@OverridepublicintupdateBook(Books books){return bookMapper.updateBook(books);}@OverridepublicBooksqueryBookById(int id){return bookMapper.queryBookById(id);}@OverridepublicList<Books>queryBooks(){return bookMapper.queryBooks();}@OverridepublicList<Books>queryBooksById(int id){return bookMapper.queryBooksById(id);}}

(4):Controller层

Servlet在这里填写 响应前端的请求

packagecom.wyj.Controller;importcom.wyj.Pojo.Books;importcom.wyj.Service.BookService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importjava.util.List;@Controller@RequestMapping("/book")publicclassBookController{@Autowired//这个是属性注入当中 自动装配 他仅仅限于如果这个属性是一个类的话@Qualifier("BookServiceImpl")//如果其注册的bean 有多个的话,其可以指定其中的一个beanprivateBookService bookService;@RequestMapping("/allBook")publicStringlistBook(Model model){List<Books> list = bookService.queryBooks();
        model.addAttribute("list",list);return"allBook";}//转发到addBook.jsp这个页面去添加书籍@RequestMapping("/toAddBook")publicStringtoAddPaper(){return"addBook";}//当提交表单后 我们将这个页面重定向到 全部的书籍的页面//这里的参数 books 我们是从表单提交的时候(填写的参数)然后的话我们的提交方式//是post 所以参数看不见 ,但是我们仍可以从url中将参数获取到@RequestMapping("/addBook")publicStringaddBooker(Books books){
        bookService.addBook(books);return"redirect:/book/allBook";}//修改书籍的请求(我们点击修改的操作的时候,在哪一行点击修改 我们就能获取到哪一行的//id信息,即可获取到这本书books;(我们是在url一行中可以获取到 id ,然后这个id参数我们就可以获取到// <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.id}">修改</a>@RequestMapping("/toUpdateBook")publicStringtoUpdateBook(Integer id,Model model){Books books = bookService.queryBookById(id);
        model.addAttribute("Book",books);//我们向updateBook.jsp中的参数Book进行赋值return"updateBook";}//修改表单的提交  提交到这个请求中进行修改@RequestMapping("/updateBook")publicStringupdateBook(Books books,Model model){System.out.println(books);
        bookService.updateBook(books);System.out.println(books);Books books1 = bookService.queryBookById(books.getId());
        model.addAttribute("Book",books);return"redirect:/book/allBook";}//删除一个页面(这个参数id 我们从前端请求的jsp页面中的url 我们可以获取到id)@RequestMapping("/deleteBook")publicStringdeleteBook(Integer id){
        bookService.deleteBook(id);return"redirect:/book/allBook";}//根据书籍编号查询一本书籍的信息@RequestMapping("/CheckBookById")publicStringCheckBook(Integer id,Model model){List<Books> list = bookService.queryBooksById(id);
        model.addAttribute("list",list);return"allBook";}}

4:配置文件

(1):连接数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/smbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=
jdbc.password=

在这里插入图片描述

(2):Mybatis-Config的配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><packagename="com.wyj.Pojo"/></typeAliases>//每写完一个Mapper要在这里进行注册一下<mappers><mapper class="com.wyj.Dao.BookMapper"/></mappers></configuration>

(3):整合spring和mybatis

a:spring-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--配置整合Mybatis--><!--1.关联数据库文件--><context:property-placeholder location="classpath:database.properties"/><!--2.数据库连接池--><bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--配置连接池属性--><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><!--c3p0的私有属性--><property name="maxPoolSize" value="30"/><property name="minPoolSize" value="10"/><!--关闭连接后不自动 commit--><property name="autoCommitOnClose" value="false"/><!-- 获取连接超时时间 --><property name="checkoutTimeout" value="10000"/><!-- 当获取连接失败重试次数 --><property name="acquireRetryAttempts" value="2"/></bean><!--3.配置SqlSessionFactory对象--><bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--注入数据库连接池--><property name="dataSource" ref="dataSource"/><!-- 配置MyBaties全局配置文件:mybatis-config.xml --><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中
        在dao层中我们写完接口和配置文件后 还需要再写一个实现类(其中要手动引入sqlSessionFactory
        或者继承一个sqlSupport)然后在配置文件中注册bean,然后再测试中,CPX  getBean ....
        现在我们可以通过配置文件省略掉这个实现类
     --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--注入sqlSessionFactory--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!--给出要扫描的Dao层接口包--><property name="basePackage" value="com.wyj.Dao"/></bean></beans>

b:spring-mvc.xml

我们用户发起的请求,以及响应我们用户的信息

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--扫描包:使指定的包下的注解有效--><context:component-scan base-package="com.wyj.Controller"/><!--静态资源默认servlet配置--><mvc:default-servlet-handler/><!--开启注解驱动--><mvc:annotation-driven/><!--配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean></beans>

c:spring-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--扫描service层相关的bean--><context:component-scan base-package="com.wyj.Service"/><!--BookServiceImpl注入到IOC容器中--><!--这里的ref = bookMapper ref是参考上下文  我们是将其中几个spring.xml联合在一块--><bean id="BookServiceImpl"class="com.wyj.Service.BookServiceImp"><property name="bookMapper" ref="bookMapper"/></bean><!--配置事务管理器--><bean id ="transaction"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入数据库连接池--><property name="dataSource" ref="dataSource"/></bean></beans>

d:将三个spring.xml整合到一块

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"><importresource="spring-dao.xml"/><importresource="spring-service.xml"/><importresource="spring-mvc.xml"/></beans>

本文转载自: https://blog.csdn.net/qq_48508278/article/details/122755738
版权归原作者 心中无女人 敲码自然神!! 所有, 如有侵权,请联系我们删除。

“图书管理(SSM)”的评论:

还没有评论