Hibernate,Spring和MySQL:无法将字符集设置为UTF-8 [英] Hibernate, Spring and MySQL : unable to set the charset to UTF-8
问题描述
我在使用Hibernate和Spring时遇到问题:我无法在任何地方使用UTF-8字符集,而且在重音字母方面也遇到了一些问题(例如éèà ...).这是我创建数据库的一部分:
I have an issue with Hibernate and Spring : I am unable to use the UTF-8 charset everywhere, and I have some issues with accented letters (like éèà...). Here is a part of my database creation :
-- 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.因此,在此之后,我配置了spring并通过web.xml
...
I think this implies that my database is in full UTF-8. So after this, I configured spring and hibernate through my web.xml
...
<?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&connectionCollation=utf8_general_ci&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>
<!-- .../... -->
有什么办法解决这个问题?我现在找不到任何有效的方法.
Is there any way to solve this ? I am unable to find anything that work right now.
推荐答案
添加到您的数据库URL参数characterEncoding=utf-8
中,因此连接URL应该是
Add to your DB URL parameter characterEncoding=utf-8
so the connection URL should be
<property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf-8"/>
这篇关于Hibernate,Spring和MySQL:无法将字符集设置为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!