繁体   English   中英

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

[英]Insert into DB non-English characters with Spring 4 + Hibernate 4

我有Spring 4 + Hibernate 4 + 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" />

用于连接,但没有效果。

我认为Spring使用标准系统环境编码(在我的情况下是Windows 8.1,编码:win1251)打开文件,这就是为什么所有Hibernate配置都无效的原因。

我的问题是:

1.我该如何解决?

2.如果将来我要使用PostgreSQL,我将在数据库连接或其他配置中进行哪些更改?

这是我的资料来源:

spring.xml

<?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>

hibernate.cfg.xml

<?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>

import.sql

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);

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

您是否尝试以此启动Java App: java -Dfile.encoding=UTF-8

如果使用的是Tomcat,则可能必须在Tomcat的(catalina)脚本文件之一中设置此System属性。

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

更新:

如果正在使用maven surefire插件,则应根据此链接提供系统属性: http : //maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

请注意,该网站提到了JVM系统属性的特殊情况: <argLine>-Djava.endorsed.dirs=...</argLine>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM