springboot从mysql中提取的数据中文是乱码
时间: 2024-05-11 22:19:00 浏览: 8
出现中文乱码的原因可能是数据库的字符集与应用程序的字符集不一致。可以检查一下以下几个方面:
1. 确认 MySQL 数据库的字符集是否为 UTF-8,可以通过执行 `show variables like 'character%';` 命令来查看。
2. 确认 Spring Boot 应用程序的字符集是否为 UTF-8,可以在 application.properties 文件中添加以下配置:
```
spring.http.encoding.charset=UTF-8
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
其中 `useUnicode=true&characterEncoding=utf8` 的配置可以保证数据库与应用程序之间的字符集一致。
3. 确认在数据库中插入数据时,使用了正确的字符集。可以通过以下命令来设置 MySQL 客户端的字符集:
```
set names utf8;
```
然后再插入数据。
如果以上操作都没有解决问题,可以尝试在 Spring Boot 应用程序中使用字符集转换工具类进行转换。例如,使用 `new String(data.getBytes("ISO-8859-1"), "UTF-8")` 将 ISO-8859-1 编码转换为 UTF-8 编码。