<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Hibernate 4 入門教程 > 原文: [https://javabeginnerstutorial.com/hibernate/hibernate-4-introduction/](https://javabeginnerstutorial.com/hibernate/hibernate-4-introduction/) ### Hibernate 4 簡介 在本文中,我將向您展示如何使用 Hibernate 4 和一個簡單的示例應用來嘗試您所了解的內容。 ### 什么是 Hibernate,我為什么要關心? 創建 Hibernate 是為了利用 Java 應用和關系數據庫之間的連接,因為很難在數據庫表和 Java 對象之間來回映射。 而且由于 Hibernate 這樣做,它減少了 JDBC 查詢執行和數據映射所消耗的開發時間。 ### 獲取 Hibernate 要獲取最新版本的 Hibernate,只需訪問[此站點](https://sourceforge.net/projects/hibernate/files/hibernate4/)。 對于本文,我將使用`4.3.10.Final`版本。 如果下載并解壓縮了包,則可以在`lib`文件夾中看到一些子文件夾。 使用 Hibernate 的任何項目都需要`required`下的所有內容。 其他文件夾包含特殊情況的庫。 例如,在`jpa`下,您可以找到提供 JPA 實體管理器支持的庫。 另外,您可以設置一個 Maven 項目并將 Hibernate 添加為依賴項。 在這種情況下,您無需擔心 Hibernate 的其他必需依賴關系,這些依賴關系在捆綁下載的*必需*包中附帶。 使用 Maven 更簡單明了,因此我將使用 Maven 作為依賴項管理。 ### 一個簡單的示例 在簡單的示例中,我將創建一個 Java 應用,該應用將有關書籍的信息存儲在數據庫中。 為了簡單起見,該數據庫將是 H2 內存數據庫。 依賴關系是使用 Maven 管理的,輸出是具有所有依賴關系的可執行 JAR。 #### 實體 我將存儲在數據庫中的實體如下: ```java package example; public class Book { private String isbn; private String title; private String author; Book() { } public Book(String isbn, String title, String author) { this.isbn = isbn; this.title = title; this.author = author; } public String getIsbn() { return this.isbn; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } public void setIsbn(String isbn) { this.isbn = isbn; } } ``` 無參數構造器是所有持久類的必需條件,因為 Hibernate 每次反射都會創建對象實例。 在這種情況下,此構造器是私有的,以防止創建沒有信息的書籍。 ### 依賴項 為了使應用運行,我們在項目中需要兩個依賴項:Hibernate 和 H2。 為此,將以下內容添加到`pom.xml`中: ```java <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.10.Final</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.187</version> </dependency> </dependencies> ``` 現在我們準備繼續。 ### 配置 Hibernate Hibernate 需要一些配置才能開始。 您需要將其包含在`hibernate.cfg.xml`文件中。 它是普通的舊 XML。 它包含數據庫連接屬性和實體映射文件的包含位置。 ```java <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.h2.Driver</property> <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> <property name="connection.username">sa</property> <property name="connection.password"/> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.H2Dialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="hibernate_example/hbm/Book.hbm.xml"/> </session-factory> </hibernate-configuration> ``` ### 實體映射 為了將正確的字段映射到數據庫中的正確列,Hibernate 需要實體的映射文件。 這些文件位于以實體名稱開頭的`.hbm.xml`文件中。 在此示例中,`Book.hbm.xml`。 ```java <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="hibernate_example"> <class name="Book" table="BOOKS"> <id name="isbn" type="string"/> <property name="title"/> <property name="author"/> </class> </hibernate-mapping> ``` ### `main`方法 要在 Hibernate 中使用該應用,我們仍然需要一個入口點—在 Java 中,這是`main`方法。 首先,我們需要進行一些配置,例如使用會話工廠創建會話。因此,讓我們看一下代碼的運行方式: ```java public class Main { public static void main(String[] args) { final Configuration configuration = new Configuration().configure(); final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); final SessionFactory factory = configuration.buildSessionFactory(builder.build()); final Session session = factory.openSession(); final Book book = new Book("93939398948 ", "Java 8", "Author"); session.beginTransaction(); session.save(book); session.getTransaction().commit(); final List<Book> books = session.createCriteria(Book.class).list(); System.out.println("\n----\n"); System.out.println(MessageFormat.format("Storing {0} books in the database", books.size())); for (final Book b : books) { System.out.println(b); } System.out.println("\n----\n"); session.close(); factory.close(); } } ``` 運行該應用后,控制臺應具有一些日志消息,并且將一本書添加到數據庫中: - ```java Storing 1 books in the database Java 8 by JBT (ISBN:9393939894 ``` —– ## 總結 Hibernate 提供了一個很好的功能,可以利用 Java 對象和關系數據庫之間的映射。 當然,該示例應用并沒有顯示 Hibernate 的全部功能:為了獲得更好的用戶體驗,您可以添加一個用戶界面來在應用中創建和列出書籍。 在下一篇文章中,我將展示如何擺脫 XML 配置(某些開發人員稱為“XML 地獄”)并改為使用注解。 因此,請繼續關注。 [您可以在此處找到并下載應用的源碼](https://github.com/ghajba/hibernate_example/)。
                  <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>

                              哎呀哎呀视频在线观看