2020.12.10,ssm简单整合(狂神说java)系列

2020.12.10,ssm简单整合

1,ssm的简单整合

1,分层实现

1,mybatis层

​ · 数据库sql语句

​ · 导入项目所需依赖,以及maven静态资源过滤

​ · 创建pojo:写实体类,dao:写数据库调用,controller,service:调用dao层

​ · 创建mybatis-config.xml:mybatis的配置文件,别名和映射注册,

​ applicationContext.xml:,

​ db.properties:写数据库的基本配置,8.0+需要添加一个时区配置

2,spring层

​ · spring-dao.xml:关联数据库配置文件(db.properties),连接池,SqlSessionFactory(SqlSessionFactoryBean),配置dao接口扫描包(动态实现dao接口注入到spring容器中)(MapperScanerConfigurer)

​ · spring-service.xml:扫描包,注入spring容器实现bean注册,声明式事务(配置事务管理器)(DataSourceTransactionManager),aop事务支持(织入)

3,springMVC层

web.xml:DispatcherServlet(前端控制器Servlet,DispatcherServlet),filter(乱码过滤器)(CharacterEncodingFilter)

spring-mvc:注解驱动,静态资源过滤,扫描包,视图解析器(InternalResourceViewResolver)

controller:

4,测试并加入日志

1,测试CRUD是否正常

2,加入日志功能,(标准日志)

5,前端页面

​ 1,首先是首页

​ 2,与controller配合编写

**注意点:**spring的配置文件之间要存在一种关系,applicatonContext.xml

6,新增一个功能

​ 1,首先想到的是aop,切入实现

2,实现过程:

1,pom.xml:导入依赖

​ lombok,junit,mybatis,spring-mybits,servlet-api,jsp-spi,spring-webmvc,jstl,mysql

2,添加web,sql代码

3,xml配置文件

​ 1,spring-dao.xml:实现数据池连接,自动注入

​ 2,spring-service.xml:Bean注入,事物管理

​ 3,spring-mvc.xml:扫描包,静态资源过滤,注解驱动,视图解析器

​ 4,mybatis-config.xml:mapper映射,别名

​ 5,db.properties:数据库优化

​ 6,applicationContext.xml:引用配置文件

​ 7,web.xml:springmvc过滤器,前端控制器

4,java层

​ 1,dao:写mapper及sql

​ 2,pojo:写实体类

​ 3,controller:写控制器

​ 4,service:服务层

5,写前端页面

​ 1,index.jsp:首页

​ 2,jsp包下(WEB-INF)下,安全性

​ · addBook.jsp:增加

​ · allBook:查询所有

​ · update:修改

3,具体代码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rRXvtuSy-1607653291708)(C:\Users\zp\AppData\Roaming\Typora\typora-user-images\image-20201210112820342.png)]

1,首先是sql文件

