Hibernate

 

       Hibernate是一個開放源代碼的對象關係映射框架(O/R對象關係模型),它對JDBC進行了很是輕量級的對象封裝,使得Java程序員能夠爲所欲爲的使用對象編程思惟來操縱數據庫。mysql

 

       Hibernate說簡單點就是把面向關係的編程(sql)轉化爲面向對象(接口)的方式。程序員

       它一端連得是面向對象的,一端連得是面向關係的。sql

        Hibernate就是對JDBC的封裝數據庫

 

  •        JDBC操做數據庫很繁瑣
  •        sql語句編寫不是面向對象的
  •        能夠在對象和關係之間創建關聯來簡化編程
  •        O/R Mapping 簡化編程
  •        O/R Mapping 跨越數據庫平臺

 

       O/R說簡單點就是有一大堆類庫,使用面向對象的方式調用,而後被自動翻譯成面向關係的語言。編程

 

 

 

配置有xml和Annotation兩種session

Annotation更好用app

 

 

XML方式框架

public class Student {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }    
}

 

先配Student.hbm.xmlide

表示模型與數據表的對照ui

<?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.bjsxt.hibernate.Student">
        <id name="id" />
        <property name="name" />
        <property name="age" />
    </class>    
</hibernate-mapping>

 

再配hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">bjsxt</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.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">update</property>

        <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

 

 

Annotation方式

 

@Entity
public class Teacher {
    private int id;
    private String name;
    private String title;
    
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

 

不須要Student.hbm.xml這個配置文件

hibernate.cfg.xml的配置前面都同樣,只須要最後引入一句便可

<mapping class="com.bjsxt.hibernate.Teacher"/>
相關文章
相關標籤/搜索