【问题标题】:Hibernate, Spring and MySQL : unable to set the charset to UTF-8Hibernate、Spring 和 MySQL:无法将字符集设置为 UTF-8
【发布时间】:2016-01-08 19:25:21
【问题描述】:

我在使用 Hibernate 和 Spring 时遇到了问题:我无法在任何地方使用 UTF-8 字符集,并且我在重音字母方面遇到了一些问题(例如 éèà...)。这是我创建数据库的一部分:

-- Dropping and creating database again
DROP DATABASE `miniprojetjee`;
CREATE DATABASE IF NOT EXISTS `miniprojetjee` default character set = "UTF8" default collate = "utf8_general_ci";

-- My user
GRANT ALL ON `miniprojetjee`.* to 'miniprojetjee'@'localhost' identified by 'miniprojetjee';

CREATE TABLE Utilisateur (
    username VARCHAR(45) NOT NULL ,
    password VARCHAR(45) NOT NULL ,
    enabled TINYINT NOT NULL DEFAULT 1 ,
    telephone VARCHAR(20),
    mail VARCHAR(255) NOT NULL,
    description MEDIUMBLOB,
    date_inscription TIMESTAMP DEFAULT NOW(),
    PRIMARY KEY (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- .../...

我认为这意味着我的数据库是完整的 UTF-8。所以在这之后,我通过我的web.xml配置了spring和hibernate...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml, /WEB-INF/spring-security.xml</param-value>
    </context-param>

<!-- .../... -->

还有我的dispatcher.xml

<!-- .../... -->
<bean id="dataSourceMySql" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&amp;connectionCollation=utf8_general_ci&amp;characterSetResults=utf8"/>
    <property name="username" value="miniprojetjee"/>
    <property name="password" value="miniprojetjee"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceMySql"/>
    <property name="packagesToScan" value="dao"/>

    <property name="hibernateProperties">
        <props>
            <prop key="show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <!-- Tryied a lot of things...
            <prop key="connection.useUnicode">true</prop>
            <prop key="connection.characterEncoding">UTF-8</prop>
            <prop key="connection.charSet">UTF-8</prop>
            <prop key="useUnicode">true</prop>
            <prop key="characterEncoding">UTF-8</prop>
            <prop key="charSet">UTF-8</prop>
            -->
        </props>
    </property>
</bean>

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

<!-- .../... -->

有没有办法解决这个问题?我现在找不到任何可行的方法。

【问题讨论】:

  • 你可能需要&lt;form accept-charset="UTF-8"&gt;

标签: mysql xml spring hibernate utf-8


【解决方案1】:

添加到您的 DB URL 参数 characterEncoding=utf-8 以便连接 URL 应该是

<property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&amp;connectionCollation=utf8_general_ci&amp;characterSetResults=utf8&amp;characterEncoding=utf-8"/>

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 2011-02-26
    • 2016-09-14
    • 2017-03-21
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2015-02-24
    相关资源
    最近更新 更多