Hibernate 中的 UTF-8

CMJ*_*CMJ 4 java mysql hibernate spring-mvc utf-8

当我从 MySql 数据库的查询中检索信息时遇到问题,如下所示:

\n\n
Je b\xc3\x83\xc2\xa2tirai \n
Run Code Online (Sandbox Code Playgroud)\n\n

UTF-8 字符集和 Hibernate 或 MySql 存在错误。

\n\n

请问我该如何解决这个问题?

\n\n

这是我的休眠设置:

\n\n
Je b\xc3\x83\xc2\xa2tirai \n
Run Code Online (Sandbox Code Playgroud)\n\n

当我检索时:

\n\n
<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<!DOCTYPE hibernate-configuration PUBLIC\n    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"\n    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">\n<hibernate-configuration>\n    <session-factory>\n        <!-- Database connection settings -->\n        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>\n        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>\n        <property name="connection.url">jdbc:mysql://localhost:3306/church</property>\n        <property name="connection.username">root</property>\n        <property name="connection.password">root</property>\n\n        <property name="hibernate.connection.CharSet">utf8</property>\n        <property name="hibernate.connection.characterEncoding">utf8</property>\n        <property name="hibernate.connection.useUnicode">true</property>\n\n        <!-- JDBC connection pool (use the built-in) -->\n        <property name="connection.pool_size">1</property>\n        <!-- SQL dialect -->\n        <!-- Enable Hibernate\'s automatic session context management -->\n        <property name="current_session_context_class">thread</property>\n        <!-- Disable the second-level cache -->\n        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>\n        <!-- Echo all executed SQL to stdout -->\n        <property name="show_sql">true</property>\n\n        <!-- Drop and re-create the database schema on startup -->\n        <!-- cr\xc3\xa9er la BD -->\n        <!-- property name="hbm2ddl.auto">create</property -->\n        <!-- Met la BD existante \xc3\xa0 jour -->\n        <!-- property name="hbm2ddl.auto">update</property -->\n        <property name="hibernate.hbm2ddl.auto">update</property>\n\n        <!-- Mapping des classes persistantes -->\n\n        <mapping class="com.church.metier.User" />\n        <mapping class="com.church.metier.Comment" />\n        <mapping class="com.church.metier.Text" />\n        <mapping class="com.church.metier.MessageText" />\n        <mapping class="com.church.metier.MessageVideo" />\n        <mapping class="com.church.metier.News" />\n        <mapping class="com.church.metier.VerseMonth" />\n        <mapping package="com.church.metier" />\n\n\n    </session-factory>\n</hibernate-configuration>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的DAO:

\n\n
System.out.println(VerseMonthDAO.retrieveVersetMonth());\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的数据库:

\n\n
+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+\n| verseId | SOURCEEN       | SOURCEFR       | SOURCEIN       | TEXTEN                                                                                                | TEXTFR                                                                                              | TEXTIN                                                                                              |\n+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+\n|       1 | Je b\xc3\x83\xc2\xa2tirai    | Je b\xc3\x83\xc2\xa2tirai    | Je b\xc3\x83\xc2\xa2tirai    | Je b\xc3\x83\xc2\xa2tirai                                                                                           | Je b\xc3\x83\xc2\xa2tirai                                                                                         | Je b\xc3\x83\xc2\xa2tirai                                                                                         |\n+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+\n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢你的回答

\n

CMJ*_*CMJ 5

使用这些参数配置 Hibernate 后我找到了答案:

<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
Run Code Online (Sandbox Code Playgroud)

我们还必须在 web.xml 中使用以下参数将 Spring 配置为 UTF-8:

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

它有效!谢谢大家