SSM简易商城项目之商品分类查询商品功能
在这篇博客中,我们将使用SSM(Spring+SpringMVC+MyBatis)框架来实现一个简易商城项目的商品分类查询功能。我们将按照以下步骤逐步进行实现。
步骤一:创建数据库和表
首先,我们需要创建一个数据库和相应的表来存储商品信息。在MySQL数据库中执行以下SQL语句来创建数据库和表。
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE product (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
price DOUBLE NOT NULL,
stock INT NOT NULL,
category VARCHAR(50) NOT NULL
);
步骤二:配置项目依赖和环境
接下来,我们需要配置项目的依赖和环境。在Maven项目的pom.xml
文件中添加以下依赖:
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
然后,我们需要配置项目的数据库连接信息和MyBatis框在项目的src/main/resources
目录下创建一个名为application.properties
的文件,并添加以下内容作为数据库连接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
接下来,在src/main/resources
目录下创建一个名为mybatis-config.xml
的文件,并添加以下内容作为MyBatis的配置信息:
<configuration>
<mappers>
<mapper resource="mapper/ProductMapper.xml"/>
</mappers>
</configuration>
最后,在src/main/resources
目录下创建一个名为springmvc-servlet.xml
的文件,并添加以下内容作为SpringMVC的配置信息:
<context:component-scan base-package="com.example.controller" />
<mvc:annotation-driven />
步骤三:创建实体类和Mapper接口
在src/main/java
目录下创建一个名为com.example.entity
的包,并在该包下创建一个名为Product.java
的文件,作为商品实体类。代码如下:
package com.example.entity;
public class Product {
private int id;
private String name;
private double price;
private int stock;
private String category;
// 省略getter和setter方法
}
接下来,在src/main/java
目录下创建一个名为com.example.mapper
的包,并在该包下创建一个名为ProductMapper.java
的文件,作为商品Mapper接口。代码如下:
package com.example.mapper;
import com.example.entity.Product;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ProductMapper {
List<Product> getProductsByCategory(@Param("category") String category);
}
步骤四:创建Mapper映射文件
在src/main/resources
目录下创建一个名为mapper
的文件夹,并在该文件夹下创建一个名为ProductMapper.xml
的文件,作为商品Mapper映射文件。代码如下:
<?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.example.mapper.ProductMapper">
<select id="getProductsByCategory" resultType="com.example.entity.Product">
SELECT * FROM product WHERE category = #{category}
</select>
</mapper>
步骤五:创建Controller类
在src/main/java
目录下创建一个名为com.example.controller
的包,并在该包下创建一个名为ProductController.java
的文件,作为商品Controller类。代码如下:
package com.example.controller;
import com.example.entity.Product;
import com.example.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class ProductController {
@Autowired
private ProductMapper productMapper;
@GetMapping("/products")
public String getProductsByCategory(@RequestParam("category") String category, Model model) {
List<Product> products = productMapper.getProductsByCategory(category);
model.addAttribute("products", products);
return "products";
}
}
步骤六:创建JSP视图
在src/main/webapp/WEB-INF
目录下创建一个名为views
的文件夹,并在该文件夹下创建一个名为products.jsp
的文件,作为商品列表的JSP视图。代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>商品列表</title>
</head>
<body>
<h1>商品列表</h1>
<table>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>库存</th>
<th>分类</th>
</tr>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>${product.stock}</td>
<td>${product.category}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
步骤七:运行项目
现在,我们可以启动项目并访问商品查询页面了。在浏览器中输入http://localhost:8080/products?category=电子产品
,即可获取并展示符合条件的商品信息。
通过以上步骤,我们成功使用SSM框架实现了商品分类查询功能。这个简易商城项目只是一个起点,您可以根据需求进行扩展和优化,添加更多功能,如商品添加、购物车、下单等。希望本篇博客能够对您理解和学习SSM框架有所帮助。