ssm-demo整合

ssm-demo整合

完成书库的增删改查,书名模糊查询

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.配置maven依赖

处理依赖和静态资源导出问题

<?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">
    <parent>
        <artifactId>test_mybatis</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test_ssm_all</artifactId>

    <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.11</version>
        </dependency>
        <!--mysql数据库连接工具jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!--连接池-->

        <!--servlet和jsp的依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</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.7</version>
        </dependency>

        <!-- spring-webmvc的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!-- spring操作数据库的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!--        lombok工具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <!--    静态资源导出问题-->
    <!--    资源导出失败的过滤设置-于四.2中发现错误:资源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>

</project>

3.建立项目结构

java文件下

com.kuang.

  • dao

    • BooksMapper.java(接口)

    • BooksMapper.xml(sql语句)

  • pojo

    • books.java(实体类)
  • controller

    • BooksController.java(与前端交互,调用sevice)
  • service

    • BooksService.java(接口)
    • BooksServiceImpl.java(通过sqlsession调用dao层)

resource目录下

  • maybatis

    • db.properties(保存数据库连接信息,用户名密码
    • mybatis-config.xml(由于spring-mybatis托管mybatis,该文件用处不大,用来取别名)
    • sping-mybatis.xml(托管mybatis的数据库连接,SqlsessionFactory,)
  • service

    • spring-service.xml(注册service,注入dao层的Mapper到service,事务支持)
  • springMVC

    • springMVC-config.xml(mvc的注解驱动,静态资源过滤,注册controller,视图解析器)
  • applicationContext.xml

web目录下

  • WEB-INF
    • jsp
      • addBook.jsp
      • allBook.jsp
      • updateBook.jsp
  • index.jsp

mybatis层

4.编写实体类:books

books.java

package com.kuang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

5.编写实体类的操作接口:BooksMapper

package com.kuang.dao;

import com.kuang.pojo.Books;

import java.util.List;

public interface BooksMapper {
    //增删改查
    public int addBook(Books books);
    public int deleteBook(int bookID);
    public int updateBook(Books books);
    public List<Books> selectAllBook();
    public Books selectBook(int bookID);
    public List<Books> selectBookByName(String bookName);
}

6.编写操作接口的实现BooksMapper.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.kuang.dao.BooksMapper">
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.books (bookName,bookCounts,detail)
        values (#{bookName},#{bookCounts},#{detail});
    </insert>

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

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

    <select id="selectAllBook" resultType="Books">
        select *
        from ssmbuild.books;
    </select>

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

    <select id="selectBookByName" resultType="Books">
        select *
        from ssmbuild.books
        where bookName like #{bookName};
    </select>
</mapper>

7.配置mybatis-config.xml

使实体类包下的类都有别名,无需指定路径

配置Mapper,注册接口

<?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>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>
</configuration>

8.编写sevice的接口:BooksService

与dao接口几乎一致

package com.kuang.service;

import com.kuang.pojo.Books;

import java.util.List;

public interface BooksService {
    //和数据库操作一致
    //增删改查
    public int addBook(Books books);
    public int deleteBook(int bookID);
    public int updateBook(Books books);
    public List<Books> selectAllBook();
    public Books selectBook(int bookID);
    public List<Books> selectBookByName(String bookName);
}

9.实现该接口:BooksServiceImpl

通过获取dao的mapper来实现接口,获取方式:写set方式,然后在spring中注册,注入所需的mapper

package com.kuang.service;

import com.kuang.dao.BooksMapper;
import com.kuang.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//在这个地方可以使用aop,进行事务注入和日志等功能

public class BooksServiceImpl implements BooksService{

    //业务层调用dao
    private BooksMapper booksMapper;
    //设置set方法,使用spring注入
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public int addBook(Books books) {
        return booksMapper.addBook(books);
    }

    @Override
    public int deleteBook(int bookID) {
        return booksMapper.deleteBook(bookID);
    }

    @Override
    public int updateBook(Books books) {
        return booksMapper.updateBook(books);
    }

    @Override
    public List<Books> selectAllBook() {
        return booksMapper.selectAllBook();
    }

    @Override
    public Books selectBook(int bookID) {
        return booksMapper.selectBook(bookID);
    }

    @Override
    public List<Books> selectBookByName(String bookName) {
        return booksMapper.selectBookByName(bookName);
    }
}

mybatis层结束

spring层

10.准备数据库连接da.properties

driver = com.mysql.jdbc.Driver
#mysql8.0要加时区   最后加上&serverTimezone=Asia/Shanghai   (亚洲上海)
url = jdbc:mysql://localhost:3306?ssmbuild/useSSL=true&useUnicode=true&characterEncoding=UTF-8
user = root
password =123456

11.编写spring-mybatis文件

接管mybatis

  1. 关联数据库配置文件
  2. 连接池--之前使用的是spring自带的连接池
  3. sqlSessionFactory
<?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: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">

<!--    进行spring-mybatis配置-->
<!--    1.关联数据库配置-->
<!--    使用contex导入数据库连接文件-->
    <context:property-placeholder location="classpath:mybatis/db.properties"/>
<!--    2.数据库连接池:
        dbcp:半自动化操作
        c3p0:自动化操作(自动化加载配置文件,自动设置到对象中)=====现在使用这个连接池
        druid:
        hikari:springboot2.0默认使用
-->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>

        <!-- c3pe连接池的私有属性部分-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="1"/>
        <!--关闭连接后不自动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"/>
<!--        绑定mybatis-config.xml===使spring和mybatis整合起来-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    </bean>

<!--    配置dao接口扫描包,动态的实现了Dao接口可以注入到spring容器中:
        由于mapper的调用要使用sqlSession来进行操作,所以写一个Mapper接口实现类,
        两种方式,
        1.使用sqlSessionTemplate来实现,需要将sqlSessionFactory注入到sqlSessionTemplate,
        再将sqlSessionTemplate注入到这个实现类中,sqlSessionTemplate和实现类都注册到spring容器
        2.使用SqlSessionDaoSupport实现,实现类继承SqlSessionDaoSupport类并实现接口,
        将实现类注册到spring容器,并将sqlSessionFactory注入,
        内部直接使用getSqlSession().getMapper(UserMapper.class)执行函数
-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--     注入sqlSessionFactory,填入sqlSessionFactory的bean的id===对应方式二,只需要注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--        指定要扫描的包-->
        <property name="basePackage" value="com.kuang.dao"/>
    </bean>
</beans>

12.编写spring-service文件

<?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: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">

<!--    扫描service下的包,使注解生效-->
    <context:component-scan base-package="com.kuang.service"/>
<!--将所有业务类注册到spring容器,可以通过配置或者注解实现-->
    <bean id="BooksServiceImpl" class="com.kuang.service.BooksServiceImpl">
<!--        将内部对象注入:booksMapper在mybatis-config中注册了,而mybatis-config交给spring托管了-->
        <property name="booksMapper" ref="booksMapper"/>
    </bean>

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

<!--    aop事务支持-->
</beans>

springMVC层

13.增加web支持

右键->add Framework support->web

14.配置web.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">

<!--    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>

<!--    乱码过滤-->
<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>

<!--    session超时设置-->
<session-config>
    <session-timeout>15</session-timeout>
</session-config>
</web-app>

15.配置springMVC-config.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: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/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">
<!--    1.注解驱动-->
    <mvc:annotation-driven/>
<!--    2.静态资源过滤-->
    <mvc:default-servlet-handler/>
<!--    3.扫描包:controller-->
    <context:component-scan base-package="com.kuang.controller"/>
<!--    4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

16.配置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">
    <import resource="classpath:mybatis/spring-mybatis.xml"/>
    <import resource="classpath:service/spring-service.xml"/>
    <import resource="classpath:springMVC/springMVC-config.xml"/>
</beans>

17.编写BooksController

package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BooksController {
    //controller调用service

    private BooksService booksService;

    @Autowired
    @Qualifier("BooksServiceImpl")//指定实现类
    public void setBooksService(BooksService booksService) {
        this.booksService = booksService;
    }

    //查询全部书
    @RequestMapping("/queryAllBooks")
    public String queryAllBooks(Model model){
        List<Books> booksList= booksService.selectAllBook();
        model.addAttribute("booksList",booksList);
        return "allBooks";
    }

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

    //添加书籍
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("add:"+books);
        booksService.addBook(books);
        return "redirect:/book/queryAllBooks";//重定向到查询全部
    }

    //跳转到修改页面--传入一本书的数据
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int bookID,Model model){
        Books books=booksService.selectBook(bookID);
        System.out.println("toUpdateBook:"+books);
        model.addAttribute("book",books);
        return "updateBook";
    }

    //修改
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.out.println("update:"+books);
        booksService.updateBook(books);
        return "redirect:/book/queryAllBooks";
    }

    //删除
    @RequestMapping("deleteBook")
    public String deleteBook(int bookID){
        System.out.println("delete:"+bookID);
        booksService.deleteBook(bookID);
        return "redirect:/book/queryAllBooks";
    }

    //查询功能
    @RequestMapping("/queryBook")
    public String queryBook(String bookName,Model model){
       List<Books> booksList = booksService.selectBookByName("%"+bookName+"%");
       model.addAttribute("booksList",booksList);
        return "allBooks";
    }
}

