복붙노트

[SPRING] Spring-Boot, 스프링 데이터 JPA를 사용하여 MySql의 유니 코드 문자열을 저장할 수 없음

SPRING

Spring-Boot, 스프링 데이터 JPA를 사용하여 MySql의 유니 코드 문자열을 저장할 수 없음

나는 내 application.properties를 이렇게 설정했다 :

spring.datasource.username = root
spring.datasource.password = root
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

그리고 내 pom.xmlI 속성을 다음과 같이 설정했습니다.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>toyanathapi.Application</start-class>
        <java.version>1.8</java.version>
</properties>

내 사업체 :     @실재 공용 클래스 DailyRashifalEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String date;
private int rollno;
private String name;
//Constructors and getters/setters 
}

문제 1 : 위의 설정을 사용하면 예외가 발생합니다.

java.sql.SQLException: Incorrect string value: '\xE0\xA4\xA7\xE0\xA4\xBE...

문제 2 : 데이터 소스 url을 다음과 같이 변경 한 경우

spring.datasource.url = jdbc:mysql://localhost:3306/dbname

내 데이터베이스의 유니 코드가 이렇게 저장됩니다.

 29 | 2074-03-04 |        3 | ?????????????? ?????,?????? ??????, ??????????? ????? ? ???? ???? ???? ??????  

어떻게 유니 코드로 변환하는 대신 유니 코드 데이터를 모두 ????????로 변환하는 것처럼 MySQL에서 저장할 수 있습니까? .

해결법

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

    1./etc/mysql/my.cnf 파일에서 다음을 변경하십시오.

    /etc/mysql/my.cnf 파일에서 다음을 변경하십시오.

    [mysql]
    default-character-set=utf8
    [mysqld]
    character-set-server=utf8
    
  2. ==============================

    2.최대 절전 모드 구성 유지

    최대 절전 모드 구성 유지

    DB 콜레 션 변경

    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    추가 정보 : 링크

  3. ==============================

    3.http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored의 "물음표"를 참조하십시오.

    http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored의 "물음표"를 참조하십시오.

    또한,

    ⚈  spring.jpa.properties.hibernate.connection.characterEncoding=utf-8 
    ⚈  spring.jpa.properties.hibernate.connection.CharSet=utf-8 
    ⚈  spring.jpa.properties.hibernate.connection.useUnicode=true 
    
  4. ==============================

    4.대답의 일부는 나를 위해 일했습니다 ... URL 인코딩 부분을 제외하고.

    대답의 일부는 나를 위해 일했습니다 ... URL 인코딩 부분을 제외하고.

    내 경우의 해결책은 두 가지입니다.

    1- Database config bean의 URL에 인코딩 추가 :

    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customersdb?useSSL=false&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8" />
    

    2 -이 구성을 운영자 구성 파일에 추가합니다.

    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    

    그렇지 않으면 다른 구성이 적용되지 않습니다.

  5. from https://stackoverflow.com/questions/44588055/spring-boot-cant-save-unicode-string-in-mysql-using-spring-data-jpa by cc-by-sa and MIT license