当前位置:   article > 正文

Spring + Hibernate + MySQL中文乱码问题_unsupported character encoding 'utf8mb4

unsupported character encoding 'utf8mb4

概要

在配置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&amp;characterEncoding=utf-8</value>
			<!-- <value>jdbc:mysql://localhost/myNews?useUnicode=true&amp;characterEncoding=GBK</value>-->
			<!-- <value>jdbc:mysql://localhost:3306/myNews?useUnicode=true&amp;characterEncoding=utf8mb4</value>-->
			<!-- <value>jdbc:mysql://localhost:3306/myNews?useUnicode=true&amp;characterEncoding=utf8mb4_unicode_ci</value> -->
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>P@ssw0rd</value>
		</property>
	</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

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 中文乱码问题解决方案

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/858622
推荐阅读
相关标签
  

闽ICP备14008679号