問題描述:
在springBoot 中爲了維戶開發方便,將***Mapper.java 和*** Mapper.xml 放在同一目錄下,由於沒有配置好,結果運行時報了
MalformedByteSequenceException: 2 字節的 UTF-8 序列的字節 2 無效。
ERROR: nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字節的 UTF-8 序列的字節 2 無效。
解決辦法:
1、網上說是由於文件編碼不對 引起的,我用了笨方法,將所有文件(*.java *.xml *.yml 等等)都另存爲uft-8 但還是一樣報錯
2、網上說打包時pom.xml 加上-Dfile.encoding=utf-8 如下
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!-- spring-boot:run 中文亂碼解決 -->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
但還是無效
3、最終經過排查,是由於 mapper-locations 配置引起的。
錯誤的配置:mapper-locations: classpath:com/qen/dao/**/*
正確的配置:mapper-locations: classpath:com/qen/dao/**/*.xml
沒有看源代碼,猜測mapper-locations: 加載文件如果解析不出來 就報錯,因爲 ***Mapper.class 是二進制文件,肯定讀不出來
希望能幫到您!