我的系统在Linux Mandriva、RDBMS- 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>
...
我的.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 |