本文章是记录自己遇到的错误,总结错误,今后再遇到错误也可以回头参考一下,本文会持续更新

JavaSE

程序包javax.servlet不存在

解决方法

引入javax.servlet包,在maven的pom文件添加如下

1
2
3
4
5
6
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

Log4j报错

1
2
3
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

log4j.pro20221217-097

解决方法

在pom.xml添加slf4j的依赖即可

1
2
3
4
5
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

java: 错误: 不支持发行版本 5

错误展示:

01-错误内容-20230130-993

解决方法

将项目设置的JDK同步语言级别。

1. 打开项目结构

02-项目结构20230130-025

2. 将语言级别改为JDK版本

03-项目结构20230130-862

3. 项目字节版本和目标字节版本改为JDK版本

04-设置20230130-086

不再支持源选项 5。请使用 7 或更高版本

Maven构建失败

Failed to execute goal org.apache.maven.plugins

1
2
3
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project lab11: Compilation failure: Compilation failure:  
[ERROR] 不再支持源选项 5。请使用 7 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 7 或更高版本。

解决办法

pom.xml添加插件maven-compiler-plugin就可以解决

1
2
3
4
5
6
7
8
9
10
11
12
13
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

不再支持源选项 5。请使用 6 或更高版本

1
2
3
[ERROR] 不再支持源选项 5。请使用 6 或更高版本。
[ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
[INFO] 2 errors

解决方法一

按照上面错误的java: 错误: 不支持发行版本 5设置好对应的JDK版本,再在pom.xml添加如下设置即可,设置JDK版本为11,JDk版本根据自己的情况设置。

1
2
3
4
5
6
7
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

或者下面这个都可以

1
2
3
4
5
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

解决方法二

设置当前项目结构JDK版本

文件-->项目-->SDK设置JDK版本大于报错的版本,比如设置JDK11

然后再设置语言级别,也是在当前页面下,在SDK下方,设置语言别对应的JDK版本,比如我刚刚设置了JDK11,那语言级别也要设置11

设置全局JDK版本

文件-->设置-->构建、执行、部署-->编译器-->Java编译器-->项目字节码版本设置版本为11。在该位置的下方的模块,设置目标字节码版本为11

总之,就是你设置的JDK版本要和字节码对应版本号

java: 警告: 源发行版 17 需要目标发行版 17

原因

SringBoot版本从3.0开始默认依赖JDK是JDK17,我使用的是JDK11,因此出现了错误。

解决方法

  1. 使用JDK17以上
  2. 修改pom.xml的 SpringBoot版本为2.x版本

Maven识别失败

Maven图标没有识别为蓝色

选中pom.xml文件

右键添加为Maven项目

等待项目构建结束就可以了

Spring、SpringMVC

SpringMVC运行报错没有刷新

1
2
3
4
Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@6fd02e5 has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1096)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
at site.hikki.App.main(App.java:12)

解决方法

在注册配置之后刷新一下Ioc容器,再获取bean就没问题了。

1
2
3
4
5
6
7
8
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(SpringConfig.class);
ctx.refresh();
System.out.println(ctx.getBean(StudentController.class));
}
}

运行Spring项目出错

Unsupported class file major version 61

ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn’t supported yet

1
2
3
4
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\Cache\Java\lab-2\lab07\target\test-classes\cn\edu\seig\JdbcTest.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [E:\Cache\Java\lab-2\lab07\target\test-classes\cn\edu\seig\JdbcTest.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61

ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [E:\Cache\Java\lab-2\lab07\target\test-classes\cn\edu\seig\JdbcTest.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61

解决方法

网上寻找一番大概是JDK和Spring版本不对应。我用的是jdk17,我用是5.2.10.RELEASE版本不行,一直构建失败,换了5.3.18就行了。

将spring的依赖改为5.3.18就好了

Maven使用clean命令出错

报错内容

It is highly recommended to fix these problems because they threaten the stability of your build.
For this reason, future Maven versions might no longer support building such malformed projects.

翻译:

强烈建议修复这些问题,因为它们会威胁构建的稳定性。因此,未来的Maven版本可能不再支持构建这种畸形的项目。

解决方法

将pom.xml文件下的maven插件删除就好了。

01-erroe-lab20230326-011

out.print报错 && out.没有提示

out.print报错是因为没有找到servlet的包,将包导入就可以解决问题,out需要jsp.jarservlet-api.jar包,将这两个包导入即可。

01报错内容

解决方法一

maven导入jar包,在pom.xml文件添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

打开pom.xml文件

11打开pom文件

添加内容

12添加到pom

重新加载项目jar包

13加载pom

解决方法二

将本机的tomcat下的bin的jsp.jarservlet-api.jar包导入到idea的项目中

打开tomcat

02将tomcat下的bin文件复制到idea

在WEB-INF新建一个lib文件夹

把包放进去

03文件存放路径

添加lib为一个库

04添加为库

确认

05确认

添加库成功

添加为库成功的话,包是可以展开看到包的内容的。

06添加库成功

错误’xxx‘不是一个有效的JRE主目录

报错显示

01-错误显示-运行调试配置20221107-533

解决方法

原因是我们选择JRE的路径不对,因为我们的JDK是包含有JRE的,所以我们将JRE的路径选择为我们的JDK的根目录即可解决这个问题。

02-运行调试配置20221107-616

Could not open ServletContext resource XXX

Web项目启动后就报如下错误

报错内容

1
2
3
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/druid/druid.properties]

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/druid/druid.properties]

根据报错内容可以看到是没找到/druid/druid.propertie这个文件,我们找一下哪里有引用这个文件,找到这个调用位置,可能是写写错了路径。

解决问题

我们在bean.xml配置了错误,在Web项目中,文件的调用位置都不太一样,不能像以往在Test.java的位置调用一样,在Web项目中,需要添加classpath:,比如以下的错误修改为:

1
context:property-placeholder location="druid/druid.properties"/>

修正后:

需要加上classpath:

1
<context:property-placeholder location="classpath:druid/druid.properties"/>

请求的资源[xxx]不可用

错误展示

出现该错误的情况有非常多,以下的方式只适合参考,不一定适合你

HTTP状态 404 - 未找到 — Mozilla Firefox20221113-118

导包错误

我在导包的时候,手快导入了import jakarta.servlet.annotation.WebServlet;这个包,实际上导入这个包是错误的,并不能正确使用WebServlet,实际上应该导入是import javax.servlet.annotation.WebServlet;这个包

02-错误的包-lab10 – LoginServlet.java20221113-166

修改正确

导入import javax.servlet.annotation.WebServlet;就可以正确运行了

03-正确的包-lab10 – LoginServlet.java20221113-417

生产工具

隐藏IDEA项目中的文件或文件夹

效果

如下图,我隐藏文件后的效果,原本的Java项目下一班都会有.mvn、HELP.md、.idea等待文件夹或文件,我们可以删除它,但也可以将它隐藏起来,但最好的方法还是隐藏起来,我们看不见就好。

00-样式-20230411-421

方法

文件–>设置–>文件类型–>忽略的文件和文件夹–>点击+号–>添加要隐藏的文件名

*:表示匹配全部

*.md表示隐藏以.md文件结尾的全部文件或文件夹

01-添加文件后缀-20230411-488

Intellij乱码

方法一

打开设置

打开设置设置文件编码为utf-8

打开编辑器–>文件编码

把编码改为UTF-8或者其他

方法二

打开帮助

打开打开自定义VM选项

设置虚拟机加载为utf-8

添加一条语句在配置下面

1
-Dfile.encoding=UTF-8

IDEA打开失败

Internal error. Please refer to https://jb.gg/ide/critical-startup-errors

正常打开IDEA就出现以下错误,昨天还能正常使用,重新启动了几次,也无法解决问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Internal error. Please refer to https://jb.gg/ide/critical-startup-errors

