gpt4 book ai didi

java - hibernate中对utf8数据的支持

转载 作者:行者123 更新时间:2023-11-29 18:35:52 26 4
gpt4 key购买 nike

我在从 hibenate 中获取 utf8 数据时遇到了一些麻烦。我正在使用 RDS 数据库。我使用以下语法创建了一个表:

CREATE TABLE `Test' (
`id` char(36) NOT NULL
)DEFAULT CHARSET=utf8;

这是我的 hibernate 配置:

<prop key="hibernate.connection.url">${JDBC.databaseURL}</prop>
<prop key="hibernate.connection.username">${Username} </prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>

我已经用日语在数据库表中手动插入了一行,当我从 Test 中选择 * 时,我可以看到数据以日语显示;

但是当我从 hibernate 状态查询时,它变成了垃圾。我没有正确返回。你能帮我解决我在这里做错的事情吗?

最佳答案

除了 hibernate 配置之外,您还需要使用过滤器进行设置。

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

hibernate 配置。

<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>

关于java - hibernate中对utf8数据的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45305356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com