【Hibernate】hibernate架構的搭建

來源:互聯網
上載者:User

標籤:clip   支援   輕量級   oct   over   開發   程式   encoding   統計   

1, Hibernate 是什麼

Hibernate架構簡化了java應用程式與資料庫互動的開發。 Hibernate是一個開源,輕量級的ORM(對象關係映射)工具。

2,Hibernate架構的優點

Hibernate架構有很多優點。它們分別如下:

  •     開源和輕量級: Hibernate架構是根據LGPL許可證和輕量級的開源工具。
  •     快速效能: Hibernate架構的效能很快,因為緩衝在Hibernate架構內部使用。 hibernate架構中有兩種類型的緩衝:一級緩衝和二級緩衝。一級緩衝預設是啟用的。
  •     資料庫獨立查詢: HQL(Hibernate查詢語言)是物件導向的SQL版本。 它產生資料庫獨立查詢。 所以你不需要編寫資料庫特定的查詢語句。 在Hibernate之前,如果項目更改了資料庫,我們需要更改SQL查詢,從而導致維護變得非常複雜。
  •     自動建立表: Hibernate架構提供了自動建立資料庫表的功能。 因此,無需手動在資料庫中建立表。
  •     簡化複雜串連: 在hibernate架構中可輕鬆擷取多個表中的資料。
  •     提供查詢統計和資料庫狀態: Hibernate支援查詢快取,並提供有關查詢和資料庫狀態的統計資訊。
3, Hibernate 架構的搭建

首先下載Hibernate的包,然後和資料庫驅動包一起到匯入到項目中。

3.1 註解方式

首先看一看項目結構:

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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>        <property name="connection.url">            jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8        </property>        <property name="connection.username">root</property>        <property name="connection.password">517839</property>        <property name="connection.driver_class">            com.mysql.jdbc.Driver        </property>                <!-- 載入映射描述資訊 -->        <mapping class="cn.test.bean.User" />            </session-factory></hibernate-configuration>
hibernate.cfg.xml

User.java檔案

package cn.test.bean;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="user")//表示對應的表名public class User {        @Id//表示主鍵    @Column(name="uid")//對應表中的欄位名    private Integer id;        @Column(name="uname")//對應表中的欄位名    private String name;        @Column(name="upass")//對應表中的欄位名    private String password;        public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    @Override    public String toString() {        return "User [id=" + id + ", name=" + name + ", password=" + password + "]";    }}
User.java

HibernateUtil.java檔案

package cn.test.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {        public static Session getSession(){        Configuration conf = new Configuration();        conf.configure("hibernate.cfg.xml");//讀取串連參數和映射描述資訊        SessionFactory factory = conf.buildSessionFactory();        Session session = factory.openSession();        return session;    }}
HibernateUtil.xml

UserTest.java檔案

package cn.test.test;import org.hibernate.Session;import org.junit.Test;import cn.test.bean.User;import cn.test.util.HibernateUtil;public class UserTest {    @Test    public void testName1() throws Exception {        Session session  = HibernateUtil.getSession();        User user = (User)session.get(User.class, 1);        if(user != null){            System.out.println(user);        }else{            System.out.println("未找到記錄");        }        session.close();    }}
UserTest.java

這裡的方法: session.get(User.class, 1); 是Hibernate架構封裝好的一個類,他表示查詢資料表中主鍵為1的值,並且將結果反射到User的對象中。

3.2 非註解的方式

如果是非註解的方式的話,我們只需要把上面的user.java檔案替換調,並且加上User.hbm.xml檔案,再在hibernate.cfg.xml檔案中改一改映射關係就可以了。

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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>        <property name="connection.url">            jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8        </property>        <property name="connection.username">root</property>        <property name="connection.password">517839</property>        <property name="connection.driver_class">            com.mysql.jdbc.Driver        </property>                <!-- 載入映射描述資訊 -->        <mapping resource="cn/test/hbm/User.hbm.xml" />            </session-factory></hibernate-configuration>
hibernate.cfg.xml

User.hbm.xml檔案

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <!-- 指定User類對應user表 -->    <class name="cn.test.bean.User" table="user">                <!-- 指定no屬性對應userid欄位,類型為integer,主鍵 -->        <id name="id" column="uid" type="integer"></id>                <property name="name" column="uname" type="string"></property>                <property name="password" column="upass" type="string"></property>            </class></hibernate-mapping>
User.hbm.xml

User.java檔案

package cn.test.bean;public class User {    private Integer id;    private String name;    private String password;        public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    @Override    public String toString() {        return "User [id=" + id + ", name=" + name + ", password=" + password + "]";    }}
User.java

HibernateUtil.java檔案

package cn.test.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {        public static Session getSession(){        Configuration conf = new Configuration();        conf.configure("hibernate.cfg.xml");//讀取串連參數和映射描述資訊        SessionFactory factory = conf.buildSessionFactory();        Session session = factory.openSession();        return session;    }    }
HibernateUtil.java

UserTest.java檔案

package cn.test.test;import org.hibernate.Session;import org.junit.Test;import cn.test.bean.User;import cn.test.util.HibernateUtil;public class UserTest {    @Test    public void testName1() throws Exception {        Session session  = HibernateUtil.getSession();        User user = (User)session.get(User.class, 1);        if(user != null){            System.out.println(user);        }else{            System.out.println("未找到記錄");        }        session.close();    }}
UserTest.java

 

【Hibernate】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.