mysql – 持久化对象时,Hibernate编码错误[UTF-8]

我正在面对UTF-8编码问题,同时保持我的模型对象.在土耳其语’ı’是一封信.还有一些其他土耳其字符包含在UTF-8编码中.当我坚持我的模型对象时,所有的“ı”字符都会被保留为“?”到DB.我在Ubuntu Linux 64位操作系统上使用 MySQL 5.5.另外我已经设置了hibernate& c3p0连接编码属性也是UTF-8.当我调试时,来自客户端的数据是真实的.

这是我的配置,如果有人可以帮助我,我会很高兴.

提前致谢.

春天&休眠配置

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="packagesToScan" value="com.tk.dms.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <!-- c3p0 properties -->
                <prop key="hibernate.c3p0.min_size">2</prop>
                <prop key="hibernate.c3p0.max_size">50</prop>
                <prop key="hibernate.c3p0.maxPoolSize">50</prop>
                <prop key="hibernate.c3p0.minPoolSize">2</prop>
                <prop key="hibernate.c3p0.initialPoolSize">2</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>                
            </props>
        </property>
    </bean>
尝试在数据源中设置编码

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://127.0.0.1:3306/databaseName?characterEncoding=UTF-8</value>
    </property>
    <property name="username">
        <value>?</value>
    </property>
</bean>

您还确定表单的输入是否已正确编码?你在spring应用程序中使用过滤器吗?在调试模式下运行应用程序,并在持久化之前检查模型对象的字段.

过滤器应放置在您的web.xml文件中:

<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>
相关文章
相关标签/搜索