日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate配置(核心配置文件.cfg.xml 和映射配置文件.hbm.xml)(对象-关系映射 Object Relational Mapping)

發(fā)布時間:2023/12/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate配置(核心配置文件.cfg.xml 和映射配置文件.hbm.xml)(对象-关系映射 Object Relational Mapping) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

分層體系結(jié)構(gòu):
耦合性(依賴)
持久化層(數(shù)據(jù)訪問層)
對象-關(guān)系映射(Object Relational Mapping,簡稱ORM),是隨著面向?qū)ο蟮能浖_發(fā)方法發(fā)展而產(chǎn)生的。用來把域模型表示的對象映射到關(guān)系數(shù)據(jù)模型對應(yīng)的數(shù)據(jù)庫結(jié)構(gòu)中去。

Hibernate是什么?

  • 在分層體系結(jié)構(gòu)中Hibernate位于持久層,是完成對象持久化的持久層框架
  • Hibernate是連接Java應(yīng)用程序和關(guān)系型數(shù)據(jù)庫的框架,能夠建立對象模型和關(guān)系數(shù)據(jù)模型之間的映射,是一種自動ORM框架;
  • Hibernate是對JDBC API的封裝,是JDBC輕量級封裝框架

  • 注意核心配置文件.cfg.xml 和映射配置文件.hbm.xml的根目錄:

    1. .cfg.xml 根目錄:hibernate-configuration
    2. .hbm.xml 根目錄: hibernate-mapping

    <?xml version="1.0" encoding="UTF-8"?> <!--表明解析本XML文件的DTD文檔位置,DTD是Document Type Definition 的縮寫,即文檔類型的定義,XML解析器使用DTD文檔來檢查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3軟件包中的src\org\hibernate目錄中找到此文件--> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!--Hibernate配置文件的根元素,其他文件要包含在其中--><hibernate-configuration><!--SessionFactory是Hibernate中的一個類,這個類主要負(fù)責(zé)保存Hibernate的配置信息,以及對session的操作--> <session-factory><!--hibernate.dialect 只是Hibernate使用的數(shù)據(jù)庫方言,就是要用Hibernate連接那種類型的數(shù)據(jù)庫服務(wù)器。--><property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!--配置數(shù)據(jù)庫的驅(qū)動程序,Hibernate在連接數(shù)據(jù)庫時,需要用到數(shù)據(jù)庫的驅(qū)動程序--> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!--設(shè)置數(shù)據(jù)庫的連接url:jdbc:mysql://localhost:3306/dbname,其中l(wèi)ocalhost表示mysql服務(wù)器名稱,此處為本機, dbname是數(shù)據(jù)庫名--> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/anno?characterEncoding=UTF-8</property><!-- 連接數(shù)據(jù)庫時數(shù)據(jù)的傳輸字符集編碼方式 --><property name="hibernate.connection.characterEncoding">UTF-8</property><!--連接數(shù)據(jù)庫時的用戶名--> <property name="hibernate.connection.username">root</property><!--連接數(shù)據(jù)庫時的密碼--> <property name="hibernate.connection.password"></property><!--是否在后臺顯示Hibernate用到的SQL語句,開發(fā)時設(shè)置為true,便于差錯,程序運行時可以在Eclipse的控制臺顯示Hibernate的執(zhí)行Sql語句。項目部署后可以設(shè)置為false,提高運行效率--> <property name="hibernate.show_sql">true</property><!-- 格式化輸出的Sql語句 --><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><!-- 使用ThreadLocal管理Session --> <property name="hibernate.current_session_context_class">thread</property><!--指定映射文件,可映射多個映射文件--> <mapping class="com.test.entity.Product" /><!-- <mapping resource="com/test/entity/Product.hbm.xml"/> --> </session-factory> </hibernate-configuration> <!-- 文檔定義類型 --> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 映射屬性 --> <hibernate-mapping package="com.test.entity"><class name="Product" table="products" lazy="true"> <!-- lazy 延遲加載 --><id name="id" column="id" > <!-- column列 --><generator class="identity"> <!-- generator主鍵生成方式 --></generator></id><!-- property除主鍵之外的屬性 column字段 --><property name="name" column="name" ><!-- <column name="" sql-type=""></column> --></property><property name="price" column="price"></property><property name="description" column="description" ></property></class> </hibernate-mapping>

    總結(jié):

    Configuration cfg=new Configuration().configure();SessionFactory factory=cfg.buildSessionFactory();Session session=factory.openSession();Transaction tran=session.beginTransaction();

    Hibernate API操作流程

  • 加載Hibernate核心配置文件,創(chuàng)建Configuration對象;
  • Configuration cfg=new Configuration().configure().

  • 創(chuàng)建SessionFactory對象,該對象創(chuàng)建耗費資源,創(chuàng)建一次即可;
  • SessionFactory factory=cfg.buildSessionFactory();

  • 創(chuàng)建Session對象;
  • Session session=factory.openSessin();

  • 開啟事務(wù);
  • Transaction tran=session.beginTransaction();

  • 執(zhí)行增刪改查操作;
  • save()、update()、delete()、get()/load()

  • 提交事務(wù)
  • tran.commit();

  • 關(guān)閉事務(wù)/關(guān)閉session/關(guān)閉sessionFactory
  • close();

    Hibernate核心類和接口

    orm思想 對象-關(guān)系映射(Object Relational Mapping)

    Hibernate創(chuàng)建過程

    在這里插入代碼片

    public class Testcx {/*** 04.04 測試類--->主要測試程序功能實現(xiàn)情況*/public Testcx() {// TODO Auto-generated constructor stub}public static void main(String[] args) {save();//saveProducts();//updateProduct();//deleteProduct();// findAllProduct();// findProductByprice(22);//find();}/** * 實現(xiàn)商品列表功能(根據(jù)價格查詢)--->該方法參數(shù)是價格,根據(jù)參數(shù)查詢表中滿足條件的數(shù)據(jù); hibernate中查詢語句占位符的下標(biāo)是從0開始的;*/public static void saveProducts() {Product p1 = new Product();p1.setName("沙田柚");p1.setDescription("香");p1.setPrice(11);Product p2 = new Product();p2.setName("包子");p2.setDescription("可以有");p2.setPrice(22);ProductService ps = new ProductService();ps.save(p1, p2);System.out.println(p1.getName());}private static void save() {Session session = null;Transaction tran = null;try {session = HibernateUtil.getCurrentSession();tran = session.beginTransaction();// ---------------------------------Product p1 = new Product();p1.setName("桂林米粉");p1.setDescription("香");p1.setPrice(11);Product p2 = new Product();p2.setName("豬肉脯");p2.setDescription("可以有");p2.setPrice(22);session.save(p1);session.save(p2);// ---------------------------------tran.commit();} catch (Exception e) {e.printStackTrace();tran.rollback();} finally {if (null != session) {session.close();}}}private static void find() {// TODO Auto-generated method stub// TODO Auto-generated method stubSession session = null;Transaction tran = null;Product p = null;try {session = HibernateUtil.getCurrentSession();tran = session.beginTransaction();// ---------------------------------// 查找Product對象p = session.load(Product.class, 2);System.out.println(p.getDescription());p.setName("蘋果");// ---------------------------------tran.commit();} catch (Exception e) {e.printStackTrace();tran.rollback();} finally {if (null != session) {session.close();}}}/** * 實現(xiàn)商品刪除功能*/public static void deleteProduct() {// TODO Auto-generated method stubSession session = null;Transaction tran = null;try {session = HibernateUtil.getCurrentSession();tran = session.beginTransaction();// ---------------------------------// 查找Product對象Product p = session.load(Product.class, 2);//如果查詢的id不存在會報異常 get方法會報NullSystem.out.println(p.getDescription());session.delete(p);// ---------------------------------tran.commit();} catch (Exception e) {e.printStackTrace();tran.rollback();} finally {if (null != session) {session.close();}}}/** 實現(xiàn)商品修改功能* */public static void updateProduct() {Session session = null;Transaction tran = null;try {session = HibernateUtil.getCurrentSession();tran = session.beginTransaction();// ---------------------------------// 查找Product對象Product p = session.load(Product.class, 1);p.setDescription("這個東西難得的好吃");session.update(p);System.out.println(p.getDescription());// 保存// session.save(p);// ---------------------------------tran.commit();} catch (Exception e) {e.printStackTrace();tran.rollback();} finally {if (null != session) {session.close();}}}}

    總結(jié)

    以上是生活随笔為你收集整理的Hibernate配置(核心配置文件.cfg.xml 和映射配置文件.hbm.xml)(对象-关系映射 Object Relational Mapping)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。