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

标签 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"%>

有人可以帮忙吗?

最佳答案

这是一个 JEE 解决方案,因此您需要检查您的 web.xml 是否配置为接受 UTF-8,我只是忘了它。我需要一个过滤器来做这件事

web.xml

<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> 

CharacterEncodingFilter.java

package com.yourcompany.yourapp.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Struts 1.3.10 encoding problem
 * 
 * @see http://www.coderanch.com/t/557874/Struts/Struts-encoding
 */
public class CharacterEncodingFilter implements Filter { 
    private FilterConfig fc; 

    @Override
    public void destroy() {
        // TODO Auto-generated method
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req; 
        HttpServletResponse response = (HttpServletResponse) resp; 

        request.setCharacterEncoding("UTF8"); 
        response.setCharacterEncoding("UTF8"); 

        chain.doFilter(request, response); 

        request.setCharacterEncoding("UTF8"); 
        response.setCharacterEncoding("UTF8"); 
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }
}

来源:Struts 1.3.10 encoding problem

关于java - 如何在 Hibernate 中正确设置 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984064/

相关文章:

mysql - 全文搜索不排除-xxxword

mysql - 为什么在 MySQL 表中添加重复索引会导致查询执行时间变长?

java - 如何部署Openfire服务器

java - 查找二叉树中的所有子树

java - 每次尝试编译程序时,我都会遇到多个错误。不知道我哪里出错了

php - 如何使用angularjs php将mysql中的数据显示到ckeditor字段中?

java - 声明 for 更好还是相同?

hibernate - 使用 Grails DetachedCriteria 无法获取 "count"和 "groupBy"

java - Spring Maven hibernate 类未找到异常

java - 我们可以在没有 hibernate 的情况下使用jpa吗