baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.46k stars 4.31k forks source link

Springboot3原生编译后运行,包含中文字符的字段报超长的错误 #5774

Closed dwj1979 closed 1 year ago

dwj1979 commented 1 year ago

当前使用版本(必填,否则不予处理)

Springboot: 3.1.5 mybatis-plus-spring-boot3-starter:3.5.4-SNAPSHOT OpenJDK Runtime Environment GraalVM CE 22.3.3 (build 17.0.8+7-jvmci-22.3-b22) Windows10 VS 2022

该问题是如何引起的?(确定最新版也有问题再提!!!)

下载示例工程: https://github.com/nieqiurong/mybatis-native-demo 将pom中的数据库依赖换成msyql如下:

<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
</dependency>

重现步骤(如果有就写完整)

进行原生编译:mvn install -Pnative clean native:compile 执行编译后的exe文件

报错信息

由于create_user、update_user字段包含中文字符,执行插入或更新报超长的错误。如果改成英文就没问题,不编译原生普通模式执行也没问题。

Message[id=2, message=Hello World! on runWithXmlMapper] ------------演示insert----------------- 2023-11-11T21:12:24.312+08:00 INFO 35652 --- [ main] c.e.nativedemo.MyMetaObjectHandler : 进入insertFill填充了 2023-11-11T21:12:24.312+08:00 DEBUG 35652 --- [ main] c.e.n.mapper.MessagesMapper.insert : ==> Preparing: INSERT INTO messages ( message, message_type, create_user ) VALUES ( ?, ?, ? ) 2023-11-11T21:12:24.312+08:00 DEBUG 35652 --- [ main] c.e.n.mapper.MessagesMapper.insert : ==> Parameters: Hello MybatisPlus(String), voice(String), 聂秋秋(String) 2023-11-11T21:12:24.314+08:00 INFO 35652 --- [ main] o.s.jdbc.support.SQLErrorCodesFactory : Default sql-error-codes.xml not found (should be included in spring-jdbc jar) 2023-11-11T21:12:24.314+08:00 ERROR 35652 --- [ main] o.s.boot.SpringApplication : Application run failed

