Hibernate與MySQL開發中亂碼解決方案

本文參考自:[url]http://blog.tremend.ro/2007/08/14/how-to-set-the-default-charset-to-utf-8-for-create-table-when-using-hibernate-with-java-persistence-annotations/[/url],感謝提供者spostelnicu.

問題:hibernate與mysql開發中,新增和修改時出現中文亂碼
解決:
步驟一:將hibernate.cfg.xml中的connection.url值加入characterEncoding=utf8,具體如下:

文件:hibernate.cfg.xml內容

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?characterEncoding=utf8</property>
...

<session-factory>
<hibernate-configuration>


步驟二:在建表時,一定要將MySQL中的資料表格的欄位字符集設置成utf8的 ,否則會拋出類似如下錯誤:
錯誤訊息:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1;

如果是在外部建表,建表語句寫法如下:


create table `my_table` (
`Id` int(11) NOT NULL auto_increment,
`my_column` mediumtext NOT NULL default ”,
......
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



另外, hibernate的ORM機制允許我們選建實體,再建表,可以通過hibernate內部的hbm2ddl將實體類直接導出成關系表,可以寫一個
hbm2ddl操作類,如下:
java文件:ExportDB.java

	package com.jason.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

/**
* 執行本程式將會建立資料庫表格
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

/**
* 讀取hibernate配置文件
* 加上.configure()會去讀取hibernate.hbm.xml文件,如果沒有設定.
* 則會默認讀取hibernate.properties
*/
Configuration cfg = new Configuration().configure();

//讀取配置檔,讀取User.hbm.xml成ddl
SchemaExport export = new SchemaExport(cfg);
//生成實體表
export.create(true, true);
}
}



此時,需要額外對hibernate.cfg.xml進行設定, 先寫一個客戶方言類CustomerMySQLDialect.java,
重載MySQLDialect的getTableTypeString()方法,實現對生成的表格進行字符集設定

文件: CustomerMySQLDialect.java

package com.jason.hibernate;

import org.hibernate.dialect.MySQLDialect;

public class CustomerMySQLDialect extends MySQLDialect {

/**
* 重載getTableTypeString()方法
*/
public String getTableTypeString()
{
return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
}
}



之後,將hibernate的方言映身到該文件:
文件: hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
...
<property name="hibernate.dialect">com.jason.hibernate.CustomerMySQLDialect</property>
...

<session-factory>
<hibernate-configuration>



這樣就一切OK了,其它的方法沒試過,不過這個我運行過是ok的.
[color=red]另外說明:[/color] 在網上有一些提供以下的方法,不過經過我的驗證,這種方法是不行的,因為Hibernate3中好像沒有hibernate.connection.charSet屬性:
文件: hibernate.cfg.xml

	<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
...
<property name="hibernate.connection.charSet">utf8(或utf-8)</property>
...

<session-factory>
<hibernate-configuration>



小弟只是略懂皮毛,拋磚引玉,希望高手們能提供更好的解決方案,如有不足之處,還請高手們留言提點....多謝...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值