<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ? ??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; } 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數據庫: ![](https://box.kancloud.cn/2016-02-18_56c53c0fd900f.jpg) ? ? 運行ExportDB的main方法,結果如圖: ![](https://box.kancloud.cn/2016-02-18_56c53c0fee366.jpg) ![](https://box.kancloud.cn/2016-02-18_56c53c100bd78.jpg) ? ??
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看