SSM框架整合——增删改查图书管理系统

1、创建数据库、表、插入对应的数据

CREATE DATABASE `ssmbuild`;

USE `ssmbuild`;

DROP TABLE IF EXISTS `books`;

CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES 
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

2、导入需要用到得依赖、配置静态资源映射:

<?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.westos</groupId>
    <artifactId>ssmbuild</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖问题:junit、数据库驱动、连接池(C3P0)、servlet、JSP、jstl、mybatis、mybatis-spring、spring-jdbc、springmvc-->
    <dependencies>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</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>
    </dependencies>

    <!--静态资源导出问题-->
    <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>

</project>

3、搭建整个项目框架,并创建部分配置文件:
在这里插入图片描述

  • mybatis-config.xml
<?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>

</configuration>
  • applicationContext.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">

</beans>
  • database.properties
#基本配置
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
username=root
password=123456

4、编写dao层、对应的mapper配置文件:

public interface BooksMapper {
    //增加一本书
    int addBooks(Books book);

    //删除一本书
    int deleteBookById(@Param("bookid") int id);

    //修改一本书
    int updateBookById(Books book);

    //根据ID查询
    Books queryBookById(int id);

    //查询全部书籍
    List<Books> queryAll();
}
<?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="org.westos.dao.BooksMapper">
    <insert id="addBooks" parameterType="Books">
        insert into
            ssmbuild.books(bookID, bookName, bookCounts, detail)
        VALUES(
               #{book.bookid},
               #{book.bookname},
               #{book.bookcounts},
               #{book.detail}
            )
    </insert>

    <delete id="deleteBookById" parameterType="int">
        delete from ssmbuild.books where bookID=#{bookid}
    </delete>

    <update id="updateBookById" parameterType="Books">
        update ssmbuild.books
        set
            bookName = #{bookname},
            bookCounts = #{bookcounts},
            detail = #{detail}
        where
              bookID=#{bookid};
    </update>

    <select id="queryBookById" resultType="Books">
        select * from ssmbuild.books where bookID=#{bookid}
    </select>

    <select id="queryAll" resultType="Books">
        select * from books
    </select>
</mapper>
  • mybatis-context.xml核心配置文件:
<?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>
    <!--配置数据源:交给Spring做-->

    <!--1、忽略大小写配置-->
    <typeAliases>
        <package name="org.westos.pojo"/>
    </typeAliases>

    <!--2、mapper映射文件-->
    <mappers>
        <mapper class="org.westos.dao.BooksMapper"/>
    </mappers>

</configuration>

5、编写service层:

public interface BooksService {
    //增加一本书
    int addBooks(Books book);

    //删除一本书
    int deleteBookById(int id);

    //修改一本书
    int updateBookById(Books book);

    //根据ID查询
    Books queryBookById(int id);

    //查询全部书籍
    List<Books> queryAll();
}
public class BooksServiceImpl implements BooksService {
    private BooksMapper booksMapper;

    public int addBooks(Books book) {
        return booksMapper.addBooks(book);
    }

    public int deleteBookById(int id) {
        return booksMapper.deleteBookById(id);
    }

    public int updateBookById(Books book) {
        return booksMapper.updateBookById(book);
    }

    public Books queryBookById(int id) {
        return booksMapper.queryBookById(id);
    }

    public List<Books> queryAll() {
        return booksMapper.queryAll();
    }

    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }
}

6、整合Spring:

首先是编写spring-dao.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--整合dao层:关联数据库配置文件、连接池、动态扫描mapper,注入SqlSession对象-->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--1、-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--配置连接池属性-->
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!--
            dbcp:半自动化操作,不能自动连接
            c3p0:自动化操作,(可自动加载配置文件,可以自动设置到对象中)
            druid:
            hikari:
        -->

        <!--2、 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、SqlSession-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-context.xml"/>
    </bean>

    <!--4、配置dao接口扫描包:动态地实现了dao接口注入到spring容器中-->
    <!--原来是需要写一个实现类,然后给这个实现类里面注入SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的dao包-->
        <property name="basePackage" value="org.westos.dao"/>
    </bean>

</beans>

分别配置数据源、连接池、SqlSession、配置dao接口扫描,将SqlSession注入进去;
并扫描dao包下的所有类;

其次是编写spring-service.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--1、扫描service下的包-->
    <context:component-scan base-package="org.westos.service"/>

    <!--2、将我们的所有业务类注入到Spring,可以通过配置或者注解实现-->
    <bean id="booksService" class="org.westos.service.BooksServiceImpl">
        <!--这里是因为IDEA帮我们都做了,把这些配置文件都放在同一个上下文中,
        这样就可以检测到被扫描的dao层,也就是dao包下的所有类都交由Spring管理-->
        <property name="booksMapper" ref="booksMapper"/>
    </bean>

    <!--3、声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--4、AOP事务支持-->

