1.Java代码在连接mysql时的配置文件上加上这一句话?useUnicode=true&characterEncoding=utf8用来设置字符编码,记住?不能省略
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
2.在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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.数据库编码出现问题
设置数据库的字符编码:ALTER DATABASE `数据库` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
设置数据库表的字符编码:ALTER TABLE `数据表` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci