欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
hibernate 入門(mén)學(xué)習 Demo

下面以一個(gè)簡(jiǎn)單的例子描述hibernate的使用。

 

軟件配置:jdk1.6+hibernate4.1+oracle10g

 

1.hibernate包下載

 

先到hibernate官網(wǎng)下載hibernate的開(kāi)發(fā)包hibernate-search-4.1.1.Final-dist.zip,解壓縮此包,dist\lib目錄下是開(kāi)發(fā)所需的jar包。

 

2.在eclipse中創(chuàng )建java項目

 

項目創(chuàng )建后,需要引入依賴(lài)的jar包:

 

hibernate包:required下全部jar包,provided下的hibernate-jpa-2.0-api-1.0.1.Final.jar和jta-1.1.jar包

oracle包:ojdbc6.jar

日志包(可選,如果不添加則看不到hibernate的日志輸出):slf4j-api-1.6.3.jar、logback-core-1.0.0.jar、logback-classic-1.0.0.jar 

 

創(chuàng )建包:test,test.hibernate,test.model

 

3.創(chuàng )建數據庫表

 

本示例使用oracle,其他數據庫的特性和sql可參考一下代碼稍作改變即可:

 

創(chuàng )建House表:

 

Sql代碼  
  1. CREATE TABLE House  
  2. (  
  3.     id       NUMBER(8) NOT NULL,  
  4.     name     VARCHAR2(50),  
  5.     address  VARCHAR2(50) NOT NULL  
  6. )  
  7. ;  
  8.   
  9.   
  10. ALTER TABLE House ADD CONSTRAINT PK_House   
  11.     PRIMARY KEY (id)   
  12.  USING INDEX   
  13. ;  
  14.   
  15. CREATE SEQUENCE SEQ_House_id   
  16.     INCREMENT BY 1   
  17.     START WITH 1   
  18.     NOMAXVALUE   
  19.     MINVALUE 1   
  20.     NOCYCLE   
  21.     NOCACHE   
  22.     NOORDER  
  23. ;  

 

 創(chuàng )建Person表:

 

Sql代碼  
  1. CREATE TABLE Person  
  2. (  
  3.     id    NUMBER(8) NOT NULL,  
  4.     name  VARCHAR2(50),  
  5.     age   NUMBER(3)  
  6. )  
  7. ;  
  8.   
  9.   
  10. ALTER TABLE Person ADD CONSTRAINT PK_Person   
  11.     PRIMARY KEY (id)   
  12.  USING INDEX   
  13. ;  

 

4.編寫(xiě)hibernate配置文件

 

在src目錄下創(chuàng )建hibernate.cfg.xml文件:

 

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>  
  8.         <property name="connection.username">hi</property>  
  9.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  10.         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>  
  11.         <property name="connection.password">hi</property>  
  12.         <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>  
  13.           
  14.         <property name="current_session_context_class">thread</property>  
  15.         <!-- this will show us all sql statements -->  
  16.         <property name="hibernate.show_sql">true</property>  
  17.           
  18.         <!-- mapping files -->  
  19.         <span style="color: #ff6600;"><mapping resource="test/model/House.hbm.xml"/></span>  
  20.         <span style="color: #ff6600;"><mapping class="test.model.Person"/></span>  
  21.     </session-factory>  
  22. </hibernate-configuration>  

 

注意看上面的mapping節點(diǎn):第一個(gè)使用的是mapping映射文件,第二個(gè)使用java注解映射方式。這兩個(gè)文件將在后面創(chuàng )建。

 

5.編寫(xiě)SessionFactoryUtil類(lèi)

 

在test.hibernate包下建立SessionFactoryUtil類(lèi),此類(lèi)用于獲取session:

 

Java代碼  
  1. package test.hibernate;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.AnnotationConfiguration;  
  6.   
  7. public class SessionFactoryUtil {  
  8.       
  9.     private static org.hibernate.SessionFactory sessionFactory;  
  10.     private SessionFactoryUtil() {  
  11.     }  
  12.       
  13.     static {  
  14.         // 使用xml文件或者注解方式加載hibernate配置  
  15.         sessionFactory = new AnnotationConfiguration().configure()  
  16.                 .buildSessionFactory();  
  17.         // 只是用xml文件方式加載hibernate配置  
  18.         // sessionFactory = new Configuration().configure().buildSessionFactory();  
  19.     }  
  20.       
  21.     public static SessionFactory getInstance() {  
  22.         return sessionFactory;  
  23.     }  
  24.       
  25.     /** 
  26.      * 打開(kāi)會(huì )話(huà)但不綁定到會(huì )話(huà)上下文中 
  27.      * @return the session 
  28.      */  
  29.     public Session openSession() {  
  30.         return sessionFactory.openSession();  
  31.     }  
  32.   
  33.     /** 
  34.      * 從會(huì )話(huà)上下文中返回會(huì )話(huà),如果上下文中不存在會(huì )話(huà)示例則先創(chuàng )建一個(gè)會(huì )話(huà)示例并保存到上下文中,然后再返回。 
  35.      * <br> 
  36.      * 會(huì )話(huà)上下文與hibernate配置中的current_session_context_class屬性值有關(guān)。 
  37.      * @return the session 
  38.      */  
  39.     public Session getCurrentSession() {  
  40.         return sessionFactory.getCurrentSession();  
  41.     }  
  42.       
  43.     /** 
  44.      * 關(guān)閉會(huì )話(huà)工廠(chǎng) 
  45.      */  
  46.     public static void close() {  
  47.         if (sessionFactory != null)  
  48.             sessionFactory.close();  
  49.         sessionFactory = null;  
  50.     }  
  51. }  

 

6.創(chuàng )建映射文件和類(lèi)

 

本例示范兩種方式的映射,mapping文件和java注解方式。

House采用mapping文件映射方式,Person采用java注解映射方式。

 

在test.model包下新建House類(lèi)(POJO):

 

Java代碼  
  1. package test.model;  
  2.   
  3. public class House {  
  4.     private Integer id;  
  5.     private String name;  
  6.     private String address;  
  7.     /** 
  8.      * @return the id 
  9.      */  
  10.     public Integer getId() {  
  11.         return id;  
  12.     }  
  13.     /** 
  14.      * @param id the id to set 
  15.      */  
  16.     public void setId(Integer id) {  
  17.         this.id = id;  
  18.     }  
  19.     /** 
  20.      * @return the name 
  21.      */  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.     /** 
  26.      * @param name the name to set 
  27.      */  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.     /** 
  32.      * @return the address 
  33.      */  
  34.     public String getAddress() {  
  35.         return address;  
  36.     }  
  37.     /** 
  38.      * @param address the address to set 
  39.      */  
  40.     public void setAddress(String address) {  
  41.         this.address = address;  
  42.     }  
  43.     /* (non-Javadoc) 
  44.      * @see java.lang.Object#toString() 
  45.      */  
  46.     @Override  
  47.     public String toString() {  
  48.         return "House [id=" + id + ", name=" + name + ", address=" + address  
  49.                 + "]";  
  50.     }  
  51.   
  52. }  

 

同時(shí)在此包下新建House.hbm.xml映射文件,通過(guò)此文件描述House類(lèi)和數據庫中的House表之間的映射關(guān)系:

 

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD  
  3. 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  4. <hibernate-mapping>  
  5.     <class name="test.model.House" table="house">  
  6.         <id name="id" column="id">  
  7.             <generator class="sequence">  
  8.                 <param name="sequence"><span style="color: #ff6600;">SEQ_HOUSE_ID</span></param>  
  9.             </generator>  
  10.         </id>  
  11.         <property name="name" column="name" />  
  12.         <property name="address" column="address" />  
  13.     </class>  
  14. </hibernate-mapping>  
 

house表在數據庫中使用名為SEQ_HOUSE_ID的序列作為主鍵id的值。如果表的主鍵生成方式不同,generator節點(diǎn)需要修改,網(wǎng)上有很多相關(guān)資料。

 

在test.model包下新建Person類(lèi)(POJO),并添加注解來(lái)表述語(yǔ)數據庫中Person表的映射關(guān)系,不做特殊說(shuō)明時(shí),默認數據庫中的名車(chē)和類(lèi)中名稱(chēng)一致。

 

