springMVC保存数据到mysql数据库中文乱码问题解决方法

时间:2022-04-03 06:53:08

1.web.xml中添加过滤器

  <filter>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>utf-8</param-value>  
    </init-param>  
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 

2.更改数据库及其表中字符编码及其校对规则为utf8,utf8-general-ci

3.连接数据库的url增加

useUnicode=true&characterEncoding=utf-8