如何在Hibernate中正确设置UTF-8?

时间:2016-01-25 02:07:36

标签: java mysql hibernate utf-8

我正在使用Hibernate作为JEE解决方案。我需要数据库连接为UTF-8。这是我试过的:

数据库端

mysql> select c.character_set_name from information_schema.tables as t, information_schema.collation_character_set_applicability as c where c.collation_name = t.table_collation and t.table_schema = "ir2016" and t.table_name = "personne";
+--------------------+
| character_set_name |
+--------------------+
| utf8               |
+--------------------+
1 row in set (0.01 sec)

我还从MySQL Workbench插入了示例数据。结果以UTF-8编码良好。所以问题必须来自JEE服务器端。

JEE方

hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--Databaseconnectionsettings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/ir2016?useUnicode=true&amp;characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxxx</property>    
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">utf8</property>
        <property name="connection.CharSet">utf8</property>

        <!--JDBC connectionpool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!--SQL dialect-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--EnableHibernate'sautomaticsession contextmanagement -->
        <property name="current_session_context_class">thread</property>

        <!--Disablethe second-levelcache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!--Echo all executedSQL to stdout-->
        <property name="show_sql">true</property>

        <mapping resource="Quiz.hbm.xml"/>
        <mapping resource="Proposition.hbm.xml"/>
        <mapping resource="Question.hbm.xml"/>
        <mapping resource="Personne.hbm.xml"/>
        <mapping resource="Choisir.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

server.xml中

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

JSP页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是一个JEE解决方案,所以你需要检查你的constraints subdomain: /api(?:\..*)?/, format: 'json', protocol: 'http://' do get '/' => 'example#index' post '/create', to: 'example#create', as: :api_create_example end 是否配置为接受UTF-8而我忘了它。我需要一个过滤器来做到这一点

<强>的web.xml

web.xml

<强> CharacterEncodingFilter.java

<filter> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <filter-class>com.yourcompany.yourapp.util.CharacterEncodingFilter</filter-class> 
  <init-param> 
    <param-name>requestEncoding</param-name> 
    <param-value>UTF-8</param-value> 
  </init-param> 
</filter> 

<filter-mapping> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
</filter-mapping> 

来源:Struts 1.3.10 encoding problem