com.intellij.ide.plugins.StartupAbortedException: Cannot start app
at com.intellij.idea.StartupUtil.lambda$start$15(StartupUtil.java:265)
at java.base/java.util.concurrent.CompletableFuture.uniExceptionally(CompletableFuture.java:990)
at java.base/java.util.concurrent.CompletableFuture$UniExceptionally.tryFire(CompletableFuture.java:974)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
Caused by: java.net.BindException: Address already in use: bind
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:555)
at java.base/sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:337)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:294)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:562)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:506)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:491)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:260)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:356)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)

-----
Your JRE: 17.0.3+7-b469.37 amd64 (JetBrains s.r.o.)
G:\studyAPP\IDEA\IntelliJ IDEA 2022.2.1\jbr

Start Failed20221123-641

分析问题

分析了一下报错内容,端口被占用,无法启动,可能idea打开时需要的某些端口已经被其他程序占用了,导致无法启动,既然这样,把那些占用的端口接触就好,或者把idea需要的端口改一下,思路确实很简单,但还有个更简单粗暴的方法,重启大法,哈哈哈,重启后。所有端口都会重新分配,如果idea是最先使用端口,那就不存在idea需要的端口被占用了。

网上搜了一下解决方法,在idea社区看到某位大佬的评论,大概意思是说:idea在启动时会绑定本地服务器的6942-6991之间可用的任一端口,如果idea无法绑定到该端口,则会抛出异常。这段范围的端口几乎不可能全部都被使用,唯一可能的就是某些网络安全不允许该范围的端口的使用,被保护了,即使在主机使用也不允许。

这让我想起来,我上一次使用使用class for window代理端口为0不能这个正常使用也有一定关联,上一次也是因为class也需要绑定某个端口,但这个端口被安全保护,不能被使用,最后修改class需要的端口,在这里,其实也类似,要么修改端口保护范围,要么修改idea使用的端口范围,或者重启电脑,重启后电脑的安全端口似乎会动态改变,这样可能idea的端口使用范围就可以使用,或者真的是这全部的端口范围都是占用了,但这几率不大。

Critical Internal Error on Startup of IntelliJ IDEA Cannot Lock System Folders – IDEs Support (IntelliJ Platform)  JetBrains — Mozilla Firefox20221123-570

解决方法

重启电脑

重启电脑后,我的idea可以正常使用了。如果你的idea还是无法使用,可以参考以下的方法。

修改安全保护端口范围

查看tcp协议端口排除范围,看看是否存在6942-6991的范围,可能这段端口范围在安全保护范围内,那就需要修改安全保护端口范围,或者重启netsh应该也是可以的,重启后会动态改变安全保护端口范围

1
netsh interface ipv4 show excludedportrange protocol=tcp

Windows PowerShell20221123-811

参考资料

issuecomment-459205576

Internal-Error-on-Startup-of-IntelliJ-IDEA-Cannot-Lock-System-Folders

数据库

URL缺少时区

url加上?serverTimezone=UTC这一句,如下

1
String url = "jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC";

SSL安全提示

在连接数据库的url加上&verifyServerCertificate=false&useSSL=false,如下

1
url=jdbc:mysql://localhost:3306/yootk?useUnicode=true&characterEncoding=utf-8&verifyServerCertificate=false&useSSL=false

URL填写错误

报错内容

可能是连接数据库链接出错

1
Caused by: java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key host

改正URL

1
url = jdbc:mysql://localhost:3306/yootk?useUnicode=true&characterEncoding=utf-8

中文乱码

在连接数据库的URL加上useUnicode=true&characterEncoding=utf-8,如下

1
url = jdbc:mysql://localhost:3306/yootk?useUnicode=true&characterEncoding=utf-8

Spring整合mybatis连接不上

Spring-mybatis-error-s20230111-078

解决方法

{$jdbc.password}改一下就好了${jdbc.password}

02-Spring-mybatis-sussce-s20230111-078

Spring mapper出错

No qualifying bean of type ‘site.hikki.dao.BookDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

大概意思就是没有找到site.hikki.dao.BookDao的bean,没有可用的。

解决方法

public interface BookDao {}这个接口不需要添加@Repository的,应该是在MybatisConfig.java中添加bean的。如下:

