学完尚硅谷ssm后,看了狂神的整合。自己的整合记录
目录
1.项目构建时 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet的问题
环境配置
-
IDEA
-
MySQL 8.0.28
-
Tomcat 8.0.50
-
Maven 3
数据库导入
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,'从进门到进牢');
项目创建
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>com.yan</groupId>
<artifactId>ssmbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<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.28</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</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>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</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>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- 控制Maven在构建过程中相关配置 -->
<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>
<!-- 构建过程中用到的插件 -->
<plugins>
<!--打包时跳过test-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--打包-->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<!-- 插件的依赖 -->
<dependencies>
<!-- 逆向工程的核心依赖 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
2.mvc架构配置
<?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">
<import resource="spring-service.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
3.Dao层配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=root
jdbc.password=
<?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>
<!--引入jdbc配置文件-->
<!-- <properties resource="jdbc.properties"/>-->
<typeAliases>
<package name="com.yan.pojo"/>
</typeAliases>
<!-- <!–设置连接数据库的环境–>-->
<!-- <environments default="development">-->
<!-- <environment id="development">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="${jdbc.driver}"/>-->
<!-- <property name="url"-->
<!-- value="${jdbc.url}"/>-->
<!-- <property name="username" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<!--引入映射文件-->
<mappers>
<package name="com.yan.dao"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false"
userId="root"
password="324628">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.yan.pojo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.yan.dao"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yan.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="books" domainObjectName="Books"/>
</context>
</generatorConfiguration>
package com.yan.pojo;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private Integer bookid;
private String bookname;
private Integer bookcounts;
private String detail;
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname == null ? null : bookname.trim();
}
public Integer getBookcounts() {
return bookcounts;
}
public void setBookcounts(Integer bookcounts) {
this.bookcounts = bookcounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
@Override
public String toString() {
return "Books{" +
"bookid=" + bookid +
", bookname='" + bookname + '\'' +
", bookcounts=" + bookcounts +
", detail='" + detail + '\'' +
'}';
}
}
public interface BooksMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table books
*
* @mbggenerated Sat Mar 26 16:19:03 CST 2022
*/
int countByExample(BooksExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table books
*
* @mbggenerated Sat Mar 26 16:19:03 CST 2022
*/
int deleteByExample(BooksExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table books
*
* @mbggenerated Sat Mar 26 16:19:03 CST 2022
*/
int deleteByPrimaryKey(Integer bookid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table books
*
* @mbggenerated Sat Mar 26 16:19:03 CST 2022
*/
int insert(Books record);
}
<?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="com.yan.dao.BooksMapper" >
<resultMap id="BaseResultMap" type="com.yan.pojo.Books" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 26 16:19:03 CST 2022.
-->
<id column="bookID" property="bookid" jdbcType="INTEGER" />
<result column="bookName" property="bookname" jdbcType="VARCHAR" />
<result column="bookCounts" property="bookcounts" jdbcType="INTEGER" />
<result column="detail" property="detail" jdbcType="VARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 26 16:19:03 CST 2022.
-->
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
</mapper>
4.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.yan.service"/>
<!--创建事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入连接dateSource属性,注入数据源-->
<property name="dataSource" ref="druidDataSource"/>
</bean>
<!--引入名称空间-->
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!--BookServiceImpl注入到IOC容器中-->
<!-- <bean class="com.yan.service.BookServiceImpl">-->
<!-- <property name="booksMapper" ref="booksMapper"></property>-->
<!-- </bean>-->
</beans>
package com.yan.service;
import com.yan.pojo.Books;
import java.util.List;
/**
* @author Yyy
* @creat2022-03-2619:54
*/
public interface BookService {
/**
* 删除书籍信息
*/
int deleteBook(Integer id);
/**
* 增加书籍信息
*/
int addBook(Books book);
/**
* 更新书籍信息
*/
int updateBook(Books book);
/**
*根据id查询书籍
*/
Books getBookById(Integer id);
/**
* 查询所有书籍信息
*/
List<Books> getAllBooks();
/***
* 根据bookName查询
*/
List<Books> getBookByName(String bookName);
}
package com.yan.service;
import com.yan.dao.BooksMapper;
import com.yan.pojo.Books;
import com.yan.pojo.BooksExample;
import lombok.Data;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author Yyy
* @creat2022-03-2619:57
*/
//开启事务
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BooksMapper booksMapper;
@Override
public int deleteBook(Integer id) {
return booksMapper.deleteByPrimaryKey(id);
}
@Override
public int addBook(Books book) {
return booksMapper.insert(book);
}
@Override
public int updateBook(Books book) {
return booksMapper.updateByPrimaryKeySelective(book);
}
@Override
public Books getBookById(Integer id) {
return booksMapper.selectByPrimaryKey(id);
}
@Override
public List<Books> getAllBooks() {
return booksMapper.selectByExample(null);
}
@Override
public List<Books> getBookByName(String bookName) {
BooksExample booksExample = new BooksExample();
booksExample.createCriteria().andBooknameEqualTo(bookName);
return booksMapper.selectByExample(booksExample);
}
public BooksMapper getBooksMapper() {
return booksMapper;
}
public void setBooksMapper(BooksMapper booksMapper) {
this.booksMapper = booksMapper;
}
}
5.Spring整合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>
<!--引入jdbc配置文件-->
<!-- <properties resource="jdbc.properties"/>-->
<typeAliases>
<package name="com.yan.pojo"/>
</typeAliases>
<!-- <!–设置连接数据库的环境–>-->
<!-- <environments default="development">-->
<!-- <environment id="development">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="${jdbc.driver}"/>-->
<!-- <property name="url"-->
<!-- value="${jdbc.url}"/>-->
<!-- <property name="username" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<!--引入映射文件-->
<mappers>
<package name="com.yan.dao"/>
</mappers>
</configuration>
<?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">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--开启注解扫描-->
<context:component-scan base-package="com.yan.dao"/>
<!--2.数据库链接池-->
<!-- 数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<!-- <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<!-- <!–配置连接池属性–>-->
<!-- <property name="driverClass" value="${jdbc.driver}"/>-->
<!-- <property name="user" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- <property name="jdbcUrl" value="${jdbc.url}"/>-->
<!-- <!– 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>-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--配置连接池属性-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<!--3.配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.yan.dao"/>
</bean>
</beans>
6.controller层配置
<?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>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>
<!-- encodingFilter-->
<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>
</web-app>
<?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/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">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!--注解扫描-->
<context:component-scan base-package="com.yan.controller"/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<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>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.yan.controller" />
<mvc:view-controller path="/" view-name="index"/>
</beans>
package com.yan.controller;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.yan.pojo.Books;
import com.yan.service.BookServiceImpl;
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.List;
/**
* @author Yyy
* @creat2022-03-2820:04
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
BookServiceImpl bookService;
@RequestMapping("/toAddBook")
public String toAddBook(){
return "addBook";
}
@RequestMapping("/toUpdateBook/{id}")
public String toUpdateBook(Model model ,@PathVariable("id") Integer id){
Books bookById = bookService.getBookById(id);
model.addAttribute("book",bookById);
return "updateBook";
}
@RequestMapping("/getAllBooks")
public String getAllBooks(Model model){
List<Books> allBooks = bookService.getAllBooks();
model.addAttribute("books",allBooks);
return "showBooks";
}
@RequestMapping("/addBook")
public String addBook(Model model,Books book){
bookService.addBook(book);
List<Books> allBooks = bookService.getAllBooks();
model.addAttribute("books",allBooks);
return "showBooks";
}
@RequestMapping("/updateBook")
public String updateBook(Model model,Books book){
bookService.updateBook(book);
List<Books> allBooks = bookService.getAllBooks();
model.addAttribute("books",allBooks);
return "showBooks";
}
@RequestMapping("/deleteBook")
public String deleteBook(Model model,Integer id){
bookService.deleteBook(id);
List<Books> allBooks = bookService.getAllBooks();
model.addAttribute("books",allBooks);
return "showBooks";
}
@RequestMapping("/queryBooks")
public String queryBooks(Model model,String bookName){
List<Books> bookByName = bookService.getBookByName(bookName);
model.addAttribute("books",bookByName);
return "showBooks";
}
}
7.视图层编写
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/getAllBooks">点击进入列表页</a>
</h3>
</body>
</html>
<%@ 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>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</div>
<div class="col-md-4 column" style="text-align: center">
<form action="${pageContext.request.contextPath}/book/getAllBooks" method="post">
<input type="submit" value="查询所有数据信息" class="btn btn-primary">
</form>
</div>
<div class="col-md-4 column">
<form action="${pageContext.request.contextPath}/book/queryBooks" method="post" style="float: right">
<input type="text" name="bookName" class="form-control" placeholder="请输入要查询的书籍名称">
<input type="submit" value="查询" class="btn btn-primary">
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${requestScope.books}">
<tr>
<td>${book.getBookid()}</td>
<td>${book.getBookname()}</td>
<td>${book.getBookcounts()}</td>
<td>${book.getDetail()}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook/${book.getBookid()}">更改</a> |
<a href="${pageContext.request.contextPath}/book/deleteBook?id=${book.getBookid()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</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/addBook" method="post">
书籍名称:<input type="text" name="bookname"><br><br><br>
书籍数量:<input type="text" name="bookcounts"><br><br><br>
书籍详情:<input type="text" name="detail"><br><br><br>
<input type="submit" value="添加">
</form>
</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/updateBook" method="post">
<input type="hidden" name="bookid" value="${book.getBookid()}"/>
书籍名称:<input type="text" name="bookname" value="${book.getBookname()}"/>
书籍数量:<input type="text" name="bookcounts" value="${book.getBookcounts()}"/>
书籍详情:<input type="text" name="detail" value="${book.getDetail() }"/>
<input type="submit" value="提交"/>
</form>
</div>
至此,此ssm整合结束!
8.整合中发现的问题
web项目构建时 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet的问题_YanDDDeat的博客-CSDN博客
关于ssm整合时关联Spring配置文件的问题_YanDDDeat的博客-CSDN博客