写在前面,其实我个人是很不喜欢用mybatis-plus的,因为我很讨厌在代码里面写sql,或者是用链式编程写sql逻辑,因为很不直观,但是新公司新项目里面用了,也只能硬着头皮用了
今天碰到一个很奇怪的问题,本地idea启动运行完全没问题,maven打完包之后启动就一直报空指针异常,找了一下午,简直要崩溃:
Caused by: java.lang.NullPointerException: Cannot invoke "String.split(String)" because "v1" is null
at com.github.yulichang.toolkit.VersionUtils.compare(VersionUtils.java:12)
at com.github.yulichang.injector.MPJSqlInjector.getJoinMethod(MPJSqlInjector.java:117)
at com.github.yulichang.injector.MPJSqlInjector.methodFilter(MPJSqlInjector.java:111)
at com.github.yulichang.injector.MPJSqlInjector.getMethodList(MPJSqlInjector.java:93)
at com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector.getMethodList(DataScopeSqlInjector.java:24)
at com.baomidou.mybatisplus.core.injector.AbstractSqlInjector.inspectInject(AbstractSqlInjector.java:49)
at com.github.yulichang.injector.MPJSqlInjector.inspectInject(MPJSqlInjector.java:168)
at com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.parserInjector(MybatisMapperAnnotationBuilder.java:133)
at com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.parse(MybatisMapperAnnotationBuilder.java:123)
at com.baomidou.mybatisplus.core.MybatisMapperRegistry.addMapper(MybatisMapperRegistry.java:95)
at com.baomidou.mybatisplus.core.MybatisConfiguration.addMapper(MybatisConfiguration.java:129)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.bindMapperForNamespace(XMLMapperBuilder.java:445)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:102)
at com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.buildSqlSessionFactory(MybatisSqlSessionFactoryBean.java:570)
... 75 common frames omitted
具体的代码也就是mp源码com.github.yulichang.toolkit.VersionUtils的这里:
public static int compare(String v1, String v2) {
String[] v1s = v1.split("\\.");
String[] v2s = v2.split("\\.");
String[] vs = v1s.length > v2s.length ? v2s : v1s;
for (int i = 0; i < vs.length; i++) {
int compareV = compareV(v1s[i], v2s[i]);
if (compareV != 0) {
return compareV;
}
}
if (v1s.length == v2s.length) {
return 0;
}
return v1s.length > v2s.length ? 1 : -1;
}
调用方在这里:com.github.yulichang.injector.MPJSqlInjector
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
if (VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.4.3.2") >= 0) {
throw ExceptionUtils.mpe("DefaultSqlInjector 的 getMethodList(Class<?> mapperClass) 方法已在 3.4.3.2+ 改为" +
"getMethodList(Class<?> mapperClass, TableInfo tableInfo)\n");
}
if (Objects.nonNull(sqlInjector)) {
List<AbstractMethod> methodList = AbstractMethodV3431.getMethod(sqlInjector, mapperClass);
return methodFilter(methodList);
} else {
List<AbstractMethod> list = Stream.of(
new Insert(),
new DeleteByMap(),
new DeleteById(),
new DeleteBatchByIds(),
new UpdateById(),
new SelectById(),
new SelectBatchByIds(),
new SelectByMap()
).collect(toList());
list.addAll(getWrapperMethod());
list.addAll(getJoinMethod());
return list;
}
}
也就是说MybatisPlusVersion.getVersion()返回的是null,进去这个方法里面去看:
public static String getVersion() {
return determineSpringBootVersion();
}
private static String determineSpringBootVersion() {
final Package pkg = MybatisPlusVersion.class.getPackage();
if (pkg != null && pkg.getImplementationVersion() != null) {
return pkg.getImplementationVersion();
}
CodeSource codeSource = MybatisPlusVersion.class.getProtectionDomain().getCodeSource();
if (codeSource == null) {
return null;
}
URL codeSourceLocation = codeSource.getLocation();
try {
URLConnection connection = codeSourceLocation.openConnection();
if (connection instanceof JarURLConnection) {
return getImplementationVersion(((JarURLConnection) connection).getJarFile());
}
try (JarFile jarFile = new JarFile(new File(codeSourceLocation.toURI()))) {
return getImplementationVersion(jarFile);
}
} catch (Exception ex) {
return null;
}
}
private static String getImplementationVersion(JarFile jarFile) throws IOException {
return jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
}
也就是最终会调用private static String getImplementationVersion(JarFile jarFile)这个方法,刚开始没注意,以为这个就是正常的读取jar里面的内容,于是各种百度,AI大模型,在偶然的情况下改了一下pom文件里面的打包配置,也就是加了下面这个插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
莫名奇妙就好了(原本是没有加这个插件的,并且我的项目是java代码和scala代码混在一个项目里面写的),后面问了一下chatgpt这个getImplementationVersion是干嘛的,它是这么说的:
于是点进去源码里面看,是jdk源码里面读取jar的,最核心的代码就在这里了,图中标红的地方相信大家都不陌生,用maven编译打包都会生成这个文件:
这个文件的内容如下:
Manifest-Version: 1.0
Created-By: Apache Maven 3.9.2
Built-By: Administrator
Build-Jdk: 17.0.9
Implementation-Version: 5.3.0
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.das.geo.GeotrellisApplication
Spring-Boot-Version: 2.7.18
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
里面的Implementation-Version也正是pom文件里面配置的maven-jar-plugin这个插件配置的:
下面是最终完整的插件配置:搞不懂为什么mybatis-plus要读这个配置,坑了我一下午
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.5.4</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.github.therapi</groupId>
<artifactId>therapi-runtime-javadoc-scribe</artifactId>
<version>0.15.0</version>
</path>
</annotationProcessorPaths>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<finalName>${project.build.finalName}</finalName> <!-- 指定生成的 JAR 文件名 -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>