在这篇博客中,我们将使用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>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.8version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.26version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
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 />
在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);
}
在src/main/resources
目录下创建一个名为mapper
的文件夹,并在该文件夹下创建一个名为ProductMapper.xml
的文件,作为商品Mapper映射文件。代码如下:
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>
在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";
}
}
在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" %>
商品列表
商品列表
ID
名称
价格
库存
分类
${product.id}
${product.name}
${product.price}
${product.stock}
${product.category}
现在,我们可以启动项目并访问商品查询页面了。在浏览器中输入http://localhost:8080/products?category=电子产品
,即可获取并展示符合条件的商品信息。
通过以上步骤,我们成功使用SSM框架实现了商品分类查询功能。这个简易商城项目只是一个起点,您可以根据需求进行扩展和优化,添加更多功能,如商品添加、购物车、下单等。希望本篇博客能够对您理解和学习SSM框架有所帮助。