农产品销售管理系统实战——后端项目创建

说明

我们先来开发后端,确保之前的环境已经配置好。

初始化数据库

Win + R打开运行,输入“cmd”,然后输入mysql -u root -p会提示输入密码

我们输入刚才设置的管理员密码root敲击回车(命令行里不显示),然后就会进入MySQL管理系统,此时提示符已经变成了 mysql>

输入我们刚才数据库表的初始化语句,如果报错,就逐句输入。

CREATE DATABASE db_agriculture;
USE db_agriculture;

CREATE TABLE tb_user (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID,主键',
    `name` VARCHAR(255) COMMENT '用户名',
    `password` VARCHAR(255) COMMENT '密码',
    `money` TEXT COMMENT '余额',
    salt VARCHAR(255) COMMENT '盐值',
    `role` VARCHAR(255) COMMENT '角色',
    is_deleted TINYINT(1) COMMENT '是否删除 true: 已删除, false: 未删除',
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='用户表';

CREATE TABLE tb_address (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '地址ID,主键',
    user_id BIGINT COMMENT '用户ID',
    `address` VARCHAR(255) COMMENT '地址',
    is_deleted TINYINT(1) COMMENT '是否删除 true: 已删除, false: 未删除',
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='用户地址表';


CREATE TABLE tb_commodity (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '商品ID,主键',
    `name` VARCHAR(255) COMMENT '商品名称',
    price VARCHAR(255) COMMENT '商品价格',
    total BIGINT COMMENT '商品总量',
    cnt BIGINT COMMENT '商品剩余量',
    pic LONGBLOB COMMENT '商品图片',
    is_deleted TINYINT(1) COMMENT '是否删除 true: 已删除, false: 未删除',
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    user_id BIGINT COMMENT '所属商家ID'
) COMMENT='商品表';


CREATE TABLE tb_cart (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '购物车ID,主键',
    user_id BIGINT COMMENT '用户ID',
    commodity_id BIGINT COMMENT '商品ID',
    cnt BIGINT COMMENT '选中数量',
    is_deleted TINYINT(1) COMMENT '是否删除 true: 已删除, false: 未删除',
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) COMMENT='购物车表';

CREATE TABLE tb_order (
    id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '订单ID,主键',
    user_id BIGINT COMMENT '用户ID',
    commodity_id BIGINT COMMENT '商品ID',
    cnt BIGINT COMMENT '选中数量',
    total TEXT COMMENT '总价',
    is_deleted TINYINT(1) COMMENT '是否删除 true: 已删除, false: 未删除',
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)COMMENT='订单表';

当然你也可以在SQLyog或者其他图形化界面的数据库管理软件中进行配置。

创建项目

我们在某个目录下创建一个新的文件夹。例如G:\my_program\下创建agricultural_manager,目录名尽量不要包含中文和空格。

然后我们在VS Code中打开这个目录G:\my_program\agricultural_manager,然后再VS Code中按Ctrl + Shift + P

输入Spring,选择第一个Spring Initializr: Create a Maven Project,意思是初始化一个Spring项目,它默认使用Maven作为包管理器。

选择Spring3.4.4版本

选择语言为Java

组织我这里以UJN为例

设置Artifact Idargi

包类型选择Jar

Java版本选择17

引入的包选择这几个

  • Spring Web
  • Spring Boot Dev Tools
  • Lombok
  • Thymeleaf

点击Generate into this folder

右下角会提示打开项目目录,点击Open

然后回打开一个新窗口,项目目录结构如下

配置项目

打开pom.xml,将内容替换成以下内容

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.3</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.edu.ujn</groupId>
    <artifactId>argi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>argi</name>
    <description>Demo project for Spring Boot</description>
    <url />
    <licenses>
        <license />
    </licenses>
    <developers>
        <developer />
    </developers>
    <scm>
        <connection />
        <developerConnection />
        <tag />
        <url />
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

然后打开src/main/resource目录,将application.properties删除,替换成application.yaml

spring:
  application:
    name: argi
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_argi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

server:
  servlet:
    context-path: /api

jwt:
  secret: 123456
  expired: 3600000

注意这里的spring.datasource部分将url中的数据库替换成你的,例如db_argi,以及下方的用户名账户需要根据你的及其来,按我们之前的教程设置的root账户密码也为root

对于server.servlet.context-path部分则是用于指定接口的URL前缀,比如我们项目跑起来就是http://xxx/api/后面跟着Controller的映射和方法的路径映射。(接下来你会看到)

这里的jwt字段是用于配置JWT的Token校验的,expired是Token过期时间,secret是JWT服务器签发的私钥

运行项目

我们接下来写一个控制器来测试下,在src/main/java/cn/edu/ujn/argi下创建文件夹controller,然后在新创建的文件夹下添加测试控制器TestController,其文件名为TestController.java

输入以下内容,按下Ctrl + S保存(编辑完文件一定要记得保存,否则运行不生效)

package cn.edu.ujn.argi.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testController")
public class TestController {
    
    @GetMapping("helloworld")
    public ResponseEntity<?> helloworld(){
        return ResponseEntity.ok("hello world");
    }
}

这里需要解释下,@RequestMapping注解至于类声明上,也就是说这个控制器下每个方法的Http URL路径都是以testController为前缀的。

然后@GetMapping注解在方法helloworld上,说明这是个Get请求,而且请求路径结点对应helloworld,也就是说结合我们在配置文件设置的前缀,到访问helloworld方法的总路径为http://localhost:8080/api/testController/helloworld

接下来我们修改文件src/main/java/cn/edu/ujn/argi/ArgiApplication.java,添加@ComponentScan(basePackages = "cn.edu.ujn.argi")主机,这个用于自动扫描当前包下的所有组件(例如我们刚才的控制器)

package cn.edu.ujn.argi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "cn.edu.ujn.argi")
@SpringBootApplication
public class ArgiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ArgiApplication.class, args);
    }

}

接下来我们可以运行项目了,在VS Code左侧右键文件src/main/java/cn/edu/ujn/argi/ArgiApplication.java(这个文件包含了main方法以及@SpringBootApplication注解,是程序的入口),点击Run Java选项启动项目

然后就可以在VS Code控制台中看到项目成功启动了

打开浏览器,地址栏输入http://localhost:8080/api/testController/helloworld,发现已经能显示内容了。

如果到这一步你不能显示,请检查之前的步骤是否正确,若正确,你可以在项目根目录(VS Code左侧右键点击空白部分,点击在集成终端打开)打开终端,输入mvn clean清除构建,然后mvn install重新打包尝试。(一定要在项目根目录运行,也就是src文件夹所在目录,不是src里面)

最后修改:2025 年 04 月 15 日
如果觉得我的文章对你有用,请随意赞赏