java.lang.IllegalStateException: Failed to execute ApplicationRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:765) ~[mybatis-native-demo.exe:3.1.5] at org.springframework.boot.SpringApplication.lambda$callRunners$3(SpringApplication.java:752) ~[mybatis-native-demo.exe:3.1.5] at java.base@17.0.8/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[na:na] at java.base@17.0.8/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357) ~[na:na] at java.base@17.0.8/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510) ~[mybatis-native-demo.exe:na] at java.base@17.0.8/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[mybatis-native-demo.exe:na] at java.base@17.0.8/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[mybatis-native-demo.exe:na] at java.base@17.0.8/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[na:na] at java.base@17.0.8/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[mybatis-native-demo.exe:na] at java.base@17.0.8/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[mybatis-native-demo.exe:na] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:750) ~[mybatis-native-demo.exe:3.1.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:323) ~[mybatis-native-demo.exe:3.1.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[mybatis-native-demo.exe:3.1.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[mybatis-native-demo.exe:3.1.5] at com.example.nativedemo.NativeDemoApplication.main(NativeDemoApplication.java:23) ~[mybatis-native-demo.exe:na] Caused by: org.springframework.dao.DataIntegrityViolationException:

Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect string value: '\xE8\x81\x82\xE7\xA7\x8B...' for column larkxr_new.messages.create_user at row 1

The error may exist in com/example/nativedemo/mapper/MessagesMapper.java (best guess)

The error may involve com.example.nativedemo.mapper.MessagesMapper.insert-Inline

The error occurred while setting parameters

SQL: INSERT INTO messages ( message, message_type, create_user ) VALUES ( ?, ?, ? )

Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect string value: '\xE8\x81\x82\xE7\xA7\x8B...' for column larkxr_new.messages.create_user at row 1

; Data truncation: Incorrect string value: '\xE8\x81\x82\xE7\xA7\x8B...' for column larkxr_new.messages.create_user at row 1 at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:110) ~[na:na] at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[mybatis-native-demo.exe:6.0.13] at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82) ~[mybatis-native-demo.exe:6.0.13] at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82) ~[mybatis-native-demo.exe:6.0.13] at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92) ~[na:na] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:439) ~[na:na] at jdk.proxy4/jdk.proxy4.$Proxy117.insert(Unknown Source) ~[na:na] at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272) ~[mybatis-native-demo.exe:3.0.2] at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59) ~[na:na] at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:152) ~[na:na] at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) ~[na:na] at jdk.proxy4/jdk.proxy4.$Proxy44.insert(Unknown Source) ~[na:na] at com.example.nativedemo.NativeDemoApplication.lambda$runWithMybatisPlus$2(NativeDemoApplication.java:52) ~[mybatis-native-demo.exe:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762) ~[mybatis-native-demo.exe:3.1.5] ... 14 common frames omitted Caused by: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect string value: '\xE8\x81\x82\xE7\xA7\x8B...' for column larkxr_new.messages.create_user at row 1 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104) ~[na:na] at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mybatis-native-demo.exe:8.0.33] at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:354) ~[mybatis-native-demo.exe:8.0.33] at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44) ~[mybatis-native-demo.exe:na] at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java) ~[na:na] at java.base@17.0.8/java.lang.reflect.Method.invoke(Method.java:568) ~[mybatis-native-demo.exe:na] at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:58) ~[na:na] at jdk.proxy2/jdk.proxy2.$Proxy115.execute(Unknown Source) ~[na:na] at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:48) ~[na:na] at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:75) ~[mybatis-native-demo.exe:3.5.13] at java.base@17.0.8/java.lang.reflect.Method.invoke(Method.java:568) ~[mybatis-native-demo.exe:na] at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[na:na] at jdk.proxy4/jdk.proxy4.$Proxy47.update(Unknown Source) ~[na:na] at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50) ~[na:na] at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) ~[mybatis-native-demo.exe:3.5.13] at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) ~[na:na] at java.base@17.0.8/java.lang.reflect.Method.invoke(Method.java:568) ~[mybatis-native-demo.exe:na] at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49) ~[mybatis-native-demo.exe:3.5.13] at com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:106) ~[mybatis-native-demo.exe:3.5.4-SNAPSHOT] at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:59) ~[na:na] at jdk.proxy4/jdk.proxy4.$Proxy48.update(Unknown Source) ~[na:na] at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197) ~[na:na] at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184) ~[na:na] at java.base@17.0.8/java.lang.reflect.Method.invoke(Method.java:568) ~[mybatis-native-demo.exe:na] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:425) ~[na:na] ... 22 common frames omitted

2023-11-11T21:12:24.320+08:00 INFO 35652 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2023-11-11T21:12:24.320+08:00 INFO 35652 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

dwj1979 commented 1 year ago

mysql数据库使用的字符集为utf8mb4

dwj1979 commented 1 year ago

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.0.55:3306/mybatis_test?useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456

Jick-study commented 1 year ago

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.0.55:3306/mybatis_test?useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456

老铁,别发ip 账号 密码。。。

即使提供的账号和密码是假的,但是如果让你们公司扫描到你在公共的地方发这些信息,大概率会被警告~

dwj1979 commented 1 year ago

账号和密码我修改过了,都是假的,只是个示意

halozhy commented 1 year ago

试试给 pom.xml 里面的 build->plugins->plugin->org.graalvm.buildtools->configuration->buildArgs 加入这两行参数

<buildArg>-Dfile.encoding=UTF-8</buildArg>
<buildArg>-H:+AddAllCharsets</buildArg>
dwj1979 commented 1 year ago

试试给 pom.xml 里面的 build->plugins->plugin->org.graalvm.buildtools->configuration->buildArgs 加入这两行参数

<buildArg>-Dfile.encoding=UTF-8</buildArg>
<buildArg>-H:+AddAllCharsets</buildArg>

非常感谢非常感谢!OK了

dwj1979 commented 1 year ago

