首先在命令行运行如下命令查看系统参数:
mysql>show variables like "character%";
mysql>show variables like "coll%";
结果发现不是utf-8,那么我们去修改配置文件。
首先关闭MySQL服务:
sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop
sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
修改其内容:
[client] default-character-set = utf8 [mysqld] character-set-server = utf8
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart
此时去检查系统变量应该已经设置。
create database hibernate character set utf8;
其中"set utf8"可省略。如果在工具中创建,选择默认的编码,之后应该可以看到数据库已经是utf-8的了。
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect"> org.hibernate.dialect.MySQL5Dialect </property> <!-- Supported UTF-8 character set --> <property name="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
通过测试代码生成表:
SchemaExport export = new SchemaExport(new Configuration().configure()); export.create(true, bExport);
完成后通过Java测试是否可以正确输入中文。