这几天在学习Hibernate,数据库用的是MySQL,在学习的过程中,遇到了中文显示的问题,于是在网上找了好多资料!最终搞定,于是写出来初学者一起分享!<o:p></o:p>
我觉得要考虑hibernate和mysql的中文显示问题应该从以下几个方面考虑:<o:p></o:p>
(1)Hibernate所才用的字符集<o:p></o:p>
(2)MySql所采用的字符集(应该是这个软件的字符集,我不知道这样说合不合适,暂且这样说!)<o:p></o:p>
(3)创见数据库和表时 所采用的字符集<o:p></o:p>
针对以上的问题,我用以下的办法解决:<o:p></o:p>
对于Hibernate而言,不知道大家是否记得,我们使用JDBC时,可以有这样的语句:<o:p></o:p>
connection =DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&password=&useUnicode=true&characterEncoding=GBK" );<o:p></o:p>
<o:p> </o:p>
在Hibernate也可以有这样的设置,这就需要在Hibernate.cfg.xml文件中进行配置:<o:p></o:p>
<o:p></o:p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<o:p></o:p>
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
<hibernate-configuration></hibernate-configuration><o:p></o:p>
<o:p> </o:p>
<session-factory></session-factory><o:p></o:p>
<property name="connection.username">root</property> <o:p></o:p>
<property name="connection.url">jdbc:mysql://localhost:3306/gcwebt?useUnicode=true&characterEncoding=UTF-8</property> <o:p></o:p>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <o:p></o:p>
<property name="myeclipse.connection.profile">gcweb</property> <o:p></o:p>
<property name="connection.password">19850520</property> <o:p></o:p>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <o:p></o:p>
<property name="show_sql">true</property> <o:p></o:p>
<mapping resource="hibernate.cfg.xml"></mapping><o:p></o:p>
<mapping resource="edu/jlu/gcweb/model/User.hbm.xml"></mapping><o:p></o:p>
<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
<o:p> </o:p>
在以上配置的的第二个属性我添加了:?useUnicode=true&characterEncoding=UTF-8<o:p></o:p>
<o:p> </o:p>
这样,Hibernate处理完毕。<o:p></o:p>
<o:p> </o:p>
下面,对于MySql数据库本身而言,在安装完之后,他默认的字符集是:latin1<o:p></o:p>
<o:p></o:p>
为了中文显示,我要将它变为utf8,就是我用UltraEdit打开 MySQL安装目录下的my.ini文件找到如下内容:<o:p></o:p>
[client]<o:p></o:p>
port=3306<o:p></o:p>
[mysql]<o:p></o:p>
default-character-set=latin1<o:p></o:p>
# SERVER SECTION<o:p></o:p>
# ----------------------------------------------------------------------<o:p></o:p>
# The following options will be read by the MySQL Server. Make sure that<o:p></o:p>
# you have installed the server correctly (see above) so it reads this<o:p></o:p>
# file.<o:p></o:p>
#<o:p></o:p>
[mysqld]<o:p></o:p>
# The TCP/IP Port the MySQL Server will listen on<o:p></o:p>
port=3306<o:p></o:p>
#Path to installation directory. All paths are usually resolved relative to this.<o:p></o:p>
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"<o:p></o:p>
#Path to the database root<o:p></o:p>
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"<o:p></o:p>
# The default character set that will be used when a new schema or table is<o:p></o:p>
# created and no character set is defined<o:p></o:p>
default-character-set=latin1<o:p></o:p>
我将上文中的latin1改为utf8: 如下:<o:p></o:p>
[client]<o:p></o:p>
port=3306<o:p></o:p>
[mysql]<o:p></o:p>
default-character-set=utf8<o:p></o:p>
# SERVER SECTION<o:p></o:p>
# ----------------------------------------------------------------------<o:p></o:p>
#<o:p></o:p>
# The following options will be read by the MySQL Server. Make sure that<o:p></o:p>
# you have installed the server correctly (see above) so it reads this<o:p></o:p>
# file.<o:p></o:p>
#<o:p></o:p>
[mysqld]<o:p></o:p>
# The TCP/IP Port the MySQL Server will listen on<o:p></o:p>
port=3306<o:p></o:p>
#Path to installation directory. All paths are usually resolved relative to this.<o:p></o:p>
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"<o:p></o:p>
#Path to the database root<o:p></o:p>
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"<o:p></o:p>
# The default character set that will be used when a new schema or table is<o:p></o:p>
# created and no character set is defined<o:p></o:p>
default-character-set=utf8<o:p></o:p>
<o:p> </o:p>
这样,剩下的数据库和表的问题就好办了,一下是创建数据库和表的语句:<o:p></o:p>
create database xxx character set utf8<o:p></o:p>
create table xxx character set utf8<o:p></o:p>
或者:<o:p></o:p>
create table XXX DEFAULT CHARSET=utf8;<o:p></o:p>
<o:p> </o:p>
以上是我的心得,作为一个初学者,我希望得到大家的指导!<o:p></o:p>