前言

springboot日常使用中有一些兼容性的错误,有时候没遇见过会让人无法摸到头脑,这里记录一些问题,后续不定期补充

1、mysql

1.1 Received fatal alert: protocol_version

因为更换了数据源才发现并记录,之前用的是mariadb8正常,换了docker mysql8就报错,不过为了减少无法定位的错建议采用完整的url连接串。

1
2
3
4
5
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 6 milliseconds ago.  The last packet sent successfully to the server was 5 milliseconds ago.

Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version

原因:
spring.database.url连接串需要增加useSSL=false

1
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&useSSL=false

1.2 Unknown system variable ‘query_cache_size’

因为更换了数据源才发现并记录,之前用的是mariadb8正常,换了docker mysql8就报错记录。

1
Unknown system variable 'query_cache_size'

原因:
连接的数据库mysql是8.*版本, 但是mybatis用的是低于8.*的版本导致, 升级相同版本的包

1
2
3
4
5
6
<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

2、maven

2.1 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0

maven打包编译的时候报这个错,暂没找到具体根因,暂调整版本号可以解决问题

1
Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0

原因: maven的版本包冲突,具体详细原因暂时没查到,后续分析出了原理再更新

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!--在这里修改版本-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </plugin>
        <!---->

    </plugins>
</build>

3、spring cache

spring cache碰到的执行错误,不影响启动

3.1

1
java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.sxt.entity.Teacher com.sxt.service.impl.TeacherServiceImpl.queryById(java.lang.Integer)] caches=[] | key=''member:'+#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='#result == null' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@4ae15abe'. At least one cache should be provided per cache operation.

原因:
@Cacheable缺少cacheNames参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 第一种 在方法的@Cacheable增加cacheNames,如果还有其他方法没添加,需要挨个方法都增加,建议用第二种
public class TestQueryImpl implements TestQuery {

	@Cacheable(cacheNames = {"user"}, key = "#id")
    public User findById(BigInteger id){
        return userMapper.selectByPrimaryKey(id);
    }
}

// 第二种 在类名增加@CacheConfig(cacheNames = "***"),其他方法就不用挨个添加了
@Service
@Cacheable(cacheManager = "redisCacheManager")
@CacheConfig(cacheNames = "user")
public class TestQueryImpl implements TestQuery {

}

4、mybatis

4.1 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

1
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

原因:

  • 1、target\classes下没编译出来*Mapper.xml,用mvn compile下或build下项目即可
  • 2、xxxMapper.xml文件没有按照传统的maven架构进行放置,在pom增加resource, 参考下面示例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
  • 3、代码配置问题
    检查MapperScan
1
@MapperScan({"com.*.mapper"})

检查namesapce, 一般通过点击跳转可以验证是否正确

1
2
3
<mapper namespace="com.**.user.mapper.UserMapper">
......
</mapper>

检查id,一般ide插件可以关联跳转

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
......
</select>

检查mybatis的配置,如果不是标准目录和命名需要细心检查

1
2
3
mybatis:
  mapperLocations: classpath:**/mapper/**/*Mapper.xml
  type-aliases-package: com.**.entity

5、excel

如果excel保存在 resource 目录下,mvn编译时复制到target\class目录下,容易破坏二进制导致poi解析异常

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationException: Could not open the specified zip entry source stream
	at org.apache.poi.openxml4j.opc.ZipPackage.openZipEntrySourceStream(ZipPackage.java:212)
	at org.apache.poi.openxml4j.opc.ZipPackage.openZipEntrySourceStream(ZipPackage.java:194)
	at org.apache.poi.openxml4j.opc.ZipPackage.openZipEntrySourceStream(ZipPackage.java:168)
	at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:149)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:277)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
	at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.readOpcPackage(XlsxSaxAnalyser.java:191)
	at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.<init>(XlsxSaxAnalyser.java:89)
	at com.alibaba.excel.analysis.ExcelAnalyserImpl.choiceExcelExecutor(ExcelAnalyserImpl.java:103)
	at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:55)
	at com.alibaba.excel.ExcelReader.<init>(ExcelReader.java:27)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:202)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:239)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:227)
Caused by: java.io.IOException: ZIP entry size is too large or invalid
	at org.apache.poi.openxml4j.util.ZipArchiveFakeEntry.<init>(ZipArchiveFakeEntry.java:43)
	at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:53)
	at org.apache.poi.openxml4j.opc.ZipPackage.openZipEntrySourceStream(ZipPackage.java:210)

需要在 pom 的 maven-resources-plugin 增加 filter,这样 excel 文件被 mvn 复制到 target\class 时不编译保留原格式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
    <!-- resource 目录下的 xls 被 mvn 复制到 target/class 目录后二进制文件会破坏导致 excel 解析异常,需添加这个过滤 -->
    <configuration>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

to be continue…

6、官方文档

spring cache:https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#cache
spring:https://docs.spring.io/spring-framework/docs/5.1.9.RELEASE/spring-framework-reference/
spring yml核心参数: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties