一尘不染

在JSF2.0,Hibernate,MySQL中启用UTF-8字符集

hibernate

我们将对使用JSF2.0,Hibernate,MySQL设计的Web应用程序启用UTF-8字符编码。

以下是我们的应用程序上下文文件中定义的数据源

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
        <property name="useUnicode" value="yes" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

在运行应用程序时,我们遇到异常

Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'DataSource'  Error creating bean with name 'DataSource' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invalid property 'useUnicode' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'useUnicode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我们也尝试使用以下方法,但出现错误

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8;" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>


Error: The reference to entity "characterEncoding" must end with the ';' delimiter

阅读 363

收藏
2020-06-20

共1个答案

一尘不染

经过一些解决后,我能够处理该问题-以下是对我有用的代码,以启用与UTF8一起使用的JDBC

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=UTF-8" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>

配合使用useUnicode=true&amp;characterEncoding=UTF-8达到&amp;目的

为了能够与Hibernate一起使用,还应在hibernate属性中指定以下内容

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>               
            </props>
        </property>

还要在过滤器中指定请求编码格式

public class ApplicationFilter implements Filter {    
        public void destroy() {
        }

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
        }

        public void init(FilterConfig fConfig) throws ServletException {
        }
    }

现在,即使您的应用程序和数据库不支持特殊字符,也请检查数据库字符集,尝试重新创建数据库并使用字符集UTF8代替latin1

2020-06-20