복붙노트

[SPRING] 스프링 데이터 jpa utf-8 인코딩이 작동하지 않습니다.

SPRING

스프링 데이터 jpa utf-8 인코딩이 작동하지 않습니다.

나는 spring-data-jpa와 mysql 데이터베이스를 사용한다. 내 테이블 캐릭터 세트는 utf-8입니다. 또한 application.properties 파일에서 mysql url에 useUnicode = yes & characterEncoding = utf8을 추가했습니다. "±"와 같은 문자를 컨트롤러에 전달하여 mysql에 저장하면 문제가 발생합니다. mysql에서 나는있어? 점수. 하지만 내가 MySQL을 콘솔 예를 업데이 트 projects_data 설정 데이터 = "ąęąčę"설정 id = 1; 모든 것이 잘 작동합니다.

application.properties:

# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=utf8
spring.datasource.username = gehive
spring.datasource.password = pass

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

테이블 :

+---------------+--------------------+
| TABLE_NAME    | character_set_name |
+---------------+--------------------+           
| customer      | utf8               |
| projects      | utf8               |
| projects_data | utf8               |
+---------------+--------------------+

해결법

  1. ==============================

    1.시험

    시험

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

    문제는 "-"누락으로 인한 것 같습니다.

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

  2. ==============================

    2.나는 같은 문제를 가지고 있었고, 나는이 줄을 application.properties 파일에 추가하여 해결했다.

    나는 같은 문제를 가지고 있었고, 나는이 줄을 application.properties 파일에 추가하여 해결했다.

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

    참고 : 다음은 작동하지 않습니다.

    spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
    
  3. ==============================

    3.다음과 같이 특수 문자를 이스케이프 처리해야합니다.     spring.datasource.url = jdbc \ : mysql \ : // localhost \ : 3306 / $ {SERVER_DB_NAME} \? useUnicode = true \ & characterEncoding = utf \ -8 \ & characterSetResults = utf \ -8

    다음과 같이 특수 문자를 이스케이프 처리해야합니다.     spring.datasource.url = jdbc \ : mysql \ : // localhost \ : 3306 / $ {SERVER_DB_NAME} \? useUnicode = true \ & characterEncoding = utf \ -8 \ & characterSetResults = utf \ -8

  4. ==============================

    4.내 경우에 그건 내 문제를 해결하는거야. https://mathiasbynens.be/notes/mysql-utf8mb4

    내 경우에 그건 내 문제를 해결하는거야. 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
    
  5. from https://stackoverflow.com/questions/38677740/spring-data-jpa-utf-8-encoding-not-working by cc-by-sa and MIT license