Build background framework

1. Introduce dependencies

The package hierarchy relationship of the project is as follows

Dependency introduction

<?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>com.like</groupId>
    <artifactId>ImageCrudProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>

    <dependencies>
<!--        springboot 启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
<!--     springboot web 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.1.0</version>
        </dependency>
<!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
<!--        mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>

<!--       mysql依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

<!--        连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>
    </dependencies>
</project>

2. Startup category

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.like.dao")
public class ImageCrudApplication {
     public static void main(String[] args) {
          SpringApplication.run(ImageCrudApplication.class,args);
     }
}

3. Configure yml file

# 配置端口号
server.port=3128

#链接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/imagesups?&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=

#配置mp
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml
mybatis-plus.type-aliases-package=com.like.entity

#设置大文件上传
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=1024MB


#图片存储路径
images.path=D:/images/

4. Related configurations

Solve cross-domain issues

import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

     //ctrl + i
     @Override
     public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping("/**")
                  .allowedOriginPatterns("*")
                  .allowedHeaders(CorsConfiguration.ALL)
                  .allowedMethods(CorsConfiguration.ALL)
                  .allowCredentials(true)
                  .maxAge(3600);//1小时内不需要再校验(option请求)
     }
}

MP paging plug-in configuration

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {
    //配置分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //数据库类型是MySql,因此参数填写DbType.MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

5. Creation of public classes in common package

import lombok.Data;

//Dto是后端返回给前端的数据
@Data
public class CommonDto<T>{
     //业务上的成功失败
     private Boolean success = true;

     //返回给前端的数据
     private String message;

     //返回范型数据
     private T content;
}
import lombok.Data;

import java.util.List;

@Data
public class PageDto<T> {

     private Long total;

     private List<T> list;
}
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

//Vo用来接受前台的数据
@Data
public class PageVo {
     //第几页
     @TableField(exist = false)
     private int page;
     //每页多少条
     @TableField(exist = false)
     private int size;
}

6. Some related issues in back-end framework construction

1. Issues related to dependency introduction

First, the dependent version number must correspond to the project-related version number.

When using Maven to download dependencies, we often encounter situations where the dependencies cannot be loaded and the loading speed is too slow. This is because our Maven setting. It's very fast.

Domestic download source and idea configuration Maven portal

2. When the project starts

When the springboot project is started, the following phenomenon appears, but it does not affect the normal operation of the project.

OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

Solution:

This is because idea will perform some parameter optimization when starting the project. It is for jdk versions below 13. Jdk versions above 13 are already obsolete parameters and will be deleted in the future. Therefore, the jdk17 we use does not need to optimize this parameter.

Check Disable startup optimization

Guess you like

Origin blog.csdn.net/m0_63732435/article/details/133436531