前端

18.index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/20
  Time: 22:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
    <style>
      a{
        text-decoration:none;
        color: black;
        font-size: 18px;
      }
      h3{
        width: 200px;
        height: 40px;
        margin:100px auto;
        text-align: center;
        line-height: 40px;
        background:burlywood;
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
  <h3>
    <a href="/book/queryAllBooks">进入书籍页面</a>
  </h3>
  </body>
</html>

19.allBook.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 核心 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>
</head>
<body>

<div class="container">
    <div class="row clearfix">
<%--clear清除浮动--%>
        <div class="col-md-12">
<%--            把屏幕分成中等的12份--%>
            <div class="page-header">
<%--                页面头--%>
                <h1>
                    <small>书籍列表</small>
                </h1>
            </div>
            <div class="row">
                <div class="col-md-4">
<%--                    跳转到添加数据页面--%>
                    <a class="btn btn-primary" href="/book/toAddBook">添加书籍</a>
                </div>
                <div class="col-md-4">
                    <form action="/book/queryBook" method="get" style="float: right" class="form-inline">
                        <input type="text" placeholder="请输入要查询的书的名称" name="bookName" class="form-control">
                        <input type="submit" class="btn btn-primary" value="查询">
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12">
            <table class="table table-hover table-striped">
<%--                table-hover隔行变色,table-striped有边框--%>
                <thead>
                <tr>
                    <th>书籍编号</th>
                    <th>书籍名称</th>
                    <th>书籍数量</th>
                    <th>书籍详情</th>
                    <th>操作</th>
                </tr>
                </thead>
<%--                数据库中查询出数据,从list遍历出来:forEach--%>
                <tbody>
                <c:forEach var="book" items="${booksList}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.detail}</td>
                        <td>
                            <a href="/book/toUpdateBook?bookID=${book.bookID}">修改</a>
                            &nbsp|&nbsp
                            <a href="/book/deleteBook?bookID=${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>

            </table>
        </div>
    </div>
</div>

</body>
</html>

20.addBook.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/21
  Time: 9:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加书籍</title>
    <!-- 新 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>
</head>
<body>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12">
            <div class="page-header">
                <h1>
                    <small>书籍列表</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="/book/addBook" method="post">
        <div class="form-group">
            <label for="bookName">书籍名字</label>
            <input type="text" name="bookName" class="form-control" id="bookName" required>
        </div>
        <div class="form-group">
            <label for="bookCount">书籍数量</label>
            <input type="number" name="bookCounts" class="form-control" id="bookCount" required>
        </div>
        <div class="form-group">
            <label for="detail">书籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" required>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
</div>


</body>
</html>

21.updateBook.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/21
  Time: 9:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>更新书籍</title>
    <!-- 新 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>
</head>
<body>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12">
            <div class="page-header">
                <h1>
                    <small>书籍列表</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="/book/updateBook" method="post">
        <div class="form-group">
            <label for="bookName">书籍名字</label>
            <input type="text" name="bookName" class="form-control" id="bookName" value="${book.bookName}" required>
        </div>
        <div class="form-group">
            <label for="bookCount">书籍数量</label>
            <input type="number" name="bookCounts" class="form-control" id="bookCount" value="${book.bookCounts}" required>
        </div>
        <div class="form-group">
            <label for="detail">书籍描述</label>
            <input type="text" name="detail" class="form-control" id="detail" value="${book.detail}" required>
        </div>
        <input type="hidden" name="bookID" value="${book.bookID}">
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
</div>


</body>
</html>

错误记录

1.Mapper注册问题:sqlSessionFactory创建出现问题。

原因:spring-mybatis中使用MapperScannerConfigurer扫描了dao下的包,然而在mybatis-config中还注册了Mapper,重复注册导致扫描问题。

解决:删除mybatis中的Mapper注册

2.数据库连接失败-c3p0连接失败次数超过30次:

原因:db.properties文件中配置username为关键字

解决:使用usr替换

3.无法调用到Serevice的是实现类

原因:web.xml配置的初始化文件是springmvc-config.xml,这个只有一部分的内容,无法使用service层的bean

解决:web.xml中dispatcherServlet初始化配置中导入applicationContex.xml这个总的文件

4.无法找到jspallBooks.jsp

原因:视图解析器前缀少写/

解决:加上/

出现无法找到jsp/allBooks.jsp

原因:没建jsp文件夹

解决:建一个jsp文件夹,将allBooks.jsp放入

5.发现datail的错误:mybatis没找到实体类有datail属性

原因:实体类为detail,mappper.xml的sql语句中写错了

解决:改正

posted @ 2021-07-21 16:13  世界时停  阅读(108)  评论(0)    收藏  举报