一口一口吃掉Hibernate(一)——使用SchemaExport產生資料表

來源:互聯網
上載者:User

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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.