package com.bjpowernode.hibernate;import java.util.Date;/** * 使用者 * @author Longxuan * */public class User {private String id;private String name;private String password;private Date createTime;private Date expireTime;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getExpireTime() {return expireTime;}public void setExpireTime(Date expireTime) {this.expireTime = expireTime;}}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjpowernode.hibernate.User" ><!--hibernate為我們產生主鍵id--><id name="id"><generator class="uuid" /></id><!--預設把類的變數映射為相同名字的表列,當然我們使用column屬性修改表欄位--><property name="name" column="name"></property><property name="password"></property><property name="createTime"></property><property name="expireTime"></property></class></hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory name="foo"><!-- 資料庫的串連也可以直接使用hibernate.properties檔案 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 --><property name="hibernate.show_sql">true</property><!-- 設定是否顯示產生sql語句 --><property name="hibernate.format_sql">true</property><!-- 設定是否格式化sql語句--><mapping resource="com/bjpowernode/hibernate/User.hbm.xml" /></session-factory></hibernate-configuration>
package com.bjpowernode.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 將hbm產生ddl * @author Longxuan * */public class ExportDB {/** * @param args */public static void main(String[] args) {// 預設讀取hibernate.cfg.xml檔案Configuration cfg = new Configuration().configure();// 產生並輸出sql到檔案(目前的目錄)和資料庫SchemaExport export = new SchemaExport(cfg);// true 在控制台列印sql語句,true 匯入sql語句到資料庫,即可執行export.create(true, true);}}
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=warn, stdout