hibernate環境搭建

來源:互聯網
上載者:User

標籤:搭建

持久化概念:

持久化就是把資料(如記憶體中的對象)同步儲存到資料庫或某些存放裝置中(如磁碟檔案中、XML資料檔案中)等等。 
在軟體的分層體繫結構中,持久化層是與資料庫打交道的邏輯層.

JDBC-的問題:

代碼繁瑣:

一個插入對象的例子

public void addAccount(final Account account) throws DAOException,AccountAlreadyExistException{final Connection conn=getConnection();PreparedStatement pstmt=conn.prepareStatement(“insert into account values(?,?,?)”);pstmt.setString(1.account.getUserName));pstmt.setInt(2,account.getPassWord));pstmt.setString(3.account.getSex);pstmt.execute();conn.close();

如果account表有上百個欄位,則pstmt.setXX()語句要寫上百次,這明顯不合適


非物件導向

對於資料庫的操作不是物件導向,是面向關聯式資料庫的


ORM簡介

ORM(object relational mapping)是對象到關係的映射

它把對錶直接進行的操作變成對持久化類的屬性和方法的直接操作

ORM作為分層體系中的持久層.

ORM技術可以極大地提高開發效率和開發時間,同時開發品質也更容易得到保證


hibernate核心介面

Session、SessionFactory、Transaction、Query和Configuration


Session:

Session負責執行被持久化對象的CRUD操作(CRUD的任務是完成與資料庫的交流,包含了很多常見的SQL語句。

但需要注意的是Session對象是非安全執行緒的。同時,Hibernate的session不同於JSP應用中的HttpSession。

這裡當使用session這個術語時,其實指的是Hibernate中的session,而以後會將HttpSesion對象稱為使用者session。


SessionFactory

SessionFactroy負責初始化Hibernate。它充當資料存放區源的代理,並負責建立Session對象。

這裡用到了原廠模式。需要注意的是SessionFactory並不是輕量級的,因為一般情況下,

一個項目通常只需要一個SessionFactory就夠,當需要操作多個資料庫時,可以為每個資料庫指定一個SessionFactory。


Configuration

Configuration負責配置並啟動Hibernate,建立SessionFactory對象。

在Hibernate的啟動的過程中,Configuration類的執行個體首先定位映射文檔位置、讀取配置,然後建立SessionFactory對象。


Transaction

Transaction負責事務相關的操作。

它是可選的,可發人員也可以設計編寫自己的底層交易處理代碼.


Query:

Query負責執行各種資料庫查詢。它可以使用HQL語言或SQL語句兩種表達方式。


環境搭建:

依賴包:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo</groupId><artifactId>my-hibernate</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>3.6.10.Final</version></dependency><dependency><groupId>org.hibernate.javax.persistence</groupId><artifactId>hibernate-jpa-2.0-api</artifactId><version>1.0.0.Final</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.18.2-GA</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency><dependency><!-- 自己手動添加到maven本地倉庫 --><groupId>oracle</groupId><artifactId>ojdbc</artifactId><version>14</version></dependency></dependencies></project>

注意:ojdbc14.jar需要手動添加到maven本地倉庫,自訂座標

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"><!-- 顯示sql --><property name="show_sql">true</property><property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property><property name="hibernate.connection.username">diankun</property><property name="hibernate.connection.password">diankun</property><!-- 產生sql語句採用的文法 --><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- 自動建立表結構 --><property name="hibernate.hbm2ddl.auto">create</property><!-- bean與表的映射 --><mapping resource="com/demo/model/Student.hbm.xml"/></session-factory></hibernate-configuration>

Student:

package com.demo.model;public class Student {private int studentId ;private String studentName ;private int age;public int getStudentId() {return studentId;}public void setStudentId(int studentId) {this.studentId = studentId;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Student.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 package="com.demo.model">    <class name="Student" table="t_student">        <id name="studentId" column="student_id">            <generator class="sequence"/>        </id>        <property name="studentName" column="student_name"/>        <property name="age" />    </class></hibernate-mapping>

Test

package com.demo;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;public class StudentTest {SessionFactory sessionFactory ;@Beforepublic void init(){sessionFactory=new Configuration().configure().buildSessionFactory();}@Testpublic void createTest(){}}


運行mvn test ,資料庫自動產生表結構





hibernate環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.