我遇到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
...
<?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>
<!-- .../... -->
有什么方法可以解决这个问题吗?我现在无法找到任何可行的东西。
答案 0 :(得分:2)
添加到您的数据库网址参数characterEncoding=utf-8
,因此连接网址应为
<property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf-8"/>