Java代碼  
  1. package test.model;  
  2.   
  3. import javax.persistence.AttributeOverride;  
  4. import javax.persistence.Column;  
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Id;  
  7. import javax.persistence.Table;  
  8.   
  9. @Entity  
  10. @Table(name="PERSON")//表名稱(chēng)和類(lèi)名稱(chēng)相同時(shí)可以不添加此注解來(lái)說(shuō)明  
  11. public class Person {  
  12.     @Id  
  13.     private Integer id;  
  14.     private String name;  
  15.     @AttributeOverride(column = @Column, name = "AGE")//字段名稱(chēng)和屬性名稱(chēng)相同可以不添加此注解來(lái)說(shuō)明  
  16.     private int age;  
  17.       
  18.     /** 
  19.      * @return the id 
  20.      */  
  21.     public Integer getId() {  
  22.         return id;  
  23.     }  
  24.     /** 
  25.      * @param id the id to set 
  26.      */  
  27.     public void setId(Integer id) {  
  28.         this.id = id;  
  29.     }  
  30.     /** 
  31.      * @return the name 
  32.      */  
  33.     public String getName() {  
  34.         return name;  
  35.     }  
  36.     /** 
  37.      * @param name the name to set 
  38.      */  
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.     /** 
  43.      * @return the age 
  44.      */  
  45.     public int getAge() {  
  46.         return age;  
  47.     }  
  48.     /** 
  49.      * @param age the age to set 
  50.      */  
  51.     public void setAge(int age) {  
  52.         this.age = age;  
  53.     }  
  54. }  

 

7.編寫(xiě)測試方法

 

 

Java代碼  
  1. package test;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.Transaction;  
  5.   
  6. import test.hibernate.SessionFactoryUtil;  
  7. import test.model.House;  
  8. import test.model.Person;  
  9.   
  10. public class Test {  
  11. //  private static Logger logger = LoggerFactory.getLogger(Test.class);//slf4j logging  
  12.       
  13.     public static void main(String[] args) {  
  14.         Session session = SessionFactoryUtil.getInstance().getCurrentSession();  
  15.         Transaction tx = session.beginTransaction();  
  16.           
  17.         House house = new House();//瞬態(tài)  
  18.         house.setName("forest honey");  
  19.         house.setAddress("beijing");  
  20.           
  21.         session.save(house);//持久態(tài)  
  22.           
  23.         //對持久態(tài)對象進(jìn)行修改  
  24.         house.setAddress("yj1212");  
  25.           
  26.         Person person = new Person();//瞬態(tài)  
  27.         person.setId(1);  
  28.         person.setAge(24);  
  29.         person.setName("張三");  
  30.         session.save(person);//持久態(tài)  
  31.           
  32.         session.delete(person);//session關(guān)閉后此person便會(huì )成為托管態(tài)對象  
  33.           
  34.         tx.commit();//提交事務(wù),提交后會(huì )自動(dòng)關(guān)閉session  
  35.           
  36. //      session.close();  
  37.     }  
  38. }  
 

執行上面的main方法,控制臺輸出:

 

控制臺輸出代碼  
  1. Hibernate: select SEQ_HOUSE_ID.nextval from dual  
  2. Hibernate: insert into house (name, address, id) values (?, ?, ?)  
  3. Hibernate: insert into PERSON (age, name, id) values (?, ?, ?)  
  4. Hibernate: update house set name=?, address=? where id=?  
  5. Hibernate: delete from PERSON where id=?  
 

測試類(lèi)中紊亂的“業(yè)務(wù)邏輯”代碼只是為了說(shuō)明hibernate的使用方式,請忽略!呵呵。

 

hibernate擁有緩存機制,其中session便是一級緩存,還可以配置二級緩存。要想充分利用hibernate的優(yōu)勢,還需要深入了解hibernate的session,事務(wù),緩存,延遲加載,以及各種對象尤其是session的生命周期等。后續再慢慢總結。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Hibernate3.2 .1參考文檔
hibernate官方入門(mén)教程 (轉載)
Hibernate Annotations 實(shí)戰
Eclipse下Hibernate入門(mén)
Spring2.0與Hibernate3整合 - J2EE之巔 - BlogJava
Hibernate Tools使用說(shuō)明——Table to Pojo
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久