<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring ORM 簡介 > 原文: [https://javatutorial.net/introduction-to-spring-orm](https://javatutorial.net/introduction-to-spring-orm) 在本教程中,您將學習什么是 Spring ORM 以及如何使用它。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## 什么是 Spring ORM? Spring ORM 涵蓋了 Hibernate,iBatis 和 JPA 等許多技術。 Spring 提供了集成類,因此,所提到的每種技術都可以按照 Spring 的配置原則來實現。 推薦的集成方式是針對 Hibernate,JPA 和 JDO 制作 DAO。 因為所有內容都是作為一組可重用的 JavaBean 設計的,所以我們可以從 ORM 支持中提取與從庫中提取一樣多的功能。 ## 使用 ORM 的優點 由于我們使用名為“實體”的 java 對象映射到數據庫表,因此我們可以使用諸如繼承和封裝之類的 OOP 概念與環境進行交互。 這是一個巨大的好處,因為我們已經熟悉 OOP 概念,并且不必花時間學習新概念等。 * 更容易的測試 * 常見的數據訪問異常 * 一般資源管理 * 整合交易管理 要完全深入了解上述優點,請單擊此處以獲取文檔。 ## 實踐 首先,我們需要在 Spring 容器中設置`SessionFactory`。 這樣做是為了避免將應用程序對象與硬編碼的資源查找綁定在一起。 我們在應用程序上下文中將 Hibernate `SessionFactory`定義為 bean。 如果您不熟悉 bean,[可以單擊此處](https://javatutorial.net/introduction-to-spring-bean)。 ```java <beans> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> </property> </bean> </beans> ``` 以下示例顯示了 Spring 容器中的 DAO 定義,該定義引用了上面定義的`SessionFactory`和 DAO 方法實現的示例。 ```java <beans> <bean id="myProductDao" class="product.ProductDaoImpl"> <property name="sessionFactory" ref="mySessionFactory"/> </bean> </beans> ``` ```java public class ProductDaoImpl implements ProductDao { private HibernateTemplate hibernateTemplate; public void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); } public Collection loadProductsByCategory(String category) throws DataAccessException { return this.hibernateTemplate.find("from test.Product product where product.category=?", category); } } ``` 以上代碼段摘自[原始文檔](https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/orm.html)。 我們在這里所做的是,創建了一個實現`ProductDao`接口的`ProductDaoImpl cla`,并在其中包含`HibernateTemplate`實例變量以及`setSessionFactory()`和`loadProductsByCategory()`方法。 如您所見,在`setSessionFactory()`方法中,我們將`hibernateTemplate`設置為指定的`sessionFactory`。 ### 什么是`HibernateTemplate`? `HibernateTemplate`是一個類,可幫助簡化數據訪問代碼。 此類將`HibernateExceptions`轉換為`DataAccessExceptions`,這是未經檢查的異常。`HibernateTemplate`用于實現數據訪問或業務邏輯服務。 如果您對 ORM API 支持的所有方法感興趣,則可以查看 [Oracle javadoc](https://static.javadoc.io/org.springframework/spring-orm/5.0.8.RELEASE/index.html?overview-summary.html) 。
                  <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>

                              哎呀哎呀视频在线观看