CREATE DATABASE ssmbuild;
USE ssmbuild;
CREATE TABLE `books`(
`bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT 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,导入所需依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.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>

            <!--Mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</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>

3,dao层

在这里插入图片描述

开始编写实体类pojo,数据库操作层dao层,以及mybatis配置文件的创建

pojo

Books

package com.zp.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    
    
    private Integer bookId;
    private String bookName;
    private int bookCounts;
    private String detail;
}

dao

bookMapper,books的接口

package com.zp.dao;

import com.zp.pojo.Books;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.util.List;

public interface BookMapper {
    
    
    //增
    int insertBook(Books books)throws Exception;
    //删
    int deleteBook(int bookID)throws Exception;
    //改
    int updateBook(Books books)throws Exception;
    //查
    Books selectBookByID(int bookId)throws Exception;
    List<Books> selectBookAll()throws Exception;
    //搜索
    Books queryBookName(@Param("bookName")String bookName)throws Exception;
}

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.zp.dao.BookMapper">
    <insert id="insertBook" 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="selectBookByID" resultType="books">
           select *from ssmbuild.books
          where bookID=#{
    
    bookId}
    </select>
    <select id="selectBookAll" resultType="books">
          select *from ssmbuild.books
    </select>
    <select id="queryBookName" resultType="books">
        select *from ssmbuild.books
        where bookName=#{
    
    bookName}
    </select>
</mapper>

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="spring-service.xml"/>
    <import resource="spring-dao.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

mybatis-config,xml: mybatis的配置文件

<?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.zp.pojo"/>
    </typeAliases>
    <!--mapper注册-->
    <mappers>
        <mapper resource="com/zp/dao/BookMapper.xml"/>
    </mappers>
    
</configuration>

spring-dao.xml: dao层的配置文件

<?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
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--1,关联数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--2,连接池:c3p0-->
    <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.user}"/>
        <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"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--4,dao接口扫描包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.zp.dao"/>
    </bean>
</beans>

db.properties: 数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456

dao层的实现基本到这里就结束了

4,spring-service层的实现

包括service层,以及spring-service.xml配置文件的实现

service:BookService

package com.zp.service;

import com.zp.pojo.Books;

import java.util.List;

public interface BookService {
    
    
    //增
    int insertBook(Books books)throws Exception;
    //删
    int deleteBook(int bookID)throws Exception;
    //改
    int updateBook(Books books)throws Exception;
    //查
    Books selectBookByID(int bookId)throws Exception;
    List<Books> selectBookAll()throws Exception;
    //搜索
    Books queryBookName(String bookName)throws Exception;
}

BookServiceImpl:BookService的实现类

package com.zp.service;

import com.zp.dao.BookMapper;
import com.zp.pojo.Books;

import java.awt.print.Book;
import java.util.List;

public class BookServiceImpl implements BookService {
    
    
    //Service层调用dao层
    private BookMapper bookMapper;

    public int insertBook(Books books) throws Exception {
    
    
        return bookMapper.insertBook(books);
    }

    public int deleteBook(int bookID) throws Exception {
    
    
        return bookMapper.deleteBook(bookID);
    }

    public int updateBook(Books books) throws Exception {
    
    
        return bookMapper.updateBook(books);
    }

    public Books selectBookByID(int bookId) throws Exception {
    
    
        return bookMapper.selectBookByID(bookId);
    }

    public List<Books> selectBookAll() throws Exception {
    
    
        return bookMapper.selectBookAll();
    }

    public Books queryBookName(String queryBookName) throws Exception {
    
    
        return bookMapper.queryBookName(queryBookName);
    }

    public void setBookMapper(BookMapper bookMapper) {
    
    
        this.bookMapper = bookMapper;
    }

}

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: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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!--1,扫描包,service层-->
    <context:component-scan base-package="com.zp.service"/>
    <!--2,注入spring容器-->
    <bean id="bookServiceImpl" class="com.zp.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!--3,配置事务管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

然后spring-service层就结束了

5,spring—mvc:的实现

spring-mvc:需要实现的由控制类,以及前端页面

web.xml:mvc的基本配置文件

<?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,前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--2,乱码过滤器-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</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: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,开启扫描包-->
    <context:component-scan base-package="com.zp.controller"/>
    <!--4,视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

controller:控制层

package com.zp.controller;

import com.zp.pojo.Books;
import com.zp.service.BookService;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    
    
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    //查
    @RequestMapping("/allBook")
    public String selectBookAll(Model model) throws Exception {
    
    
        List<Books> list = bookService.selectBookAll();
        model.addAttribute("list",list);
        return "allBook";
    }
    //增
    @RequestMapping("/toAddBook")
    public String toinsertBook(){
    
    
        return "addBook";
    }
    @RequestMapping("/addBook")
    public String insertBook(Books books) throws Exception {
    
    
        bookService.insertBook(books);
        System.err.println("addBooks---->"+books);
        return "redirect:/book/allBook";
    }
    //改
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook( int Id, Model model) throws Exception {
    
    
        Books books = bookService.selectBookByID(Id);
        System.err.println("toUpdateBook---->"+books);
        model.addAttribute("books",books);
        return "updateBook";
    }
    @RequestMapping("/updateBook")
    public String updateBook(Books books,Model model) throws Exception {
    
    
        bookService.updateBook(books);
        System.err.println("updateBook---->"+books);
        Books books1 = bookService.selectBookByID(books.getBookId());
        model.addAttribute("books1",books1);
        return "redirect:/book/allBook";
    }
    //删除
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) throws Exception {
    
    
        bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }
    //搜索
    @RequestMapping("/queryBookName")
    public String queryBookName(String queryBookName,Model model) throws Exception {
    
    
        Books books = bookService.queryBookName(queryBookName);
        System.err.println("queryBookName------>"+books);
        List<Books> list=new ArrayList<Books>();
        list.add(books);
        if (books==null){
    
    
            list= bookService.selectBookAll();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);

        return "allBook";
    }
}

index.jsp:首页的实现

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
    <style type="text/css">
      a{
        text-decoration: none;
        color: #000;
      }
      h3{
        width: 180px;
        height: 38px;
        line-height: 38px;
        margin: 30px auto;
        background: cyan;
        text-align: center;
      }
    </style>
  </head>
  <body>

  <h3>
   <a href="${pageContext.request.contextPath}/book/allBook"> 点击进入书籍列表</a>
  </h3>
  </body>
</html>

然后是具体跳转页面的实现,是写在WEB-INF下的,在WEB-INF下创建一个jsp,然后页面写在jsp下

addBook.jsp:书籍的添加页面

<%@ 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="http://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/addBook" method="post">
        书籍名称:<input type="text" name="bookName">
        书籍数量:<input type="text" name="bookCounts">
        书籍详情:<input type="text" name="detail">
        <input type="submit" value="提交">
    </form>
</div>
</body>
</html>

allBook.jsp:查询所有书籍

<%@ 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="http://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/addBook" method="post">
        书籍名称:<input type="text" name="bookName">
        书籍数量:<input type="text" name="bookCounts">
        书籍详情:<input type="text" name="detail">
        <input type="submit" value="提交">
    </form>
</div>
</body>
</html>

updateBook.jsp:增加书籍页面

<%@ 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="http://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/addBook" method="post">
        书籍名称:<input type="text" name="bookName">
        书籍数量:<input type="text" name="bookCounts">
        书籍详情:<input type="text" name="detail">
        <input type="submit" value="提交">
    </form>
</div>
</body>
</html>

然后到这里基本就完成了,配置本地comcat就可以运行了

6,最后是排错情况

1, No bean named 'booksServiceImpl' available
思路:
       1,以为是和bean:id不一致--->排错后发现没有解决问题

原因是:没有在applicationContext.xml文件中,注册其他xml资源,注册后即可解决问题


2,Mapped Statements collection already contains value for com.zp.dao.BooksMapper.insertBook.
思路:
       1,直接想到去BooksMapper.xml,也就是sql文件中找错误,
原因:在增删改查方法中的id全是insertBook

3,: Could not open ServletContext resource [/db.properties]
思路:可能是db.properties文件出了问题,但是这个只是数据库的配置文件,怎么可能会有问题于是找和
        这个文件相关的配置,和mybatis有关的只有spring配置文件
原因:,Tomcat部署项目,src/main/resources目录下的配置文件默认位置为:
        {项目名}/WEB-INF/classes,而Spring却在项目根目录下寻找,肯定找不到,
         因此,配置时指定classpath目录下寻找即可。
         解决方案如下:
           <context:property-placeholder location="classpath:db.properties" />
4,找不到方法:class com.zp.pojo.Books.getdetail()
    找不到方法:class com.zp.pojo.Books.getBookID()
思路:去Books下去看看,有没有这个get

原因:点开Books,打开Structure,发现,属性是小写开头,但是get方法中确是大写开头

5,Column 'detail' cannot be null
思路:之前碰到过,应该是sql语句的问题

原因:不是sql语句的原因,其实是在jsp页面中表单name属性的值都写成一样的了
<input type="text" name="bookName" value="${books.getDetail()}"/>

6,修改可以提交,但是数据没有发生修改,无论是前端页面还是数据库本身
思路:事务没有提交,但是增加就可以实现,说明spring事务的自动提交没有出现问题,这里考虑是否加入aop横切

原因:我们修改是根据bookID来的,当跳转到修改页面的时候,我们需要获取一定的数据来作为修改的依据,
       但是我们在前端页面用来获取值的参数和后台controller层用来传递值的参数不一致,也就导致了无法获取
       到,所以也无法提交
注意点:在表单根据bookID获取值和提交值的时候,我们需要把它设置为隐式
esources目录下的配置文件默认位置为:
        {项目名}/WEB-INF/classes,而Spring却在项目根目录下寻找,肯定找不到,
         因此,配置时指定classpath目录下寻找即可。
         解决方案如下:
           <context:property-placeholder location="classpath:db.properties" />
4,找不到方法:class com.zp.pojo.Books.getdetail()
    找不到方法:class com.zp.pojo.Books.getBookID()
思路:去Books下去看看,有没有这个get

原因:点开Books,打开Structure,发现,属性是小写开头,但是get方法中确是大写开头

5,Column 'detail' cannot be null
思路:之前碰到过,应该是sql语句的问题

原因:不是sql语句的原因,其实是在jsp页面中表单name属性的值都写成一样的了
<input type="text" name="bookName" value="${books.getDetail()}"/>

6,修改可以提交,但是数据没有发生修改,无论是前端页面还是数据库本身
思路:事务没有提交,但是增加就可以实现,说明spring事务的自动提交没有出现问题,这里考虑是否加入aop横切

原因:我们修改是根据bookID来的,当跳转到修改页面的时候,我们需要获取一定的数据来作为修改的依据,
       但是我们在前端页面用来获取值的参数和后台controller层用来传递值的参数不一致,也就导致了无法获取
       到,所以也无法提交
注意点:在表单根据bookID获取值和提交值的时候,我们需要把它设置为隐式

猜你喜欢

转载自blog.csdn.net/dearand/article/details/111030121