我使用的操作系统是Linux Mandriva,关系型数据库管理系统是MySQL 5。
我需要在UTF-8编码下创建数据库和表格。
以下是hibernate.cfg.xml的一部分内容 -
...
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
...
my.cnf -
# The MySQL server
[mysqld]
...
default-character-set=cp1251
character-set-server=cp1251
collation-server=cp1251_general_ci
init-connect="SET NAMES cp1251"
skip-character-set-client-handshake
...
[mysqldump]
...
default-character-set=cp1251
...
例如,某些类:
@Entity
@Table(name = "USER")
public class User {
@Id
@Column(name = "USERID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "USERNAME")
private String name;
@Column(name = "USERPASSWORD")
private String password;
@Column(name = "USERIP")
private String ip;
// getter's and setter's here
...
但是当表格被生成时,我看到编码为 latin1 例如-
SHOW CREATE TABLE USER;
USER | CREATE TABLE `user` (
`USERID` int(11) NOT NULL auto_increment,
`USERIP` varchar(255) default NULL,
`USERNAME` varchar(255) default NULL,
`USERPASSWORD` varchar(255) default NULL,
PRIMARY KEY (`USERID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
如何将编码更改为UTF-8?
非常感谢提供信息!谢谢!
...
这很奇怪,我已经全部更改为utf8 -
# The MySQL server
[mysqld]
...
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
init-connect="SET NAMES utf8"
skip-character-set-client-handshake
...
[mysqldump]
...
default-character-set=utf8
...
现在 -
SHOW CREATE TABLE USER;
USER | CREATE TABLE `USER` (
`USERID` int(11) NOT NULL auto_increment,
`USERIP` varchar(255) default NULL,
`USERNAME` varchar(255) default NULL,
`USERPASSWORD` varchar(255) default NULL,
PRIMARY KEY (`USERID`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 |
SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='<database-name>' AND TABLE_NAME='<table-name>' AND COLUMN_NAME='<column-name>';
- Emanuel George Hategan