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

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

问题描述

我的系统在 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>
...

my.cnf -

 #MySQL服务器
[mysqld]
...
default-character-set = cp1251
character-set-server = cp1251
collat​​ion-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和setter在这里
...

我看到编码 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



我会非常感谢的信息!
谢谢!



...



这很奇怪, strong> utf8 -

 #MySQL伺服器
[mysqld]
...
default-character-set = utf8
character-set-server = utf8
collat​​ion-server = utf8_general_ci
init-connect =SET NAMES utf8
skip- character-set-client-handshake
...
[mysqldump]
...
默认字符集= 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创建数据库/表。



有一些URL参数可以在hibernate设置的URL中指定,以使用UTF8 :

 <! - 数据库设置 - > 
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
<! - 由于性能原因更改为MyISAM从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
您正在使用

 <! - 数据库方案自动更新 - > 
< property name =hbm2ddl.auto> update< / property>

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



您可能仍然使用hbm2ddl.auto,并手动修改数据库的表以进行utf8整理。 p>

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



Sebastian


My system runs on Linux Mandriva, RDBMS - MySQL 5. I need to have the database and tables created in UTF-8.

Here is a fragment of 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
...

Some class, for example -

@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
        ...

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 |

How to change the encoding to UTF-8?

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

...

This is strange, I have changed all to 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
    ...

And now -

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 |

解决方案

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

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>

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>   

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

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

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.

Sebastian

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

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