對于JAVA開發高級,hibernate是java三大框架之一,足以見得它的重要性,那么對于hibernate的使用大家有了解多少呢?從今天開始我將帶領大家一道共同探討一下hibernate的知識,hibernate對于我們開發移動應用關聯數據庫十分方便,hibernate對于數據庫的操作十分便利,省去了很多之前開發時的不便。
Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序使用,也可以在Servlet/jsp的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。
Hibernate的核心和類接口一共有6個,分別為:session、SessionFactory、Transaction、Query、Criteria和Configuration。這6個核心和類接口在任何開發中都會用到。通過這些接口,不僅可以對持久化對象進行存取,還能夠進行事務控制。詳情見:<http://baike.baidu.com/link?url=KBUhIQIahzy0NfcFh-q3bu1_2hB9uHd1A5X5f_cDqoshxiQiXlkTR5b8r2ohme0hMzZl7ogoroZRQQMLuhDhNa>

下面開始我們本片內容的介紹:
第一步:安裝JAVA EE開發環境
JAVA EE 開發同樣使用ecipse,這里我提供一個下載地址:http://www.newasp.net/soft/71687.html=,感興趣的小童鞋可以去下載一下,安裝過程超級簡單,大家自己琢磨一下吧。
第二步:創建一個新項目
方法與我們創建web項目一致,左側右鍵單擊,選擇new PRoject,工程名大家可以自行設定,只要符合JAVA的命名規范即可。
第三步:導入我們可能使用到的jar包
這里小編沒沒有完全搞定,后續會將這部分更新,敬請期待!
第四步:配置Hibernate環境變量
a、hibernate.properties文件:
######################### Query Language ########################### define query language constants / function nameshibernate.query.substitutions yes 'Y', no 'N'## select the classic query parser#hibernate.query.factory_class org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory#################### Platforms ###################### MySQL#hibernate.dialect org.hibernate.dialect.MySQLDialecthibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialecthibernate.connection.driver_class com.mysql.jdbc.Driverhibernate.connection.url jdbc:mysql://127.0.0.1:3306/testhibernate.connection.username roothibernate.connection.passWord root## MS SQL Server#hibernate.dialect org.hibernate.dialect.SQLServerDialect#hibernate.connection.username sa#hibernate.connection.password sa#################################### Hibernate Connection Pool ####################################hibernate.connection.pool_size 1############################## C3P0 Connection Pool##############################hibernate.c3p0.max_size 2hibernate.c3p0.min_size 2hibernate.c3p0.timeout 5000hibernate.c3p0.max_statements 100hibernate.c3p0.idle_test_period 3000hibernate.c3p0.acquire_increment 2hibernate.c3p0.validate false################################# Miscellaneous Settings ################################### print all generated SQL to the console#hibernate.show_sql true## format SQL in log and consolehibernate.format_sql true## add comments to the generated SQL#hibernate.use_sql_comments true## generate statistics#hibernate.generate_statistics true## auto schema export#hibernate.hbm2ddl.auto create-drop#hibernate.hbm2ddl.auto createhibernate.hbm2ddl.auto update#hibernate.hbm2ddl.auto validate
注釋:C3P0 Connection Pool的注解詳見:http://baike.baidu.com/link?url=37RuZ_7cnA1F4S6iCZ3PP6f63H0SRBcUN3j1QSJ6EDmT1k_NFSQkP9PzJNVb7gykidNyROoL8mAq_K9OlcQqBK
hibernate.format_sql true的注解詳見:http://blog.csdn.net/zhengqiqiqinqin/article/details/8450875
hibernate.hbm2ddl.auto update的注解詳見:http://www.linuxidc.com/Linux/2011-12/49206.htm
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect:我們安裝的mysql數據庫版本聲明
第五步:創建我們的User對象類:
Hibernate的中數據的操作都是通過對象的形式進行的,故我們這里需要創建一個對象類,用于我們后續的操作。
public class User { private int id; private String name; private String pass; 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 getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }第六步:創建User.hbm.xml:
有了對象,我們需要通過這個文件把對象信息發送給我們的數據庫,為我們接下來的設計做好鋪墊。
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="po"> <class name="User"> <id name="id" column="c_id"> <generator class="native"></generator><!-- 設置主鍵 --> </id> <property name="name" column="c_name" length="20"/> <property name="pass" column="c_pass" length="20"/> </class></hibernate-mapping>
這個文件需要與我們對應的類放置到同一個文件夾中,并且該文件的文件名后綴一定不要進行改動。
第七步:配置hibernate.cfg.xml文件:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory name="foo"> <property name="show_sql">true</property> <mapping resource="po/User.hbm.xml"/> </session-factory></hibernate-configuration>
到這里我們的開發前的配置工作已經完成,對于他的使用,我將會在下一篇為大家介紹。
新聞熱點
疑難解答