持久化概念:
持久化就是把數據(如內存中的對象)同步保存到數據庫或某些存儲設備中(如磁盤文件中、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:Session、SessionFactory、Transaction、Query和Configuration
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>
Testpackage 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 ; @Before public void init(){ sessionFactory=new Configuration().configure().buildSessionFactory(); } @Test public void createTest(){ } }
運行mvn test ,數據庫自動生成表結構