Spring、SpringMVC、Mybatis框架整合(IDEA)(附Demo)

在上一篇博客 Spring框架与Mybatis框架整合(IDEA) 演示了Spring、Mybatis框架的整合,最近有搞一下SpringMVC,所以此篇博客把传说中的SSM整合一下,以防日后忘记了步骤,不过,无非是一些导jar包、添加配置文件的板砖操作。。。

S p r i n g 4.2 S p r i n g M V C 4.2 M y b a t i s 3.5.3 \color{orange}Spring4.2、SpringMVC4.2、Mybatis3.5.3整合

1、新建一个普通的Java Web项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、导入相关依赖

①、在web/WEB-INF目录下新建lib文件夹

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

②、导入SpringSpringMVC相关依赖

Spring框架支持包下载地址:https://repo.spring.io/release/org/springframework/spring/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

\color{red}注意: 还需要导入spring-expression.jar,这里写漏了

在这里插入图片描述

③、导入Mybatis依赖

Mybatis下载地址:https://github.com/mybatis/mybatis-3/releases
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

④、复制Mybatis-spring连接包到lib文件夹

maven下载地址:https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/2.0.3
在这里插入图片描述
在这里插入图片描述

⑤、导入Spring AOP联盟+织入依赖

在这里插入图片描述

⑥、导入C3P0数据库连接池依赖

C3P0下载地址:https://www.mvnjar.com/com.mchange/c3p0/0.9.5.5/detail.html
在这里插入图片描述
mchange-commons-java下载地址:https://www.mvnjar.com/com.mchange/mchange-commons-java/0.2.16/detail.html
在这里插入图片描述
在这里插入图片描述

⑦、导入MySQL驱动

MySQL官网下载地址:https://dev.mysql.com/downloads/connector/j/
在这里插入图片描述
在这里插入图片描述

⑧、导入jstl依赖

在这里插入图片描述

⑨、导入junit测试依赖

在这里插入图片描述

⑩、将lib文件夹设为项目Library

在这里插入图片描述
在这里插入图片描述

⑪、检查Project Structure是有错误

在这里插入图片描述

3、创建数据库、表

脚本文件:

# 创建ssm_demo_01数据库
DROP DATABASE IF EXISTS `ssm_demo_01`;
CREATE DATABASE `ssm_demo_01`;
USE `ssm_demo_01`;
# 创建t_product表
CREATE TABLE `t_product`(
	`id` int PRIMARY KEY auto_increment,
	`name` VARCHAR(32) COMMENT '产品名称',
	`price` double COMMENT '产品价格',
	`number` int COMMENT '库存数量',
	`info` VARCHAR(200) COMMENT '产品简介',
	`picture_path` VARCHAR(100) COMMENT '产品照片路径',
	`created_time` date COMMENT '产品创建时间'
);
# 插入几条测试数据
INSERT INTO `t_product`(`name`,`price`,`number`,`info`,`picture_path`,`created_time`) 
VALUES('电脑','5000.5','50','性能吊打苹果!',NULL, now()),('手机','2000.9','20','性价比第一!',NULL, now()),('平板','32000.9','40','性能宇宙第一!',NULL, now());

在这里插入图片描述

4、添加配置文件

①、在项目根目录创建config文件夹

在这里插入图片描述
在这里插入图片描述
然后将config文件夹设置为Resource目录
在这里插入图片描述

②、添加springmvc.xmlconfig文件夹

在这里插入图片描述

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.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.配置注解扫描位置 -->
    <context:component-scan base-package="cn.hestyle.demo.web.controller" />


    <!-- 2.配置注解处理映射-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!--3.配置适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

    </bean>

    <!-- 4.配置springmvc视图解析器 视图解析器解析的视频路径为:前缀 + 后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
③、添加db.propertiesconfig文件夹

在这里插入图片描述

# MySQL数据库驱动
jdbc.driver=com.mysql.cj.jdbc.Driver
# ssm_demo_01数据库名称
jdbc.url=jdbc:mysql://localhost:3306/ssm_demo_01
# 连接数据库的账号、密码
jdbc.username=root
jdbc.password=123456
④、添加mybatis.xmlconfig文件夹

