我的第一个用Hibernate框架写的小例子
??? ?????????????????????????????? ?Hibernate框架
? 今天我來主要講一下Hibernate框架的配置步驟,在講之前,我們先了解一下使用框架的優(yōu)勢
1.不再考慮公共問題,框架已經(jīng)幫我們做好了
2.可以專心于業(yè)務(wù)邏輯,保證核心業(yè)務(wù)邏輯的開發(fā)質(zhì)量
3.結(jié)構(gòu)統(tǒng)一,便于學(xué)習(xí)和維護
4.框架中集成了前人的經(jīng)驗,可以幫助新手寫出穩(wěn)定、性能優(yōu)良而且結(jié)構(gòu)優(yōu)美的高質(zhì)量程序。
? 我們都知道SSH集成框架指的是基于Struts或Struts2+Spring+Hibernate的技術(shù)框架,也就是我們常說的三大框架,使用這個集成框架將使我們應(yīng)用程序更加健壯、穩(wěn)固、輕巧和優(yōu)雅。
? Hibernate是數(shù)據(jù)持久化(數(shù)據(jù)持久化就是將內(nèi)存中的數(shù)據(jù)模型轉(zhuǎn)換為存儲模型,以及將存儲模型轉(zhuǎn)換為內(nèi)存... 然后根據(jù)框架的配置文件,自動或手動決定什么時候把這種保存提交到數(shù)據(jù)庫)工具,是一個開放源代碼的對象關(guān)系映射框架。
優(yōu)點:
1.Hibernate功能強大,較之JDBC方式操作數(shù)據(jù)庫,代碼量大大減少,提高持久化代碼的開發(fā)速度,降低維護成本。
2.Hibernate支持許多面向?qū)ο蟮奶匦?/span>
3.可移植性好
4.Hibernate框架開源免費
缺點:
1.不適合以數(shù)據(jù)為中心的大量使用存儲過程的應(yīng)用。
2.大規(guī)模的批量插入、修改和刪除不適合用HibernateHibernate環(huán)境搭建
1.下載和部署jar包
2.創(chuàng)建配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!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><!-- Database connection settings 數(shù)據(jù)庫連接配置--><property name="connection.driver_class">oracle.jdbc.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><property name="connection.username">wth</property><property name="connection.password">1509184562</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect 方言--><property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- Echo all executed SQL to stdout 在控制臺打印后臺sql語句--><property name="show_sql">true</property><!-- 格式化語句 --><property name="format_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><!-- 關(guān)聯(lián)小配置 --><mapping resource="cn/entity/student.hbm.xml" /></session-factory></hibernate-configuration>3.創(chuàng)建持久化類和映射文件
小配置:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.entity"><class name="Student" table="STUDENT"><id name="sId" column="SID"><generator class="native"/></id><!-- assigned 程序員賦值 native后臺DB賦值--><property name="sName" type="string" column="SNAME"/><property name="sAge"/> </class> </hibernate-mapping>持久化類
package cn.test;import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session;import cn.entity.Student;public class Test {public static void main(String[] args) {Student stu=new Student();stu.setsName("呵");stu.setsAge(12);Configuration cfg=new Configuration().configure();SessionFactory sc = cfg.buildSessionFactory();Session se = sc.openSession();Transaction tx=se.beginTransaction();se.save(stu);tx.commit();System.out.println("success!!!");} }?
轉(zhuǎn)載于:https://www.cnblogs.com/wth1129/p/5730302.html
總結(jié)
以上是生活随笔為你收集整理的我的第一个用Hibernate框架写的小例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。