日期:2014-05-16 浏览次数:21358 次
<!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>
create table `my_table` (
`Id` int(11) NOT NULL auto_increment,
`my_column` mediumtext NOT NULL default ”,
......
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
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);
}
} package com.jason.hibernate;
import org.hibernate.dialect.MySQLDialect;
public class CustomerMySQLDialect extends MySQLDialect {
/**
* 重載getTableTypeString()方法
*/
public String getTableTypeString()
{
return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
}
}<!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>
<!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>