在这里插入图片描述

<?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>
    <mappers>
        <!-- 批量加载映射文件 -->
        <package name="cn.hestyle.demo.mapper"/>
    </mappers>
</configuration>
⑤、添加applicationContext.xmlconfig文件夹

在这里插入图片描述

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.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.加载db配置文件  -->
    <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.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 3.让spring管理sqlsessionFactory -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定配置文件位置 -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>

    <!-- 4.配置mapper扫描器.批量扫描创建代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.hestyle.demo.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    </bean>

    <!-- 5.配置扫描注解 -->
    <context:component-scan base-package="cn.hestyle.demo"/>

	<!-- 6.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 7.开启事务注解-->
    <tx:annotation-driven></tx:annotation-driven>
</beans>
⑥、添加log4j.propertiesconfig文件夹

在这里插入图片描述

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
⑦、修改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">
    <!-- 1、配置spring mvc DispatcherServlet -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 3.0的spring mvc 默认加载WEB-INF下的dispatcher-servlet.xml文件 3.2的spring mvc
            加载DispatcherServlet-servlet.xml文件 -->
        <init-param>
            <!-- 修改黑底spring mvc加载的配置文件路径 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 2、spring的配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 3、配置编码过滤器(防止中文乱码)  -->
    <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>

5、创建Product实体类

①、创建cn.hestyle.demo.entity

在这里插入图片描述
在这里插入图片描述

②、创建Product实体类

在这里插入图片描述
在这里插入图片描述

package cn.hestyle.demo.entity;

import java.io.Serializable;
import java.util.Date;

/**
 * description: Product实体类
 *
 * @author hestyle
 * @version 1.0
 * @className ssm_demo_01->Product
 * @date 2020-01-30 10:30
 **/
public class Product implements Serializable {
    private Integer id;
    /**商品名*/
    private String name;
    /**商品价格*/
    private Double price;
    /**商品库存数量*/
    private Integer number;
    /**商品简介*/
    private String info;
    /**商品照片路径*/
    private String picturePath;
    /**商品创建时间*/
    private Date createdTime;

    public Product() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getPicturePath() {
        return picturePath;
    }

    public void setPicturePath(String picturePath) {
        this.picturePath = picturePath;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", number=" + number +
                ", info='" + info + '\'' +
                ", picturePath='" + picturePath + '\'' +
                ", createdTime=" + createdTime +
                '}';
    }
}

6、创建dao层(也称mapper)

①、创建cn.hestyle.demo.mapper

在这里插入图片描述

②、创建ProductMapper接口类

在这里插入图片描述

package cn.hestyle.demo.mapper;

import cn.hestyle.demo.entity.Product;

import java.util.List;

/**
 * description: ProductMapper接口类,相当于IProductDao接口类
 *
 * @author hestyle
 * @version 1.0
 * @className ssm_demo_01->ProductMapper
 * @date 2020-01-30 10:36
 **/
public interface ProductMapper {
    /**
     * 保存product
     * @param product 待保存的product实体对象
     * @return 受影响的行数
     */
    public Integer save(Product product);

    /**
     * 更新product
     * @param product 待更新的product实体对象
     * @return 受影响的行数
     */
    public Integer update(Product product);

    /**
     * 通过id删除product
     * @param id 待删除的product实体对象id
     * @return 受影响的行数
     */
    public Integer deleteById(Integer id);

    /**
     * 通过id查找product
     * @param id 待删除的product实体对象id
     * @return 查找到的实体对象
     */
    public Product findById(Integer id);

    /**
     * 查找所有product
     * @return Product list
     */
    public List<Product> findAll();
}
③、创建ProductMapper.xml映射文件

在这里插入图片描述

<?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">
<!--
	namespace:命名空间,它的作用就是对SQL进行分类化管理,可以理解为SQL隔离
	注意:使用mapper代理开发时,namespace有特殊且重要的作用
 -->
