Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。
今天就來演示一下Hibernate最初級的操作,使用SchemaExport創建數據表。
1.首先建立POJO類
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; } //URL:http://www.bianceng.cn/Programming/Java/201410/45827.htm 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; } }
2、根據POJO類裡面裡面相關的字段,在包中創建User.hbm.xml映射文件
<?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>
3、在src中建立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 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>
4、建立ExportDB類
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); } }
5、建立log4j.properties日志文件
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.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
現在可以測試了。首先在mysql中創建hibernate_test數據庫:
URL:http://www.bianceng.cn/Programming/Java/201410/45827.htm
運行ExportDB的main方法,結果如圖: