基于XML配置方式SSM框架西蒙购物网

Posted qq_48838980

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于XML配置方式SSM框架西蒙购物网相关的知识,希望对你有一定的参考价值。

一、数据库准备

1、创建数据库 - simonshop

在这里插入图片描述

2、创建用户表 - t_user

  • 创建表
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) DEFAULT NULL,
  `telephone` varchar(11) DEFAULT NULL,
  `register_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `popedom` int(11) DEFAULT NULL COMMENT '0:管理员;1:普通用户',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

  • 插入表记录
INSERT INTO `t_user` VALUES ('1', 'admin', '12345', '15734345678', '2016-12-02 08:40:35', '0');
INSERT INTO `t_user` VALUES ('2', '郑晓红', '11111', '13956567889', '2016-12-20 09:51:43', '1');
INSERT INTO `t_user` VALUES ('3', '温志军', '22222', '13956678907', '2016-12-20 09:52:36', '1');
INSERT INTO `t_user` VALUES ('4', '涂文艳', '33333', '15890905678', '2016-12-05 09:52:56', '1');

3、创建商品类别表 - t_category

  • 创建表
CREATE TABLE `t_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品类别标识符',
  `name` varchar(100) NOT NULL COMMENT '商品类别名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

  • 插入表记录
INSERT INTO t_category VALUES('1','家用电器');
INSERT INTO t_category VALUES('2','床上用品');
INSERT INTO t_category VALUES('3','文具用品');
INSERT INTO t_category VALUES('4','休闲食品');

4、创建商品表 - t_product

CREATE TABLE `t_product` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品标识符',
  `name` varchar(200) NOT NULL COMMENT '商品名称',
  `price` double DEFAULT NULL COMMENT '商品单价',
  `add_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `category_id` int(11) DEFAULT NULL COMMENT '商品类别标识符',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

CREATE TABLE `t_product` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品标识符',
  `name` varchar(200) NOT NULL COMMENT '商品名称',
  `price` double DEFAULT NULL COMMENT '商品单价',
  `add_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `category_id` int(11) DEFAULT NULL COMMENT '商品类别标识符',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

  • 插入表记录
INSERT INTO `t_product` VALUES ('1', '容声电冰箱', '2000', '2016-12-20 09:54:41', '1');
INSERT INTO `t_product` VALUES ('2', '松下电视', '5000', '2016-12-20 09:54:35', '1');
INSERT INTO `t_product` VALUES ('3', '红岩墨水', '3', '2016-12-20 09:56:05', '3');
INSERT INTO `t_product` VALUES ('4', '海尔洗衣机', '1000', '2016-11-30 08:58:09', '1');
INSERT INTO `t_product` VALUES ('5', '新宇电饭煲', '1200', '2016-12-20 09:55:11', '1');
INSERT INTO `t_product` VALUES ('6', '英雄微波炉', '600', '2016-12-20 09:55:39', '1');
INSERT INTO `t_product` VALUES ('7', '红双喜席梦思', '700', '2016-11-28 08:59:38', '2');
INSERT INTO `t_product` VALUES ('8', '旺仔牛奶糖', '24.4', '2016-12-20 10:00:11', '4');
INSERT INTO `t_product` VALUES ('9', '西蒙枕头', '100', '2016-12-20 09:56:57', '2');
INSERT INTO `t_product` VALUES ('10', '甜甜毛毯', '400', '2016-12-20 09:57:26', '2');
INSERT INTO `t_product` VALUES ('11', '永久钢笔', '50', '2016-12-20 09:57:30', '3');
INSERT INTO `t_product` VALUES ('12', '硬面抄笔记本', '5', '2016-12-20 09:57:53', '3');
INSERT INTO `t_product` VALUES ('13', '晨光橡皮擦', '0.5', '2016-11-30 09:02:40', '3');
INSERT INTO `t_product` VALUES ('14', '美的空调', '3000', '2016-11-03 09:03:02', '1');
INSERT INTO `t_product` VALUES ('15', '迷你深海鱼肠', '14.4', '2016-12-02 10:01:14', '4');

5、创建订单表 - t_order

  • 创建表
CREATE TABLE `t_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单标识符',
  `username` varchar(20) DEFAULT NULL COMMENT '用户名',
  `telephone` varchar(11) DEFAULT NULL COMMENT '电话号码',
  `total_price` double DEFAULT NULL COMMENT '总金额',
  `delivery_address` varchar(50) DEFAULT NULL COMMENT '送货地址',
  `order_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '下单时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

  • 创建表记录
INSERT INTO `t_order` VALUES ('1', '郑晓红', '13928765789', '2000','泸职院信工院','2016-12-20 09:54:41');
INSERT INTO `t_order` VALUES ('2', '温志军', '13928765789', '1000','泸职院人文学院','2016-12-20 09:24:41');

  • 最终表
    在这里插入图片描述

二、项目实现

1、创建Maven项目

在这里插入图片描述

2、添加依赖

在这里插入图片描述

0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ4ODM4OTgw,size_16,color_FFFFFF,t_70)

<?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>net.zjs.shop</groupId>
    <artifactId>SSSMSimonShop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- spring.version -->
        <spring.version>5.2.9.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!--Spring核心-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring Bean-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring容器-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring数据库支持-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--数据库驱动工具包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!--数据库连接池框架-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.24</version>
        </dependency>
        <!--持久层框架-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <!--提供MyBatis与Spring整合的支持-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>
        <!--日志框架-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--Spring AOP-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--AspectJ支持-->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
            <scope>runtime</scope>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <!--Spring Web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring MVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--JSP标准标签库-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--Servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

3、创建日志属性文件

在这里插入图片描述

log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

4、创建数据库配置属性文件

在这里插入图片描述

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/simonshop?useSSL=false
jdbc.username = root
jdbc.password = 12345

5、给项目添加Web功能

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

6、配置Tomcat服务器

在这里插入图片描述

7、创建实体类

(1)创建用户实体类

在这里插入图片描述

package net.zjs.shop.bean;

import java.util.Date;

/**
 * 功能:用户实体类
 * 作者:zjs
 * 日期:2021-05-06
 */
public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Date registerTime;
    private int popedom;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public int getPopedom() {
        return popedom;
    }

    public void setPopedom(int popedom) {
        this.popedom = popedom;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\\'' +
                ", password='" + password + '\\'' +
                ", telephone='" + telephone + '\\'' +
                ", registerTime=" + registerTime +
                ", popedom=" + popedom +
                '}';
    }
}

(2)创建类别实体类

在这里插入图片描述

package net.zjs.shop.bean;

/**
 * 功能:类别实体类
 * 作者:zjs
 * 日期:2021-05-06
 */
public class Category {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\\'' +
                '}';
    }
}

(3)创建商品实体类

在这里插入图片描述

package net.zjs.shop.bean;

import java.util.Date;

/**
 * 功能:商品实体类
 * 作者:zjs
 * 日期:2021-05-06
 */
public class Product {
    private int id;
    private String name;
    private double price;
    private Date addTime;
    private int categoryId;

    public int getId() {
        return id;
    }

    public void setId(int 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 Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", price=" + price +
                ", addTime=" + addTime +
                ", categoryId=" + categoryId +
                '}';
    }
}

(4)创建订单实体类

在这里插入图片描述

package net.zjs.shop.bean;

import java.util.Date;

/**
 * 功能:订单实体类
 * 作者:zjs
 * 日期:2021-05-06
 */
public class Order {
    private int id;
    private String username;
    private String telephone;
    private double totalPrice;
    private String deliveryAddress;
    private Date orderTime;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public String getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setDeliveryAddress(String deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }

    public Date getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", username='" + username + '\\'' +
                ", telephone='" + telephone + '\\'' +
                ", totalPrice=" + totalPrice +
                ", deliveryAddress='" + deliveryAddress + '\\'' +
                ", orderTime=" + orderTime +
                '}';
    }
}

8、创建映射器接口

(1)创建用户映射器接口

在这里插入图片描述

package net.zjs.shop.mapper;

import net.zjs.shop.bean.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能:用户映射器接口
 * 作者:zjs
 * 日期:2021-05-06
 */
@Repository
public interface UserMapper {
    // 插入用户
    int insert(User user);

    // 按标识符删除用户
    int deleteById(int id);

    // 更新用户
    int update(User user);

    // 按标识符查询用户
    User findById(int id);

    // 按用户名查询用户
    List<User> findByUsername(String username);

    // 查询全部用户
    List<User> findAll();

    // 用户登录
    User login(@Param("username") String username, @Param("password") String password);
}


(2)创建类别映射器接口

在这里插入图片描述

package net.zjs.shop.mapper;

import net.zjs.shop.bean.Category;
import org.springframework.stereotype.Repository;

import java.util.List;

/**

  • 功能:类别映射器接口
  • 作者:zjs
  • 日期:2021-05-06
    */
    @Repository
    public interface CategoryMapper {
    // 插入类别
    int insert(Category category);
    // 按标识符删除类别
    int deleteById(int id);
    // 更新类别
    int update(Category category);
    // 按标识符查询类别
    Category findById(int id);
    // 查询全部类别
    List findAll();
    }

(3)创建商品映射器接口

在这里插入图片描述

package net.zjs.shop.mapper;

import net.zjs.shop.bean.Product;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能:商品映射器接口
 * 作者:zjs
 * 日期:2021-05-06
 */
@Repository
public interface ProductMapper {
    // 插入商品
    int insert(Product product);
    // 按标识符删除商品
    int deleteById(int id);
    // 更新商品
    int update(Product product);
    // 按标识符查询商品
    Product findById(int id);
    // 按类别查询商品
    List<Product> findByCategoryId(int categoryId);
    // 查询全部商品
    List<Product> findAll();
}


(4)创建订单映射器接口

在这里插入图片描述

package net.zjs.shop.mapper;

import net.zjs.shop.bean.Order;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能:订单映射器接口
 * 作者:zjs
 * 日期:2021-05-06
 */
@Repository
public interface OrderMapper {
    // 插入订单
    int insert(Order order);
    // 按标识符删除订单
    int deleteById(int id);
    // 更新订单
    int update(Order order);
    // 按标识符查询订单
    Order findById(int id);
    // 查询全部订单
    List<Order> findAll();
}

9、创建服务类

(1)创建用户服务类

在这里插入图片描述

package net.zjs.shop.service;

import net.zjs.shop.bean.User;
import net.zjs.shop.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:用户服务类
 * 作者:zjs
 * 日期:2021-05-06
 */
@Service
@Transactional
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public int addUser(User user) {
        // 避免注册相同用户名
        if (findUsersByUsername(user.getUsername()).size()==0) {
            return userMapper.insert(user);
        } else {
            return 0;
        }
    }

    public int deleteUserById(int id) {
        return userMapper.deleteById(id);
    }

    public int updateUser(User user) {
        return userMapper.update(user);
    }

    public User findUserById(int id) {
        return userMapper.findById(id);
    }

    public List<User> findUsersByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    public List<User> findAllUsers() {
        return userMapper.findAll();
    }

    public User login(String username, String password) {
        return userMapper.login(username, password);
    }
}


(2)创建类别服务类

在这里插入图片描述

package net.zjs.shop.service;

import net.zjs.shop.bean.Category;
import net.zjs.shop.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:类别服务类
 * 作者:zjs
 * 日期:2021-05-06
 */
@Service
@Transactional
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public int addCategory(Category category) {
        return categoryMapper.insert(category);
    }

    public int deleteCategoryById(int id) {
        return categoryMapper.deleteById(id);
    }

    public int updateCategory(Category category) {
        return categoryMapper.update(category);
    }

    public Category findCategoryById(int id) {
        return categoryMapper.findById(id);
    }

    public List<Category> findAllCategories() {
        return categoryMapper.findAll();
    }
}

(3)创建商品服务类

在这里插入图片描述

package net.zjs.shop.service;

import net.zjs.shop.bean.Product;
import net.zjs.shop.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:商品服务类
 * 作者:zjs
 * 日期:2021-05-06
 */
@Service
@Transactional
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    public int addProduct(Product product) {
        return productMapper.insert(product);
    }

    public int deleteProductById(int id) {
        return productMapper.deleteById(id);
    }

    public int updateProduct(Product product) {
        return productMapper.update(product);
    }

    public Product findProductById(int id) {
        return productMapper.findById(id);
    }

    public List<Product> findProductsByCategoryId(int categoryId) {
        return productMapper.findByCategoryId(categoryId);
    }

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

(4)创建订单服务类

