关于java:boot开发踩过的坑

66次阅读

共计 6928 个字符,预计需要花费 18 分钟才能阅读完成。

我的项目罕用依赖

<dependencies>
<!--Springboot 启动包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--Springboot 测试启动包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Springboot Web 服务包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<!-- 包米豆 MybatisPlus 启动包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--lombok javabean 样板包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!--fast json 数据处理包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!-- 常用工具类包 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--MySQL 数据库连贯包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 德鲁伊数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<!-- 邮件服务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- Thymeleaf 模版,用于发送模版邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Spring Data Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>

运行前端服务

1. 在前端文件目录下运行命令符行窗口

2. 应用 node 命令运行主函数 js 运行前端服务

node main.js

常见报错解决

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'http-proxy-middleware' imported from D:\study4\test\client\main.js

他说不能找到包 ’http-proxy-middleware’

那咱们就要去装置这个包

npm install http-proxy-middleware --save

数据表的名字要和 pojo 名换等

MybatisPlus Page 性能无奈应用

起因是分页插件须要手动配置

// 分页插件
@Bean
public PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();
}
/**
* 配置 MP 的分页插件
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}

LocalTime 报错

Usage of API documented as @since 1.8+

查看应用的 java 版本 还有 Module 的编译版本

Compilation failed: internal java compiler error 解决办法_ruoxiyun 的博客 -CSDN 博客_internal java error
https://blog.csdn.net/ruoxiyu…

SpringbootTest 时呈现的问题

我创立 boot 我的项目的时候 应用 idea 提供的 maven 的阿帕奇疾速模板创立的 导致用的是 Junit4 的 Test 这两个 Test 会产生抵触 始终爆空指针异样

解决办法就是把 JUnit4 的测试都干掉 全副换成 boottest

MybatisPlus 自带雪花算法

mybatis-plus 雪花算法生成 Id 应用详解_斗者_2013 的博客 -CSDN 博客_mybatis plus 雪花 id
https://blog.csdn.net/w101407…

MybatisPlus 主动填充工夫

主动填充性能 | MyBatis-Plus
https://baomidou.com/pages/4c…

Boot 黑白日志

spring:
output:
ansi:
enabled: always

通用返回类

import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* 通用返回后果,服务端响应的数据最终都会封装成此对象
* @param <T>
*/
@Data
public class R<T> {
private Integer code; // 编码:1 胜利,0 和其它数字为失败
private String msg; // 错误信息
private T data; // 数据
private Map map = new HashMap(); // 动态数据
public static <T> R<T> success(T object) {R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
public R<T> add(String key, Object value) {this.map.put(key, value);
return this;
}
}

状态后果枚举类

/**
* 后果类枚举
*/
@Getter
public enum ResultCodeEnum {SUCCESS(true,20000,"胜利"),
UNKNOWN_ERROR(false,20001,"未知谬误"),
PARAM_ERROR(false,20002,"参数谬误"),
NULL_POINT(false, 20003, "空指针异样"),
INDEX_OUT_OF_BOUNDS(false, 20004, "下标越界异样"),
;
// 响应是否胜利
private Boolean success;
// 响应状态码
private Integer code;
// 响应信息
private String message;
ResultCodeEnum(boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
}

jwt 负载内容 registered claims 已注册的申明

这些是一组预约义的申明,它们不是强制的,而是举荐的,以提供一组有用的、可互操作的申明。比方:

iss(发行者)、

exp(到期工夫)、

sub(主题)、

aud(受众)和其余。

Spring Boot 读取 Yml 配置文件的 3 种办法

@Value("${user.userName}")
private String name;
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class Users {
private String userName;
private Integer age;
private String[] tels;
private Map<String, Integer> score;
private List<Teacher> oldTeachers;
private List<Teacher> newTeachers;
}

YamlMapFactoryBean

通过这个 factoryBean 能够间接返回一个 Map,而不须要应用 JavaBean 去接管 yml 读取的值

public static void main(String[] arg0){YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
// 能够加载多个 yml 文件
yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
// 通过 getObject()办法获取 Map 对象
Map<String, Object> map = yamlMapFactoryBean.getObject();
System.out.println(map);
map.keySet().forEach(item -> {
// 能够将 map 中的值强转为 LinkedHashMap 对象
LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>) (map.get(item));
System.out.println(linkedHashMap.get("tels"));
});

http://t.csdn.cn/XQR2S

spring boot: 从 yaml 配置文件中读取数据的四种形式(spring boot 2.4.3) – 刘宏缔的架构森林 – 博客园
https://www.cnblogs.com/archi…

@Resource 和 @Autowired 的区别

@Resource 默认按 名称 形式进行 bean 匹配,

@Autowired 默认按 类型 形式进行 bean 匹配。

Spring Boot 整合 JWT 的实现步骤

https://www.jb51.net/article/…

http://t.csdn.cn/tMztt

http://t.csdn.cn/45u1R

自定义异样

public class MyException extends Exception {public MyException() {super();
}
public MyException(String str) {super(str);
}
}
throw new MyException("自定义谬误")

Java 自定义异样
http://c.biancheng.net/view/1…

YApi 部署

Windows 环境下装置 Yapi 教程_ANTeam 的博客 -CSDN 博客_yapi 装置配置 windows
https://blog.csdn.net/ANTeam/…

Windows 环境部署 YApi 教程

YApi 介绍

YApi- 高效、易用、功能强大的可视化接口治理平台
http://yapi.dapengjiaoyu.com/

解决 npm ERR! Cannot read properties of null (reading‘pickAlgorithm‘)报错问题_南风吹云的博客 -CSDN 博客
https://blog.csdn.net/qq_4375…

git clone https://gitee.com/mirrors/YApi vendors

本地线程工具类

/**
* 基于 ThreadLocal 封装工具类,用户保留和获取以后登录用户 id
*/
public class BaseContext {
// 定义并赋值一个本地线程正本 threadLocal
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
/** 设定 setCurrentId 办法用来存入 id 的值 */
public static void setCurrentId(Long id){threadLocal.set(id);
}
/** 设定 getCurrentId 办法用来获取 id 的值 */
public static Long getCurrentId(){return threadLocal.get();
}
}

新工夫 API

LocalDate:只含年月日的日期对象
LocalTime:只含时分秒的工夫对象
LocalDateTime:同时含有年月日时分秒的日期对象

LocalDateTime 用法_ChampionDragon 的博客 -CSDN 博客
https://blog.csdn.net/xxdw199…

取得 java 我的项目门路

Java 获取我的项目门路的多种形式_java_脚本之家
https://www.jb51.net/article/…

javapackager -deploy -native image  -outdir outdir -outfile outfiles -srcfiles I_Love_You.jar -appclass frame.TestMain -name biaobai520

javapackager -deploy -native image -outdir outdir -outfile outfiles -srcfiles I_Love_You.jar -appclass frame.TestMain -name biaobai520

vue-simple-uploader

应用 vue-simple-uploader 上传文件和文件夹_Helloweba
https://www.helloweba.net/jav…

基于 vue-simple-uploader 封装文件分片上传、秒传及断点续传的全局上传插件 – 夏巨匠 \
https://www.cnblogs.com/xiahj…

vue-simple-uploader 组件的应用感触 – 简书
https://www.jianshu.com/p/da8…

SpringBoot+Vue.js 前后端拆散实现大文件分块上传

https://github.com/LuoLiangDS…

SpringBoot+Vue.js 前后端拆散实现大文件分块上传 | Fantasy
https://luoliangdsga.github.i…

基于 SpringBoot 和 WebUploader 实现大文件分块上传. 断点续传. 秒传 - 阿里云开发者社区
https://developer.aliyun.com/…

hacker-and-painter/springboot-file-uploader: 🗂️ SpringBoot 实现文件上传零碎
https://github.com/hacker-and…

正文完
 0