<mapper namespace="cn.hestyle.demo.mapper.ProductMapper">
	<!--
	保存product对象, public Integer save(Product product);
        [id]:statement的id,要求在命名空间内唯一
        keyProperty="id" useGeneratedKeys="true"用于设置自动增长标识(id字段自动增长)
        [parameterType]:入参的java类型
     -->
	<insert id="save" keyProperty="id" useGeneratedKeys="true" parameterType="cn.hestyle.demo.entity.Product">
		INSERT INTO `t_product`(
			`name`, `price`, `number`, `info`, `picture_path`, `created_time`
		) VALUES (
			#{name},#{price},#{number},#{info},#{picturePath},#{createdTime}
		)
	</insert>

	<!--
	更新product对象, public Integer update(Product product);
        注意created_time不用更新,可以在表中增加一个modify_time字段,trim的作用是删除最后一个逗号
     -->
	<update id="update" parameterType="cn.hestyle.demo.entity.Product">
		UPDATE `t_product`
		<trim prefix="set" suffixOverrides=",">
			<if test="name != null">
				`name` = #{name},
			</if>
			<if test="price != null">
				`price` = #{price},
			</if>
			<if test="number != null">
				`number` = #{number},
			</if>
			<if test="info != null">
				`info` = #{info},
			</if>
			<if test="picturePath != null">
				`picture_path` = #{picturePath},
			</if>
		</trim>
		WHERE id = #{id}
	</update>

	<!--
	通过id删除product对象,public Integer deleteById(Integer id);
        注意这里可以不用真的删除,可以在表中增加一个is_del字段记录是否删除
     -->
	<delete id="deleteById" parameterType="java.lang.Integer">
		DELETE FROM `t_product`
		WHERE id = #{id}
	</delete>

    <!--
    通过id查找Product表, public Product findById(Integer id);
        由于表中的picture_path、created_time字段与Product实体类中的picturePath、createdTime属性不对应,所以需要手动指定映射关系
     -->
    <select id="findById" parameterType="java.lang.Integer" resultType="cn.hestyle.demo.entity.Product">
		SELECT `id`, `name`, `price`, `number`, `info`, `picture_path` picturePath, `created_time` createdTime
		FROM `t_product`
		WHERE id = #{id}
	</select>

	<!--
    查找product表所有记录,public List<Product> findAll();
        由于表中的picture_path、created_time字段与Product实体类中的picturePath、createdTime属性不对应,所以需要手动指定映射关系
        虽然指定的返回类型是Product,不过这里可能会查到多条数据,因此会自动转化成list
     -->
	<select id="findAll" resultType="cn.hestyle.demo.entity.Product">
		SELECT `id`, `name`, `price`, `number`, `info`, `picture_path` picturePath, `created_time` createdTime
		FROM `t_product`
	</select>
</mapper>

6、创建service

①、创建cn.hestyle.demo.service

在这里插入图片描述

②、创建IProductService接口类

在这里插入图片描述

package cn.hestyle.demo.service;

import cn.hestyle.demo.entity.Product;

import java.util.List;

/**
 * description: IProductService接口类
 *
 * @author hestyle
 * @version 1.0
 * @className ssm_demo_01->IProductService
 * @date 2020-01-30 11:10
 **/
public interface IProductService {
    /**
     * 保存product
     * @param product 待保存的product实体对象
     * @return 受影响的行数
     */
    public Integer save(Product product);

    /**
     * 更新product
     * @param product 待更新的product实体对象
     * @return 受影响的行数
     */
    public Integer update(Product product);

    /**
     * 通过id删除product
     * @param id 待删除的product实体对象id
     * @return 受影响的行数
     */
    public Integer deleteById(Integer id);

    /**
     * 通过id查找product
     * @param id 待删除的product实体对象id
     * @return 查找到的实体对象
     */
    public Product findById(Integer id);

    /**
     * 查找所有product
     * @return Product list
     */
    public List<Product> findAll();
}
③、创建cn.hestyle.demo.service.impl

在这里插入图片描述

④、创建ProductServiceImpl实现类

在这里插入图片描述

package cn.hestyle.demo.service.impl;

