带JDBC的MySQL - 需要useUnicode吗?

时间:2017-09-11 12:35:50

标签: java mysql jdbc mysql-connector

在很多地方,需要注意的是,如果你想在MySQL中使用JDBC并且支持UTF-8,你需要在JDBC URL中添加useUnicode=true

E.g:

但是,我在Mac OS X上使用MySQL 5.7.19本地尝试使用mysql-connector-java 5.1.41(由Spring Boot 1.5.3引入)。我添加了一个文件/etc/my.cnf

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

这确保MySQL设置为UTF-8:

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | utf8                                                      |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | utf8                                                      |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.19-macos10.12-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.00 sec)

现在我的application.properties我试过了:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true

但在这两种情况下,似乎都没问题。那么:添加useUnicode=true的建议仍然有效吗?

0 个答案:

没有答案