spring data jpa utf-8 encoding not working

后端 未结 6 720
忘了有多久
忘了有多久 2020-12-14 17:37

I use spring-data-jpa and mysql database. My tables character set is utf-8. Also I added ?useUnicode=yes&characterEncoding=utf8

相关标签:
6条回答
  • 2020-12-14 18:13

    For anyone using the HikariCP connection pool who prefers not to append the parameters directly to the JDBC URL:

    The spring.datasource.connectionProperties property was removed some time ago. Since then, you need to use connection pool specific properties as shown in @SebTheGreat's answer: spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;

    Hikari doesn't have a connection-properties property, but this works:

    spring.datasource.hikari.data-source-properties.useUnicode=true
    spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
    
    0 讨论(0)
  • 2020-12-14 18:14

    Try

    spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=UTF-8
    

    It seems issue is due to missing "-".

    Reference:- https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next

    0 讨论(0)
  • 2020-12-14 18:20

    I had the same issues, and I solved it by adding this line to my application.properties file:

    spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
    

    Note: The following didn't work:

    spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
    
    0 讨论(0)
  • 2020-12-14 18:22

    It works for me if using mysql java connector version 5.1.44 (before I used connector 8.0.17 it doesn't work)

    0 讨论(0)
  • 2020-12-14 18:29

    Remember to escape any special characters, as below: spring.datasource.url=jdbc\:mysql\://localhost\:3306/${SERVER_DB_NAME}\?useUnicode=true\&characterEncoding=utf\-8\&characterSetResults=utf\-8

    0 讨论(0)
  • 2020-12-14 18:30

    In my case that's solve my issue https://mathiasbynens.be/notes/mysql-utf8mb4

    [client]
    default-character-set = utf8mb4
    
    [mysql]
    default-character-set = utf8mb4
    
    [mysqld]
    character-set-client-handshake = FALSE
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci
    
    0 讨论(0)
提交回复
热议问题