Source code:

@EnableJpaRepositories
@SpringBootApplication
class NativeMysqlCharacterTestApplication

fun main(args: Array<String>) {
    val application = runApplication<NativeMysqlCharacterTestApplication>(*args)

    val userRepository = application.getBean(UserRepository::class.java)

    val str = (1..5).joinToString(separator = "") { "123456789".random().toString() }
    userRepository.saveAndFlush(
        UserPo(nickname = "测试${str}")
    )

    val userPos = userRepository.findByNicknameLike("%测试%")
    for (userPo in userPos) {
        println(userPo)
    }
}

Code in bytecode:

The following is an intercept, in which you can see the Chinese text: 测试

 0 3 java/lang/Iterable  5   7 java/lang/CharSequence  9 Wcc/shacocloud/nativemysqlcharactertest/NativeMysqlCharacterTestApplicationKt$main$str$1  ; INSTANCE YLcc/shacocloud/nativemysqlcharactertest/NativeMysqlCharacterTestApplicationKt$main$str$1; = >  < ? kotlin/jvm/functions/Function1  A  kotlin/collections/CollectionsKt  C joinToString$default  (Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String; E F
 D G 3cc/shacocloud/nativemysqlcharactertest/model/UserPo  I  测试 K $java/lang/invoke/StringConcatFactory  M makeConcatWithConstants  (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; O P
 N Q R &(Ljava/lang/String;)Ljava/lang/String; O T   U T(Ljava/lang/Long;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V 1 W
 J X saveAndFlush &(Ljava/lang/Object;)Ljava/lang/Object; Z [
 & \ %测试% ^ findByNicknameLike $(Ljava/lang/String;)Ljava/util/List; ` a
 & b java/util/List  d iterator ()Ljava/util/Iterator; f g
 e h java/util/Iterator  j  hasNext ()Z l m
 k n next ()Ljava/lang/Object; p q
 k r java/lang/System  t out Ljava/io/PrintStream; v w     u x java/io/PrintStream  z  println (Ljava/lang/Object;)V | }

The result of the output (I cannot decompile the result of graalvm):

Hibernate: insert into def_user (nickname,id) values (?,?)
Hibernate: select up1_0.id,up1_0.nickname from def_user up1_0 where up1_0.nickname like replace(?,'\\','\\\\')
UserPo(id=1, nickname='娴嬭瘯58659')

I have prepared a demo project for this purpose:https://github.com/Lorwell/native-mysql-character-test

Comment From: Lorwell

After you have cloned the project, you can directly start a mysql first. Here is the command of docker

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:8.0.28 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max-connections=5000

then you can run it directly: gradlew nativeRun, then you will see the output of the console. not as expected in Chinese: 测试, but 娴嬭瘯

Comment From: wilkinsona

There are several moving parts here and I wonder if they're all necessary. Have you tried it without involving MySQL? If the Chinese text becomes garbled during native image compilation, it should be reproducible without involving a database. Furthermore, unless the problem's specific to Kotlin, it should be reproducible in plain Java and removing Kotlin will remove another potential source of an encoding problem.

Please reduce the sample to the bare minimum that's required to reproduce the problem and we can take another look.

Comment From: Lorwell

Thank you for your reminder. I tried to remove mysql. In the no-mysql branch of my demo project, I found that it was not a problem with kotlin or graalvm. Chinese can be printed normally, but this still did not solve my problem. Below I have provided an execution log. What you can see is that it is normal to simply print Chinese characters, and the Chinese characters injected by sql parameters are also normal, but the data written to the library becomes garbled. My current suspicion is that the mysql driver Is this the culprit?

打印中文:测试
测试
添加用户信息:UserPo(id=null, nickname='测试84178')
2024-02-01T10:30:04.653+08:00 DEBUG 11080 --- [           main] org.hibernate.SQL                        : select next_val as id_val from def_user_seq for update
2024-02-01T10:30:04.654+08:00 DEBUG 11080 --- [           main] org.hibernate.SQL                        : update def_user_seq set next_val= ? where next_val=?
2024-02-01T10:30:04.665+08:00 DEBUG 11080 --- [           main] org.hibernate.SQL                        : insert into def_user (nickname,id) values (?,?)
2024-02-01T10:30:04.665+08:00 TRACE 11080 --- [           main] org.hibernate.orm.jdbc.bind              : binding parameter (1:VARCHAR) <- [测试84178]
2024-02-01T10:30:04.665+08:00 TRACE 11080 --- [           main] org.hibernate.orm.jdbc.bind              : binding parameter (2:BIGINT) <- [1]
2024-02-01T10:30:04.679+08:00 DEBUG 11080 --- [           main] org.hibernate.SQL                        : select up1_0.id,up1_0.nickname from def_user up1_0 where up1_0.nickname like replace(?,'\\','\\\\')
2024-02-01T10:30:04.679+08:00 TRACE 11080 --- [           main] org.hibernate.orm.jdbc.bind              : binding parameter (1:VARCHAR) <- [%测试%]
2024-02-01T10:30:04.681+08:00 TRACE 11080 --- [           main] org.hibernate.orm.jdbc.extract           : extracted value (1:BIGINT) -> [1]
2024-02-01T10:30:04.681+08:00 TRACE 11080 --- [           main] org.hibernate.orm.jdbc.extract           : extracted value (2:VARCHAR) -> [娴嬭瘯84178]
打印查询用户信息:UserPo(id=1, nickname='娴嬭瘯84178')

Comment From: Lorwell

I think I have found the cause of the problem. It is the garbled code caused by useUnicode=true&characterEncoding=UTF-8 in the jdbc link character. After I removed it, everything in Chinese is normal. For details, please see my latest demo code: https://github.com/Lorwell/native-mysql-character-test