我对 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。所以在此之后,我通过我的配置配置了 spring 和 hibernateweb.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>
<!-- .../... -->
And my 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>
<!-- .../... -->
有什么办法可以解决这个问题吗?我现在找不到任何有效的东西。