问题:最近项目开发中,碰到了中文乱码问题。Mysql已设置为UTF-8,内存中的内容是正常,通过hibernate保存到Mysql数据库中就是乱码。

 

解决:

设置Hibernate的编码,两种方式:

1.hibernate.cfg.xml或applicationContenxt.xml文件中设置,如下:

< property  name ="url" > jdbc:mysql://localhost:3306/test?useUnicode=true &amp; characterEncoding=UTF-8 </ property >
 
注意:在xml中,用 &amp;代替&, 否则启动检测xml配置文件格式报错。
 
2. 通过 hibernate.properties设置
#hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8