一、引入依赖
项目的包层级关系如下
依赖引入
<?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>
二、启动类
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);
}
}
三、配置yml文件
# 配置端口号
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/
四、相关配置
解决跨域问题
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分页插件配置
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;
}
}
五、common包公共类创建
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;
}
六、后端框架搭建的一些相关问题
1.依赖引入相关问题
首先依赖的版本号要与项目相关版本号对应
在用Maven下载依赖的时候,我们常常会遇到依赖加载不进来,加载速度过慢的情况,这是我们的Maven的setting.xml配置文件中没有加入阿里的国内镜像源,加入后下载的速度就非常快了。
国内下载源及idea配置Maven 传送门
2.项目启动时
springboot项目启动时候,出现爆红如下,但是不影响项目正常运行
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.
解决方式:
这是因为idea启动项目的时候会进行一些参数优化。是针对jdk 13版本以下的,jdk 13版本以上已经是过时参数了,并在未来会被删除。因此我们用的jdk17是不需要进行这个参数优化的。
勾选禁用启动优化