分享java帶來的快樂

          我喜歡java新東西

          Hibernate中使用類映射文件創建數據庫表

           

          1.加入Hibernate 3支持;
          2.定義實體類;
          3.創建實體類的映射文件;
          4.通過配置文件hibernate.cfg.xml將映射文件加入到Hibernate中;
          5.使用SchemaExport類創建數據庫。
          6.創建序列實現自增字段:
                  <id name="id" type="java.lang.Long">
                      <column name="ID" precision="22" scale="0" />
                      <generator class="sequence">
                          <param name="sequence">SEQ_HB_USER_ID</param>
                      </generator>
                  </id>


          hibernate.cfg.xml:
          程序代碼 程序代碼

          <?xml version='1.0' encoding='UTF-8'?>
          <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

          <!-- Generated by MyEclipse Hibernate Tools.                   -->
          <hibernate-configuration>

              <session-factory>
                  <property name="connection.username">scott</property>
                  <property name="connection.url">
                      jdbc:oracle:thin:@127.0.0.1:1521:MGC
                  </property>
                  <property name="dialect">
                      org.hibernate.dialect.Oracle9Dialect
                  </property>
                  <property name="myeclipse.connection.profile">MGC</property>
                  <property name="connection.password">tiger</property>
                  <property name="connection.driver_class">
                      oracle.jdbc.driver.OracleDriver
                  </property>
                  <mapping resource="cn/edu/ahau/mgc/hibernate/simple/pojo/User.hbm.xml" />

              </session-factory>

          </hibernate-configuration>


          User.java:
          程序代碼 程序代碼

          package cn.edu.ahau.mgc.hibernate.simple.pojo;

          public class User {

              private int id;
              private String userName;
              private String password;

              public int getId() {
                  return id;
              }

              public void setId(int id) {
                  this.id = id;
              }

              public String getUserName() {
                  return userName;
              }

              public void setUserName(String userName) {
                  this.userName = userName;
              }

              public String getPassword() {
                  return password;
              }

              public void setPassword(String password) {
                  this.password = password;
              }

          }



          User.hbm.xml:
          程序代碼 程序代碼

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          <!-- 
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              <class name="cn.edu.ahau.mgc.hibernate.simple.pojo.User" table="HB_USER" schema="SCOTT">
                  <id name="id" type="java.lang.Long">
                      <column name="ID" precision="22" scale="0" />
                      <generator class="sequence">
                          <param name="sequence">SEQ_HB_USER_ID</param>
                      </generator>
                  </id>
                  <property name="username" type="java.lang.String">
                      <column name="USERNAME" length="10" not-null="true" />
                  </property>
                  <property name="password" type="java.lang.String">
                      <column name="PASSWORD" length="10" not-null="true" />
                  </property>
              </class>
          </hibernate-mapping>




          ExportToDB.java:
          程序代碼 程序代碼

          package cn.edu.ahau.mgc.hibernate.simple.export;

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

          public class ExportToDB {

              public static void main(String[] args) {
                  Configuration cfg = new Configuration().configure();
                  SchemaExport export = new SchemaExport(cfg);
                  export.create(true, true);
              }

          }

          posted on 2008-09-24 16:09 強強 閱讀(1830) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 上犹县| 双辽市| 城口县| 龙南县| 杭锦旗| 定州市| 电白县| 台东县| 辽中县| 安图县| 界首市| 涪陵区| 巧家县| 那曲县| 宜宾市| 二手房| 民和| 贵南县| 贵阳市| 南澳县| 苍南县| 崇阳县| 商城县| 巫溪县| 交城县| 泾源县| 珠海市| 五大连池市| 桂东县| 洪洞县| 清镇市| 阳高县| 汕头市| 宽城| 固始县| 龙陵县| 且末县| 浙江省| 张家川| 乳源| 奉新县|