另外问一下,执行原生编译的时候,出现以下错误: [INFO] Executing: D:\Applications\Java\graalvm-ce-java17-22.3.3\bin\native-image.cmd @target\tmp\native-image-8882161315876282256.args Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <> at index 3: D:\Projects\mybatis-native-demo\target\classes at java.base@17.0.8/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at java.base@17.0.8/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base@17.0.8/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base@17.0.8/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) at java.base@17.0.8/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232) at java.base@17.0.8/java.nio.file.Path.of(Path.java:147) at java.base@17.0.8/java.nio.file.Paths.get(Paths.java:69) at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.ClasspathUtils.stringToClasspath(ClasspathUtils.java:52) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.addCustomImageClasspath(NativeImage.java:1582) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.processClasspathArgs(DefaultOptionHandler.java:417) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.consume(DefaultOptionHandler.java:69) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage$NativeImageArgsProcessor.apply(NativeImage.java:1512) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.consume(DefaultOptionHandler.java:204) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage$NativeImageArgsProcessor.apply(NativeImage.java:1512) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.processNativeImageArgs(NativeImage.java:1770) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.completeImageBuild(NativeImage.java:959) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.build(NativeImage.java:1417) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1387) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.main(NativeImage.java:1374) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 14.850 s [INFO] Finished at: 2023-11-14T08:51:03+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.27:compile (default-cli) on project mybatis-native-demo: Execution of D:\Applications\Java\graalvm-ce-java17-22.3.3\bin\native-image.cmd @target\tmp\nativ e-image-8882161315876282256.args returned non-zero result -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

然后将target\tmp\native-image-8882161315876282256.args文件中的双反斜杠\,替换成单反斜杆/,再执行下面的命令,才能正常编译。有其他好的解决办法吗 D:\Projects\mybatis-native-demo>native-image.cmd @target\tmp\native-image-8882161315876282256.args

halozhy commented 1 year ago

另外问一下,执行原生编译的时候,出现以下错误: [INFO] Executing: D:\Applications\Java\graalvm-ce-java17-22.3.3\bin\native-image.cmd @target\tmp\native-image-8882161315876282256.args Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <> at index 3: D:\Projects\mybatis-native-demo\target\classes at java.base@17.0.8/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at java.base@17.0.8/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base@17.0.8/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base@17.0.8/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) at java.base@17.0.8/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232) at java.base@17.0.8/java.nio.file.Path.of(Path.java:147) at java.base@17.0.8/java.nio.file.Paths.get(Paths.java:69) at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.ClasspathUtils.stringToClasspath(ClasspathUtils.java:52) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.addCustomImageClasspath(NativeImage.java:1582) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.processClasspathArgs(DefaultOptionHandler.java:417) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.consume(DefaultOptionHandler.java:69) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage$NativeImageArgsProcessor.apply(NativeImage.java:1512) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.DefaultOptionHandler.consume(DefaultOptionHandler.java:204) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage$NativeImageArgsProcessor.apply(NativeImage.java:1512) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.processNativeImageArgs(NativeImage.java:1770) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.completeImageBuild(NativeImage.java:959) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.build(NativeImage.java:1417) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1387) at org.graalvm.nativeimage.driver/com.oracle.svm.driver.NativeImage.main(NativeImage.java:1374) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 14.850 s [INFO] Finished at: 2023-11-14T08:51:03+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.27:compile (default-cli) on project mybatis-native-demo: Execution of D:\Applications\Java\graalvm-ce-java17-22.3.3\bin\native-image.cmd @target\tmp\nativ e-image-8882161315876282256.args returned non-zero result -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

然后将target\tmp\native-image-8882161315876282256.args文件中的双反斜杠\,替换成单反斜杆/,再执行下面的命令,才能正常编译。有其他好的解决办法吗 D:\Projects\mybatis-native-demo>native-image.cmd @target\tmp\native-image-8882161315876282256.args

我没遇到过这个问题,不过根据 https://github.com/oracle/graal/issues/6648#issuecomment-1556762587 ,升级 graalvm jdk 可能有用