</beans>

扫描整个service层;
将serviceImpl交由Spring管理;
配置声明式事务;

这里需要注意的是:如果不采用Ioc配置的方式管理对象,也可以开启注解支持,然后进行包扫描,给各层添加注解直接注入,不需要显式的在配置文件中配置;

7、整合Springmvc层:

首先是保证项目是一个web项目;
配置DispatchServlet、乱码过滤;
最后编写spring-mvc.xml配置文件;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--1、DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--只会过滤请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--2、乱码过滤-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--3、配置Session过期时间-->
    <session-config>
        <!--15分钟-->
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

编写spring-mvc.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--1、注解驱动-->
    <mvc:annotation-driven/>

    <!--2、-静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--3、扫描包-->
    <context:component-scan base-package="org.westos.controller"/>

    <!--4、视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

8、整合所有的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

9、controller层以及前台页面:

首先是写了一个index.jsp页面,让他请求后台的controller,然后编写controller,最后跳转到展示书籍的页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<style>
    a {
        text-decoration: none;
        color: white;
    }

    h3 {
        width: 180px;
        height: 38px;
        margin: 100px auto;
        text-align: center;
        line-height: 38px;
        background: cornflowerblue;
        border-radius: 5px;
    }
</style>
<body>
<h3>
    <a href="${pageContext.request.contextPath}/book/addBook">进入书籍页面</a>
</h3>
</body>
</html>

在这里插入图片描述
controller层代码:

@Controller
@RequestMapping("/book")
public class BooksController {
    //调用service层
    @Autowired
    @Qualifier("booksService")
    private BooksServiceImpl booksService;

    //查询全部的书籍,并且返回到一个书籍展示页面
    @RequestMapping("/addBook")
    public String queryList(Model model) {
        List<Books> list = booksService.queryAll();
        model.addAttribute("list", list);
        return "allBook";
    }
}

使用BootStrap布局整个图书展示页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图书管理系统--书籍展示</title>
    <%--使用BootStrap--%>

</head>

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery文件,务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<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>
</div>

<div class="row clearfix" style="margin: auto">
    <div class="col-md-12 column">
        <table class="table table-hover table-striped">
            <thead>
            <tr>
                <th>书籍编号</th>
                <th>书籍名称</th>
                <th>书籍数量</th>
                <th>书籍详情</th>
            </tr>
            </thead>
            <%--书记从数据库中查询出来,需要从list中遍历出来--%>
            <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookid}</td>
                        <td>${book.bookname}</td>
                        <td>${book.bookcounts}</td>
                        <td>${book.detail}</td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

在这里插入图片描述

至此,整个前后台连贯起来了;

9、做添加书籍的功能:

首先在allBook.jsp页面添加一个按钮,跳转到添加书籍的页面:

<div class="row">
    <div class="col-md-4 column">
        <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
    </div>
</div>

编写添加书籍的跳转的controller:

//跳转到增加书籍页面
@RequestMapping("/toAddBook")
public String toAddPage() {
    return "addBook";
}

编写添加书籍的addBook.jsp页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图书管理系统--书籍添加</title>
    <%--使用BootStrap--%>

</head>

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery文件,务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<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 class="form-horizontal" action="${pageContext.request.contextPath}/book/addBook" method="post">
            <div class="form-group">
                <label for="input1" class="col-sm-2 control-label">书籍名称:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input1" placeholder="name" name="bookname" required>
                </div>
            </div>
            <div class="form-group">
                <label for="input3" class="col-sm-2 control-label">书籍数量:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input3" placeholder="count" name="bookcounts" required>
                </div>
            </div>
            <div class="form-group">
                <label for="input2" class="col-sm-2 control-label">书籍描述:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input2" placeholder="detail" name="detail" required>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="saubmit" class="btn btn-default">提交</button>
                </div>
            </div>
        </form>
    </div>

</body>
</html>

编写添加书籍的controller操作:

//增加书籍页面
@RequestMapping("/addBook")
public String addBook(Books books) {
    //输出日志
    System.out.println("addBook===========>" + books);

    booksService.addBooks(books);

    //重定向到书籍列表页面----请求,实现了请求复用
    return "redirect:/book/allBook";
}

dao层传递数据的时候注意接口中的参数与mapper配置文件中的SQL之间的关系;

至此,增加书籍功能实现;

10、修改书籍

首先是前台跳转到后台的ontroller:

<a href="${pageContext.request.contextPath}/book/toUpdateBook/${book.bookid}">修改</a>

controller层代码:

//跳转到修改书籍页面
//修改为RestFul风格
@RequestMapping(value = "/toUpdateBook/{eleBookId}", method = RequestMethod.GET)
public String toUpdatePage(Model model, @PathVariable("eleBookId") int bid) {
    System.out.println("eleBookId==============>" + bid);

    //调用查询书籍的方法,查询出详细的书籍信息,带回到修改书籍的页面
    Books bookMsg = booksService.queryBookById(bid);

    model.addAttribute("bookMsg", bookMsg);
    return "updateBook";
}

带着查询出来的书籍信息,跳转到前台页面,给他一个回显的效果;

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图书管理系统--书籍添加</title>
    <%--使用BootStrap--%>

</head>

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery文件,务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<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 class="form-horizontal" action="${pageContext.request.contextPath}/book/updateBook" method="post">
            <input type="hidden" name="bookid" value="${bookMsg.bookid}">
            <div class="form-group">
                <label for="input1" class="col-sm-2 control-label">书籍名称:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input1" placeholder="name" name="bookname" required value="${bookMsg.bookname}">
                </div>
            </div>
            <div class="form-group">
                <label for="input3" class="col-sm-2 control-label">书籍数量:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input3" placeholder="count" name="bookcounts" required value="${bookMsg.bookcounts}">
                </div>
            </div>
            <div class="form-group">
                <label for="input2" class="col-sm-2 control-label">书籍描述:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" style="width: 300px" id="input2" placeholder="detail" name="detail" required value="${bookMsg.detail}">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="saubmit" class="btn btn-default">提交</button>
                </div>
            </div>
        </form>
    </div>

</body>
</html>

修改之后点击提交按钮,跳转到后台,进行修改书籍;

//修改书籍页面
@RequestMapping("/updateBook")
public String updateBook(Books books) {
    //输出日志
    System.out.println("updateBook===========>" + books);
    booksService.updateBookById(books);

    //重定向到书籍列表页面----请求,实现了请求复用
    return "redirect:/book/allBook";
}

最后跳转到查询所有的页面;

11、删除书籍:

<a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookid}">删除</a>

删除书籍的controller:

//删除书籍页面
@RequestMapping("/deleteBook/{bid}")
public String deleteBook(@PathVariable int bid) {
    System.out.println("deleteBook===========>" + bid);

    //删除书籍业务,返回到查询所有书籍
    booksService.deleteBookById(bid);

    //重定向到书籍列表页面----请求,实现了请求复用
    return "redirect:/book/allBook";
}

最后跳转到查询所有的页面;

12、新增搜索功能;

<div class="col-md-4 column" style="float:right;">
    <form class="form-inline" action="/book/queryBook" method="post">
        <div class="form-group" style="margin-right: 5%">
            <input type="text" class="form-control" placeholder="请输入要查询的书籍名称" name="queryBookName">
        </div>
        <button type="submit" class="btn btn-primary">查询</button>
    </form>
</div>
//查询书籍
@RequestMapping(value = "/queryBook", method = RequestMethod.POST)
public String queryBook(@RequestParam("queryBookName") String queryBookName, Model model) {
    //需要有一个业务去处理
    List<Books> booksList = booksService.queryBookByName(queryBookName);
    model.addAttribute("list", booksList);
    return "allBook";
}
  • 6
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SSM框架是指Spring + SpringMVC + MyBatis的组合,它是一种常用的Java Web开发框架。下面是SSM框架中的增删改查的介绍: 1. 增加(Create):在SSM框架中,可以通过MyBatis的Mapper接口定义插入数据的方法,并在对应的Mapper XML文件中编写SQL语句实现数据的插入操作。在SpringMVC中,可以通过Controller接收前端传递的数据,并调用Service层的方法将数据传递给Mapper进行插入操作。 2. 查询(Retrieve):在SSM框架中,可以通过MyBatis的Mapper接口定义查询数据的方法,并在对应的Mapper XML文件中编写SQL语句实现数据的查询操作。在SpringMVC中,可以通过Controller接收前端传递的查询条件,并调用Service层的方法将查询条件传递给Mapper进行查询操作,最后将查询结果返回给前端。 3. 更新(Update):在SSM框架中,可以通过MyBatis的Mapper接口定义更新数据的方法,并在对应的Mapper XML文件中编写SQL语句实现数据的更新操作。在SpringMVC中,可以通过Controller接收前端传递的更新数据,并调用Service层的方法将更新数据传递给Mapper进行更新操作。 4. 删除(Delete):在SSM框架中,可以通过MyBatis的Mapper接口定义删除数据的方法,并在对应的Mapper XML文件中编写SQL语句实现数据的删除操作。在SpringMVC中,可以通过Controller接收前端传递的删除条件,并调用Service层的方法将删除条件传递给Mapper进行删除操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值