springboot基础及上传组件封装-准备工作

  • idea 中创建项目,java8 , springboot 2

  • maven 所需依赖
<?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>
    <groupId>com.bimcc</groupId>
    <artifactId>iot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>iot</name>
    <description>iot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--数据库迁移 -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>5.2.4</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--请求验证-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>2.7.8</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

        <!--工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.bimcc.iot.IotApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

  • 创建项目目录

在这里插入图片描述

目录意义如下:

请添加图片描述

  • 修改 application.yml 配置:

---
# 开发环境的配置
server:
  port: 9090
spring:
  controller:
    api-prefix: /api
  flyway:
    enabled: true #开启数据迁移
    table: flyway_schema_history #用于存储迁移历史记录的表名,默认为flyway_schema_history
    baseline-on-migrate: true #当迁移数据库存在但没有元数据的表时,自动执行基准迁移,新建flyway_schema_history表
    locations: classpath:db/migration #数据库迁移脚本的位置,默认为classpath:db/migration,classpath 羡慕resources目录
    clean-disabled: true #用于控制是否禁用 Flyway 的 clean 操作。
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/java_iot?serverTimezone=GMT%2b8
  config:
    activate:
      on-profile: dev #开发环境
  servlet:
    multipart:
      enabled: true # 允许文件上传
      max-file-size: 20971520 # 单文件最大限制 20M
      max-request-size: 52428800 # 单次请求最大限制 50M
file:
  upload:
    path: E:\project-java\java-upload  # 文件上传保存服务器绝对路径
    suffix: jpg,jpeg,png,bmp,xls,xlsx,pdf  # 文件上传保存路径
    is-thumb: true  # 是否开启缩略图 true false
    proportion: 5  # 缩略图缩放比例
    path-pattern: uploads  # 访问虚拟目录
log:
  level: INFO # INFO DEBUG ERROR
---

# 当前启用的配置
spring:
  application:
    name: iot # 应用平台
  profiles:
    active: dev   # 当前环境
  • 创建数据表迁移文件

在这里插入图片描述

写入以下内容:

CREATE TABLE sys_file
(
    id         INT AUTO_INCREMENT COMMENT 'id',
    file_name  VARCHAR(255) NOT NULL COMMENT '文件名称',
    ip         VARCHAR(255) COMMENT '上传ip',
    file_path  VARCHAR(255) NOT NULL COMMENT '文件路径',
    thumb_path  VARCHAR(255) COMMENT '缩略图文件路径',
    file_size  INT COMMENT '字节大小',
    file_type  VARCHAR(255) COMMENT '文件类型',
    file_ext   CHAR(36) COMMENT '文件后缀',
    file_md5   VARCHAR(255) COMMENT '文件md5',
    created_at DATETIME COMMENT '创建时间',
    updated_at DATETIME COMMENT '修改时间',
    deleted_at DATETIME COMMENT '删除时间',
    PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = UTF8MB4;
上一篇:Linux音乐前端是什么:深入解析与探索


下一篇:Nginx实战:https 配置SSL证书