在这里插入图片描述

package net.zjs.shop.service;

import net.zjs.shop.bean.Order;
import net.zjs.shop.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:订单服务类
 * 作者:zjs
 * 日期:2021-05-06
 */
@Service
@Transactional
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    public int addOrder(Order order) {
        return orderMapper.insert(order);
    }

    public int deleteOrderById(int id) {
        return orderMapper.deleteById(id);
    }

    public int updateOrder(Order order) {
        return orderMapper.update(order);
    }

    public Order findOrderById(int id) {
        return orderMapper.findById(id);
    }

    public List<Order> findAllOrders() {
        return orderMapper.findAll();
    }
}


10、创建控制器

(1)创建用户控制器

在这里插入图片描述

package net.zjs.shop.webmvc;

import net.zjs.shop.bean.Category;
import net.zjs.shop.bean.User;
import net.zjs.shop.service.CategoryService;
import net.zjs.shop.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

/**
 * 功能:用户控制器
 * 作者:zjs
 * 日期:2021-05-06
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private CategoryService categoryService;

    @RequestMapping("/login")
    public String login(@RequestParam("username")String username,
                        @RequestParam("password") String password,
                        HttpSession session) {
        // 调用用户服务对象的登录方法
        User user = userService.login(username, password);
        // 判断是否登录成功
        if (user != null) {
            session.setAttribute("username", username);
            session.removeAttribute("loginMsg");
            // 判断用户的角色
            if (user.getPopedom() == 0) {
                // 映射到后台管理页面
                return "backend/management";
            } else {
                List<Category> categories = categoryService.findAllCategories();
                session.setAttribute("categories", categories);
                // 映射到前台显示商品类别页面
                return "frontend/showCategory";
            }
        } else {
            session.setAttribute("loginMsg", "用户名或密码错误!");
            return "frontend/login";
        }
    }

    @RequestMapping("/register")
    public String register(@RequestParam("username")String username,
                           @RequestParam("password") String password,
                           @RequestParam("telephone") String telephone,
                           HttpSession session) {
        // 设置注册时间(时间戳对象)
        Timestamp registerTime = new Timestamp(System.currentTimeMillis());
        // 设置用户为普通用户
        int popedom = 1;
        // 创建用户对象
        User user = new User();
        // 设置用户对象信息
        user.setUsername(username);
        user.setPassword(password);
        user.setTelephone(telephone);
        user.setRegisterTime(registerTime);
        user.setPopedom(popedom);
        // 调用UserService对象添加用户方法
        int count = userService.addUser(user);
        // 判断是否注册成功
        if (count > 0) {
            session.setAttribute("registerMsg", "恭喜,注册成功!");
            // 映射到登录页面
            return "frontend/login";
        } else {
            session.setAttribute("registerMsg", "遗憾,注册失败!");
            // 映射到前台注册页面
            return "frontend/register";
        }
    }

    @RequestMapping("/showUser")
    public String showUser(HttpSession session) {
        List<User> users = userService.findAllUsers();
        session.setAttribute("users", users);
        // 映射到后台显示用户页面
        return "backend/showUser";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        // 删除会话属性
        session.removeAttribute("username");
        // 结束会话
        session.invalidate();
        // 映射到前台登录页面
        return "frontend/login";
    }
}

(2)创建类别控制器

在这里插入图片描述

package net.zjs.shop.webmvc;

import net.zjs.shop.bean.Category;
import net.zjs.shop.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 功能:类别控制器
 * 作者:zjs
 * 日期:2021-05-06
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @RequestMapping("/showCategory")
    public String showCategory(HttpSession session) {
        List<Category> categories = categoryService.findAllCategories();
        session.setAttribute("categories", categories);
        return "frontend/showCategory";
    }
}

(3)创建商品控制器

在这里插入图片描述

待更新

以上是关于基于XML配置方式SSM框架西蒙购物网的主要内容,如果未能解决你的问题,请参考以下文章

基于 xml 配置文件的入门级 SSM 框架整合

基于SSM框架贺州学院校园二手交易平台设计与实现

基于SSM框架的网上购物送货系统

使用纯Java搭建SSM框架

基于SSM框架配置多数据源

基于SSM框架的服饰商城系统的设计与实现(文末附源码)