Hibernate + MySQL:如何为数据库和表设置编码 utf-8 [英] Hibernate + MySQL: How to set the encoding utf-8 for database and tables

查看:17
本文介绍了Hibernate + MySQL:如何为数据库和表设置编码 utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统在 Linux Mandriva、RDBMS - MySQL 5 上运行.我需要以 UTF-8 格式创建数据库和表.

My system runs on Linux Mandriva, RDBMS - MySQL 5. I need to have the database and tables created in 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例如 -

But when the tables are generated, I see the encoding latin1 For example-

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?

如果您提供信息,我将不胜感激!谢谢!

I would be most grateful for the information! Thank you!

...

奇怪,我都改成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 |

推荐答案

您还可以使用编码创建数据库.
只需使用 phpMyAdmin 来创建数据库/表.

You can also create databases with encoding.
Simply use phpMyAdmin for the database/table creation.

您可以在休眠设置的 URL 中指定一些 URL 参数以使用 UTF8 进行连接:

There are some URL parameters you would specify in the URL of the hibernate settings to have the connection using UTF8:

<!-- Database Settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--  for performance reasons changed to MyISAM from org.hibernate.dialect.MySQLInnoDBDialect -->
<property name="dialect">org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect</property>
<property name="connection.url">jdbc:mysql://localhost/openmeetings?autoReconnect=true&amp;useUnicode=true&amp;createDatabaseIfNotExist=true&amp;characterEncoding=utf-8</property>    

<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>

不需要将数据库中的整个编码设置为utf8仅当您使用

You don't need to set the whole encoding in the database to utf8 Only if you are using

<!-- Database Scheme Auto Update -->
<property name="hbm2ddl.auto">update</property>   

您必须将 MySQL 的默认编码设置为 utf8.因为hbm2dll 将使用数据库的默认编码.

You WILL have to set the default encoding of MySQL to utf8. Cause the hbm2dll will use the default encoding of the database.

您可能仍然使用 hbm2ddl.auto,并手动修改数据库的表以进行 utf8 排序.

You might still use hbm2ddl.auto, and modify the table's of the database manually to have utf8 collation.

如果您不使用 hbm2ddl.auto,您可以简单地使用您喜欢的编码创建表格.无需将数据库设置为特殊编码.

If you are not using hbm2ddl.auto, you can simply create the tables with your favorite encoding. No need to set the database to a special encoding.

塞巴斯蒂安

这篇关于Hibernate + MySQL:如何为数据库和表设置编码 utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