1
2
3
4
5
6
7
@Bean //重点添加bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
// 设置mybatis的 xml sql语句
configurer.setBasePackage("site.hikki.dao");
return configurer;
}

启动MySQL拒绝服务

错误如下

01拒绝服务

解决方法

可能是某个安全软件禁止了MySQL自启动,关闭就好了,改为允许启动

02关闭火绒禁止mysql自启动

安装MySQL出错

报错内容大概如下

1
2
请键入 NET HELPMSG 2185 以获得更多的帮助。
请键入 NET HELPMSG 3534 以获得更多的帮助。

01-安装MySQL时遇到的Initializing-database-20230301-145

02-错误-20230301-979

03-错误2-20230301-494

解决方法

进入MySQL的安装位置

MySQL的默认安装位置:C:\Program Files\MySQL\MySQL Server 8.0\bin

使用管理员权限打开CMD,用PowerShell也可以。

加载MySQL服务

1
.\mysqld.exe --install

04-加载MySQL服务-20230301-758

初始化用户

1
2
.\mysqld.exe --initialize --user=mysql --console
net start mysql

05-初始化用户-20230301-267

参考文章:

如何解决window中mysql服务无法启动的问题

连接MySQL出错

报错内容:Access denied for user ‘root’@‘localhost’ (using password: YES) Access denied for user ‘root’@‘localhost’ (using password: YES)

image-20230302002036789

在IDEA中出现该错误

如果你是在IDEA连接数据库发生该错误如下:

1
Access denied for user 'lilbai'@'localhost' (using password: YES) Access denied for user 'root'@'localhost' (using password: YES)

其中lilbai是你的电脑用户名,该错误很简单,你如果是spring或者springboot的,就是jdbc.properties设置的数据库连接用户名为username,Java在调用该变量时,引用的值不是你写的值,而是你电脑系统环境变量里面的username值。

简单来说,就是Java里面设置的usernmae变量和系统环境变量的username冲突了,而系统的环境变量优先级更高,导致在连接数据库时调用系统的环境变量,而没有调用你Java里面的环境变量。

解决方法:

jdbc.propertiesusername改为jdbc.username就行,建议以后都用jdbc.username这种方式去命名,防止环境变量引起的错误。

解决方法

关闭mysql服务

打开CMD或者PowerShell窗口

1
net stop mysql

01-关闭MySQL-20230302-673

跳过权限验证

1
mysqld --console --skip-grant-tables --shared-memory

运行该指令如果出错爆红,可能是你的mysqld没有设置环境变量,可以去环境变量的Path添加一个C:\Program Files\MySQL\MySQL Server 8.0\bin就可以了。

02-跳过权限验证-20230302-851

运行该指令不要关闭该窗口或者停止该指令的运行。

进入mysql修改远程密码

1
2
3
4
mysql
flush privileges;
alter user root@localhost identified by 'root';
quit

打开一个新的CMD或者PowerShell窗口,刚刚的窗口不要关闭

03-进入mysql修改远程密码-20230302-542

关闭权限验证

打开刚刚运行mysqld --console --skip-grant-tables --shared-memory指令的窗口,Ctrl + C停止该任务,或者关闭该窗口也行。

启动mysql

1
net start mysql

04-启动MySQL-20230302-211

启动成功之后,就可以连接数据库了。

【MYSQL8 已解决】Access denied for user ‘root‘@‘localhost‘ (using password: YES)问题

MySql错误 1251 - Client does not support authentication protocol requested by server 解决方案

连接MySQL时出现:ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)-解决办法

DBeaver连接mysql提示安全出错

错误显示:**Public Key Retrieval is not allowed Public Key Retrieval is not allowed **

解决方法

在数据库后面按加上如下内容即可:

1
mysql?allowPublicKeyRetrieval=true

连接 localhost 配置20230302-950

文章参考:

连接mysql时报错Public Key Retrieval is not allowed的解决方法

Invalid bound statement (not found)

我使用的SpringBoot集成mybatis,出现了错误,以下方法仅供参考。

可能是以下三种错误:

  1. 语法错误
  2. 编译错误
  3. 配置错误

