使用Spring JPA将阿拉伯语文本保存到mysql

时间:2015-07-15 07:32:45

标签: java mysql spring hibernate jpa

大家,我的应用程序基于Spring MVC,我试图在MySQL中保留阿拉伯语文本(我使用JPA),但它保存 ???? 而不是阿拉伯字符。我试着用这个:

spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8

但是当我试图用阿拉伯语保存文本时我遇到了错误:

java.sql.SQLException: Incorrect string value: '\xD8\xAA\xD8\xB1\xD8\xAD...' for column 'last_name' at row 1

提前致谢。

1 个答案:

答案 0 :(得分:2)

您好,在互联网上进行了一些搜索之后,我在我的application.properties中添加了两行代码,以便使用UTF-8编码,最终工作正常。这是文件。不要忘记更改网址和密码等。

   # DataSource settings: set here your own configurations for the   
# #database 
    # connection. In this example we have "netgloo_blog" as database name #and 
    # "root" as username and password.
    spring.datasource.url = jdbc:mysql://localhost:3306/newDB?useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
    spring.datasource.username = root
    spring.datasource.password = somePassword

    # 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
    server.tomcat.uri-encoding=UTF-8

    # HTTP encoding (HttpEncodingProperties)
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true
    spring.http.encoding.force=true

    # 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
    spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
    spring.jpa.properties.hibernate.connection.CharSet=utf-8
    spring.jpa.properties.hibernate.connection.useUnicode=true

    server.port=8080