import cn.hestyle.demo.entity.Product;
import cn.hestyle.demo.mapper.ProductMapper;
import cn.hestyle.demo.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description: ProductServiceImpl实现类
 *
 * @author hestyle
 * @version 1.0
 * @className ssm_demo_01->ProductServiceImpl
 * @date 2020-01-30 11:13
 **/
@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Integer save(Product product) {
        return productMapper.save(product);
    }

    @Override
    public Integer update(Product product) {
        return productMapper.update(product);
    }

    @Override
    public Integer deleteById(Integer id) {
        return productMapper.deleteById(id);
    }

    @Override
    public Product findById(Integer id) {
        return productMapper.findById(id);
    }

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }
}

7、创建controller

①、创建cn.hestyle.demo.web.controller

在这里插入图片描述

②、创建ProductController

在这里插入图片描述

package cn.hestyle.demo.web.controller;

import cn.hestyle.demo.entity.Product;
import cn.hestyle.demo.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * description: ProductController控制器
 *
 * @author hestyle
 * @version 1.0
 * @className ssm_demo_01->ProductController
 * @date 2020-01-30 11:19
 **/
@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("add.do")
    public String add() {
        //跳转到/product/add.jsp页面
        return "/product/add";
    }

    @RequestMapping("save.do")
    public String save(Product product) {
        System.err.println("保存 " + product);
        product.setCreatedTime(new Date());
        productService.save(product);
        //保存product后,重定向到listAll
        return "redirect:listAll.do";
    }

    @RequestMapping("edit.do")
    public String edit(Integer id, Model model) {
        //通过model传入product到/product/edit.jsp页面
        Product product = productService.findById(id);
        System.err.println("通过id = " + id + "查找 " + product);
        model.addAttribute("product", product);
        return "/product/edit";
    }

    @RequestMapping("update.do")
    public String update(Product product) {
        System.err.println("更新" + product);
        productService.update(product);
        //保存更新后,重定向到/product/listAll.do
        return "redirect:listAll.do";
    }

    @RequestMapping("delete.do")
    public String delete(Integer id) {
        System.err.println("通过id = " + id + "删除");
        productService.deleteById(id);
        //删除product后,重定向到listAll
        return "redirect:listAll.do";
    }

    @RequestMapping("detail.do")
    public String detail(Integer id, Model model) {
        //通过model传入product到/product/detail.jsp页面
        Product product = productService.findById(id);
        System.err.println("通过id = " + id + "查找 " + product);
        model.addAttribute("product", product);
        return "/product/detail";
    }

    @RequestMapping("listAll.do")
    public String listAll(Model model) {
        //通过model传入productList到/product/listAll.jsp页面
        List<Product> productList = productService.findAll();
        System.err.println("查找所有product " + productList);
        model.addAttribute("productList", productList);
        return "/product/listAll";
    }
}

8、创建前端页面

①、在WEB-INF目录下创建views/product文件夹

在这里插入图片描述

②、创建add.jsp页面

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2020/1/30
  Time: 11:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加产品</title>
</head>
<body>
    <!-- 以post请求方式,multipart/form-data格式,发送请求给product/save.do -->
    <form action="${pageContext.request.contextPath}/product/save.do" method="post">
        <table border="1">
            <tr>
                <td>产品名</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td>产品价格</td>
                <td><input type="text" name="price"></td>
            </tr>
            <tr>
                <td>库存数量</td>
                <td><input type="text" name="number"></td>
            </tr>
            <tr>
                <td>产品简介</td>
                <td><textarea name="info"></textarea></td>
            </tr>
<%--            <tr>--%>
<%--                <td>产品照片</td>--%>
<%--                <td><input type="image" name="picture"></td>--%>
<%--            </tr>--%>
            <tr>
                <td colspan="2"><input type="submit" value="添加" align="center"></td>
            </tr>
        </table>
    </form>
</body>
</html>
③、创建listAll.jsp页面

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2020/1/30
  Time: 11:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="jstl" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>所有产品列表</title>