语法错误

需要到Ddao层的接口添加@mapper注解

1
2
3
4
5
6
7
import org.apache.ibatis.annotations.Mapper;
import site.hikki.pojo.Book;

@Mapper
public interface BookDaoXML {
Book getBookById(Integer id);
}

BookMapper.xml文件

  1. Dao的形参和xml的参数类型是否一致

  2. SQL返回类型是否符合查询的结果集类型

  3. Dao的方法名是否和xml的id一致

1
2
3
4
5
6
7
8
9
?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="site.hikki.dao.BookDaoXML">
<select id="getBookById" parameterType="Integer" resultType="Book">
select * from tb_book where id=#{id}
</select>
</mapper>

编译错误

我的mapper.xml存放在resource/mapper/文件夹下,需要做资源过滤,不然SpringBoot就会拦截,导致无法找到该文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>

配置错误

我们使用SpringBoot集成mybatis时,必须指定实体类的包名,否者你在编写xml时,实体类需要使用实体类的绝对路径。

我们在application.properties配置mybatis的相关配置。

1
2
3
4
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=site.hikki.pojo
# 开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true

Cannot determine value type from string ‘xxx’

错误内容

1
2
org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'category_name' from result set.  Cause: java.sql.SQLDataException: Cannot determine value type from string '生活'
; Cannot determine value type from string '生活'; nested exception is java.sql.SQLDataException: Cannot determine value type from string '生活'

或者出现这个错误也是同一个问题造成Index 1 out of bounds for length 1

分析

从问题抛出来看,大概内容可以看出来是结果映射有问题,可能是mybatis接收的类型和数据库的类型没对应上,可以先去看一下。

这是我写的Category查询:

1
2
3
4
5
6
7
8
9
10
public Integer selectCategoryByName(String categoryName) {
System.out.println(categoryName);
// 查询分类ID
Category category = categoryMapper.selectOne(new QueryWrapper<Category>()
.select("category_name")
.eq("category_name", categoryName));

// 返回分类ID,可能不存在分类ID
return null!=category?category.getId():null;
}

查看Category实体类,我使用了lombok插件,同时使用了@Date@Builder注解,经上网查找资料,需要给Category实体类添加一个无参构造函数,用于与数据库作数据映射,那就使用lombok插件再添加一个注解就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package site.hikki.model.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
* @author : 小码同学
* Date: 2023/10/24 15:13
* 小码博客 :https://blog.hikki.site
* 小码同学公众号 :小码同学
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Category {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Integer id;

/**
* 分类名
*/
private String categoryName;

/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
}

解决办法

  1. 给实体添加无参构造函数
  2. mapper.xml中查询的数据库字段属性的类型要和有参构造器的字段类型一一匹配;查询字段的个数要和有参构造器个数一样

mybatis异常集之Cannot determine value type from string ‘xxx‘

SpringBoot

SpringBoot启动类注解缺失

Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.

01-Main入口配置类出错-20230430-211

原因

注解配置错了,不应该@SpringBootConfiguration,正确的注解应该是@SpringBootApplication

02-原因-20230430-482

解决

@SpringBootConfiguration注解改为@SpringBootApplication就可以了。

1
2
3
4
5
6
7
8
9
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

细心,还是细心,非常重要

devtools版本不兼容

