in Java

Spring + Hibernate + MySQL中文乱码问题

概要

在配置Spring + Hibernate存入MySQL数据库时有中文乱码的问题,尝试了解决办法。

问题

中文乱码解决起来有时会找不到头绪,必须要把各个配置都做正确才能解决,以下是几个注意点。

解决

MySQL的配置连接

在dispatcherServlet-servlet.xml里的设置尝试了多种配置,如下:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8</value>
            <!-- <value>jdbc:mysql://localhost/myNews?useUnicode=true&characterEncoding=GBK</value>-->
            <!-- <value>jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf8mb4</value>-->
            <!-- <value>jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf8mb4_unicode_ci</value> -->
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>P@ssw0rd</value>
        </property>
    </bean>

web.xml的配置

设置SetCharacterEncodingFilter方法

    <filter>
        <filter-name>setEncodingFilter</filter-name>
        <filter-class>com.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <!-- <param-value>utf-8</param-value> -->
            <!-- <param-value>GBK</param-value> -->
            <param-value>utf-8</param-value> 
        </init-param>
    </filter>

SetCharacterEncodingFilter 实现 Filter

/**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *执行过滤的方法
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

    // Pass control on to the next filter
        chain.doFilter(request, response);

    }


    /**
     * Place this filter into service.
     *初始化方法
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if (value == null)
            this.ignore = true;
        else if (value.equalsIgnoreCase("true"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }


    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters.  If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

MySQL数据库配置

my.ini里配置
character_set_server=utf8
并重启MySQL

MySQL数据库的整体Collation设置为utf8_bin
MySQL数据库的各中文字段设置Type为varchar,Collation设置为utf8_bin

参考

MysqlDataTruncation: Data truncation: Incorrect string value: ‘\xF0\x9D\x90\xB6″#…’ for column
100% solution java.sql.SQLException : Unsupported character encoding ‘utf8mb4’.
史上最全的Spring MVC 中文乱码问题解决方案

Write a Comment

Comment