Java 使用Spring 4+Hibernate 4将非英语字符插入DB

Java 使用Spring 4+Hibernate 4将非英语字符插入DB,java,spring,hibernate,encoding,Java,Spring,Hibernate,Encoding,我有Spring4+Hibernate4+MySQL web应用程序。我需要初始化数据库表时,应用程序正在启动。为此,我使用import.sql文件并将Hibernate hbm2ddl.auto设置为create。当Hibernate执行请求时,我在DB中有象形文字。我试着用 <property name="url" value="jdbc:mysql://localhost:3306/foxrest_db?useUnicode=true&amp;characterEncodin

我有Spring4+Hibernate4+MySQL web应用程序。我需要初始化数据库表时,应用程序正在启动。为此,我使用import.sql文件并将Hibernate hbm2ddl.auto设置为create。当Hibernate执行请求时,我在DB中有象形文字。我试着用

<property name="url" value="jdbc:mysql://localhost:3306/foxrest_db?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8" />

我认为需要设置JVM的字符编码属性

您是否尝试过使用以下命令启动Java应用程序: java-Dfile.encoding=UTF-8

如果您使用的是Tomcat,那么您可能必须在Tomcat的catalina脚本文件中设置此系统属性

注:过去我只使用ISO-8559-1。Java应用程序和数据库都设置为ISO-8559-1

更新:

如果正在使用maven surefire插件,则应根据以下链接提供系统属性:

请注意,该网站提到了JVM系统属性的特殊情况:
-Djava.annowed.dirs=…

我尝试使用此解决方案:。这没用。我认为你的建议是平等的,不是吗?这种方法似乎很好,但我以前没有这样设置编码属性。该网站提到UTF8,但我认为它应该是UTF-8。我尝试将UTF8改为UTF-8,但什么都没有发生。此外,我还尝试将这些参数添加到我的Tomcat8的catalina.bat中,正如这里所说的:但它没有帮助。我尝试了两种变体:CATALINA_OPTS=-Dfile.encoding=UTF-8和JAVA_OPTS=-Dfile.encoding=UTF-8。这种解决方案很有用,但我认为这不是最好的方法。我不想更改Tomcat或其他服务器的配置。我认为最好的方法是尽可能将所有需要的配置保存在我自己的应用程序中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.foxresult" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"  value="jdbc:mysql://localhost:3306/foxrest_db" />
        <property name="username" value="lekarto" />
        <property name="password" value="1" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
        <property name="hibernate.cache.default_cache_concurrency_strategy">transactional</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.connection.useUnicode">true</property>
        <property name="hibernate.connection.characterEncoding">UTF-8</property>
        <property name="hibernate.connection.charSet">UTF-8</property>
    </session-factory>
</hibernate-configuration>
INSERT INTO `departments` (`name`) VALUES ('Developers'), ('QA'), ('Managers'), ('Support');
INSERT INTO `employees` (`first_name`, `last_name`, `salary`, `sex`, `department_id`) VALUES ('Sergey', 'Fedorov', '100', 1, '1'), ('Иван', 'Демидов', '120', 1, '1'), ('الحسيب', 'عبد', '140', 1, '2'), ('Angelina', 'Feofilaktova', '160', 0, '3'), ('湧', '阮', '180', 0, '3'), ('Test', 'Ivanovna', '180', 0, NULL);