1、mysql中配置文件的设置
#设置client执行sql的请求串编码
[ client ]
port = 3306
socket = /tmp/mysql.sock
useUnicode = true
characterEncoding = utf8
#设置数据库系统本身编码格式
[ mysqld ]
default-character-set = utf8
[ client ]
port = 3306
socket = /tmp/mysql.sock
useUnicode = true
characterEncoding = utf8
#设置数据库系统本身编码格式
[ mysqld ]
default-character-set = utf8
2、设置hibernate配置文件
在配置文件中添加两个property
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF8</property>
<
property
name
="connection.driver_class"
>
com.mysql.jdbc.Driver
</ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/test
</ property >
< property name ="connection.useUnicode" > true </ property >
< property name ="connection.characterEncoding" > UTF8 </ property >
com.mysql.jdbc.Driver
</ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/test
</ property >
< property name ="connection.useUnicode" > true </ property >
< property name ="connection.characterEncoding" > UTF8 </ property >
经测试,hibernate中的配置文件的设置不设也能正常取出,
注:[mysqld]
default-character-set=utf8
中的utf8不能写成 utf-8,否则服务器无法启动,因为Mysql不认utf-8;
可以通过命令
SHOW VARIABLES LIKE '%character_set_%'
察看Mysql的编码设置
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 , E:softProgrammysqlmysql- 5.0.22 -win32sharecharsets
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 , E:softProgrammysqlmysql- 5.0.22 -win32sharecharsets