</head>
<body>
    <table border="1">
        <tr>
            <td>产品编号</td>
            <td>产品名称</td>
            <td>产品价格</td>
            <td>库存数量</td>
            <td>产品简介</td>
            <td>产品照片</td>
            <td>创建时间</td>
            <td>相关操作</td>
        </tr>
        <!-- 需要在导入jstl.jar依赖,并且在顶端导入jstl的uri才能使用forEach标签 -->
        <jstl:forEach items="${productList}" var="product">
            <tr>
                <td>${product.id}</td>
                <td><a href="${pageContext.request.contextPath}/product/detail.do?id=${product.id}">${product.name}</a></td>
                <td>${product.price}</td>
                <td>${product.number}</td>
                <td>${product.info}</td>
                <td><img src="${pageContext.request.contextPath}/${product.picturePath}" width="50px" height="50px"></td>
                <td>${product.createdTime}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/product/edit.do?id=${product.id}">编辑</a>
                    &nbsp<a href="${pageContext.request.contextPath}/product/delete.do?id=${product.id}">删除</a>
                </td>
            </tr>
        </jstl:forEach>
    </table>
</body>
</html>
④、创建listAll.jsp页面

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2020/1/30
  Time: 15:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>产品详情</title>
</head>
    <body>
        <table border="1">
            <tr>
                <td>产品编号</td>
                <td><input type="text" name="name" readonly value="${product.id}"></td>
            </tr>
            <tr>
                <td>产品名称</td>
                <td><input type="text" name="name" readonly value="${product.name}"></td>
            </tr>
            <tr>
                <td>产品价格</td>
                <td><input type="text" name="price" readonly value="${product.price}"></td>
            </tr>
            <tr>
                <td>库存数量</td>
                <td><input type="text" name="number" readonly value="${product.number}"></td>
            </tr>
            <tr>
                <td>产品简介</td>
                <td><textarea name="info" cols="20" rows="5" readonly>${product.info}</textarea></td>
            </tr>
            <tr>
                <td>产品照片</td>
                <td><img src="${pageContext.request.contextPath}/${product.picturePath}" width="80px" height="80px"></td>
            </tr>
            <tr>
                <td>创建时间</td>
                <td>${product.createdTime}</td>
            </tr>
        </table>
    </body>
</html>
⑤、创建edit.jsp页面

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2020/1/30
  Time: 15:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>产品详情</title>
</head>
    <body>
        <form method="post" action="${pageContext.request.contextPath}/product/update.do">
            <table border="1">
                <tr>
                    <td>产品编号</td>
                    <td><input type="text" name="id" value="${product.id}" readonly></td>
                </tr>
                <tr>
                    <td>产品名称</td>
                    <td><input type="text" name="name" value="${product.name}"></td>
                </tr>
                <tr>
                    <td>产品价格</td>
                    <td><input type="text" name="price" value="${product.price}"></td>
                </tr>
                <tr>
                    <td>库存数量</td>
                    <td><input type="text" name="number" value="${product.number}"></td>
                </tr>
                <tr>
                    <td>产品简介</td>
                    <td><textarea name="info" cols="20" rows="5">${product.info}</textarea></td>
                </tr>
                <tr>
                    <td>产品照片</td>
                    <td><img src="${pageContext.request.contextPath}/${product.picturePath}" width="80px" height="80px"></td>
                </tr>
                <tr>
                    <td rowspan="2" align="center"><input type="submit" value="保存"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

9、启动项目,访问页面

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Demo源码下载地址:
地址一:https://pan.baidu.com/s/1XkCMK7wMGJzIxHIG8YlClQ 提取码: sc8g
地址二:https://pan.baidu.com/s/1c2eMVWYnxzc971wWqNo9jw 提取码: xx5j
地址三:https://pan.baidu.com/s/1DJ0gY-x0NIgRWjve37B-HA 提取码: nuy8

博客推荐:
Mybatis框架之快速入门(别再翻了,此篇博客就够了)
SpringMVC框架之创建第一个项目(IDEA)
SpringMVC框架之前后端数据交互(附Demo)

以上就是Spring、SpringMVC、Mybatis框架整合(IDEA)(附Demo)主要内容,其实我就是为了水一篇。。。。

发布了976 篇原创文章 · 获赞 230 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_41855420/article/details/104110426
今日推荐