org/springframework/boot/SpringApplicationAotProcessor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplicationAotProcessor
at org.springframework.boot.devtools.system.DevToolsEnablementDeducer.<clinit>(DevToolsEnablementDeducer.java:41)
at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getInitialUrls(DefaultRestartInitializer.java:39)
at org.springframework.boot.devtools.restart.Restarter.<init>(Restarter.java:140)
at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:534)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:90)
at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:50)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:79)
at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:56)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:56)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:298)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
at site.hikki.App.main(App.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplicationAotProcessor
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 19 more

01-出错内容-20230430-556

原因

这是我的依赖配置,我添加了devtools依赖才报错,大概率原因是依赖不兼容的问题。

1
2
3
4
5
6
7
8
<!--devtools热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<version>3.0.4</version>
<scope>true</scope>
</dependency>

解决

尝试换一个devtools版本,更换掉2.5.4版本就没问题了。

1
2
3
4
5
6
7
8
<!--devtools热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<version>2.5.4</version>
<scope>true</scope>
</dependency>

spring-boot-maven-plugin报错

错误展示

01fail

换成spring-boot-starter-parent相同版本

spring-boot-starter-parent
1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring-boot-maven-plugin
1
2
3
4
5
6
7
8
9
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.4</version>
</plugin>
</plugins>
</build>

错误原因是找不到spring-boot-maven-plugin,让spring-boot-maven-plugin版本和spring-boot-starter-parent相同就可以找到该版本了

02换成相同的版本号

没有设置数据库信息

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

这个错误是在创建SpringBoot时,引入了数据库,但没配置数据库的连接信息,然后启动了SpringBoot项目,于是就报错了。

1
2
3
4
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

解决方法

在启动类中,在@SpringBootApplication()注解上添加exclude= {DataSourceAutoConfiguration.class}忽略数据库连接,这样就不会出错了。

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class Springboot02SsmpApplication {

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

序列化操作失败

1
DefaultSerializer requires a Serializable payload but received an object of type [site.hikki.pojo.Teacher]

原因分析

在Spring Boot中,缓存通常使用缓存管理器来进行操作,而缓存管理器又依赖于序列化操作。因此,如果你的实体类需要被缓存在缓存中,必须实现Serializable接口,以便将对象序列化为字节数组,并存储在缓存中。

同时,使用序列化也对缓存的可靠性和扩展性有很大的帮助,可以方便地将缓存数据移植到其他系统或平台。

所以,在开启缓存@EnableCaching之前,需要确保你的实体类都实现了Serializable接口,这样才能安全地将它们存储在缓存中。

解决

在实体类中实现Serializable接口就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;

/**
* 老师类
*/
@Entity(name = "tb_teacher")
public class Teacher implements Serializable {
@Id
private Integer jobId;//工号
private String teacherName;
private String gender;//性别
private String tel;
private String role;//角色 0:学习导师,1:辅导员
private Integer deptId;

//为属性提供setter和getter方法

}

Maven

Maven拉取依赖失败

Could not find artifact xxx.xxx.xxx:jar:unknown in aliyunmaven (https://maven.aliyun.com/repository/public)

为什么会失败?

错误一:

原因是我们原本设置的依赖仓库是在阿里云仓库,有时候在Maven官方仓库添加了新依赖,阿里云仓库没有及时更新,阿里云仓库自然就没有这个依赖,就会导致拉取阿里云仓库的依赖失败。

有些依赖用得比较少,阿里云可能会考虑成本的原因就没有将这些依赖添加进去,自然也没有依赖,也会导致拉取依赖失败。

总之:就是阿里云没有这个依赖就对了

错误二:

可能是你没有指定版本,源仓库不指导你要的是哪一个版本,就报错了,你指定一下版本可能就可以解决问题了。

如果你是SpringBoot项目,有时候你指定版本和不指定版本都可以正常拉取依赖,这次不指定版本为什么就拉取失败了?

原因是SpringBoot内置了非常多的依赖版本组合,SpringBoot通过组合大量的依赖版本,减少开发者添加不同的依赖版本造成的冲突问题。但这样也会存在一个问题,有些依赖是SpringBoot没有添加的,这时就得开发者自己添加版本了,不然就会报上面这个错误了,表示Maven找不到依赖,这时我们自己添加依赖版本就可以解决问题了。

怎么解决?

阿里云没有这个依赖,那我们就去官方拉取就好了,官方仓库服务器在国外,下载的速度不是很理想,但只是偶尔用一下,我们可以给单独的项目设置Maven官方仓库源。

给项目指定仓库源

放在<project>标签下就好了,重新刷新一下就可以在官方源拉取项目了。

1
2
3
4
5
6
7
8
9
10
<project>
<!-- ... -->
<repositories>
<repository>
<id>apache-maven-repo</id>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<!-- ... -->
</project>