溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何對Hibernate進行增刪改查操作

發(fā)布時間:2020-11-10 17:30:00 來源:億速云 閱讀:216 作者:Leah 欄目:編程語言

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何對Hibernate進行增刪改查操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1.首先我們要知道什么是Hibernate

Hibernate是一個輕量級的ORMapping對象。主要用來實現(xiàn)Java和數(shù)據(jù)庫表之間的映射,除此之外還提供數(shù)據(jù)查詢和數(shù)據(jù)獲取的方法,

可以大幅度減少開發(fā)時人工使用SQL和JDBC處理數(shù)據(jù)的時間,解放編程人員95%的任務。

2.什么是ORM  Object-Relational-Mapping對象關(guān)系映射

ORM:是通過java對象映射到數(shù)據(jù)庫表,通過操作Java對象可以完成對數(shù)據(jù)表的操作。(假如你用的是Dbutils那么還需要在Java類中寫sql語句,而orm就不用)

Hibernate是一個完全的ORM框架只需要對對象的操作即可生成底層的SQL。

接下來直接進入主題:

先看看使用hibernate的基本流程!下面是簡單的流程圖

 如何對Hibernate進行增刪改查操作

1.創(chuàng)建項目:

用myeclipse創(chuàng)建一個web project

2.導入hibernate相關(guān)的架包到項目

 如何對Hibernate進行增刪改查操作

第三步: 配置hibernate

在src目錄下新建一個xml文件,名稱為hibernate.cfg.xml(當然,你也可以不叫這個名稱,不過在代碼中要作相應的修改),拷貝如下內(nèi)容:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <!-- 配置會話工廠 hibernate 核心 管理數(shù)據(jù)庫連接池 -->
 <session-factory>
  <!-- 1.配置數(shù)據(jù)庫連接參數(shù) -->
  <!-- 1.1配置jdbc四個基本連接參數(shù) -->
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql:///hibernateexec</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <!-- 1.2配置 hibernate使用的方言 -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <!-- 2.配置其他相關(guān)屬性 -->
  <!-- 2.1自動建表 -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- 2.2在日志中輸出sql -->
  <property name="hibernate.show_sql">true</property> 
  <!-- 2.3格式化sql -->
  <property name="hibernate.format_sql">true</property> 
  <!-- 開啟事務 -->
  <property name="hibernate.connection.autocommit">true</property>
  <!-- 配置c3p0數(shù)據(jù)庫連接池 -->
  <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">50</property>
  <property name="hibernate.c3p0.timeout">120</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>  
  <!-- 3.加載映射文件 -->
  <mapping resource="com/study/model/Customer.hbm.xml"/> 
 </session-factory> 
 </hibernate-configuration>

這里提醒一點:customer表你可以不用去手動創(chuàng)建,但是數(shù)據(jù)庫hibernateexec是要你手動創(chuàng)建的

第四步.創(chuàng)建實體和映射文件

public class Customer {
 private int id;
 private String name;
 private int age;
 private String city;
 private String addr;
}
/*
 * 提供set和get方法
 */
Customer 實體

映射文件和實體對象在同一個包下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
 <!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
 <!-- 完成實體類 和數(shù)據(jù)表的映射 -->
 <!-- 1.類與表的映射 -->
 <!-- 
  name 要映射的完整類名
  table 映射到數(shù)據(jù)庫的表名
  catalog 映射到數(shù)據(jù)庫的名字
 -->
 <class name="com.study.model.Customer" table="customer" catalog="hibernateexec">
  <!-- 2.類中屬性 和表中 數(shù)據(jù)列的映射 -->
  <!-- 2.1主鍵 -->
  <!-- 
  name 屬性名(類中)
  column 列名(表中)
  type 數(shù)據(jù)類型
  -->
  <id name="id" column="id" type="int">
  <!-- 配置主鍵生成策略 主鍵自動增長-->
  <generator class="identity"></generator>
  </id>
  <!-- 2.2 普通屬性 -->
  <!-- 
  name 屬性名(類中)
  column 列名(表中)
  type 數(shù)據(jù)類型(也可以直接寫String)
  -->
  <property name="name" column="name" type="java.lang.String"></property> 
  <property name="age" column="age" type="int"></property>
  <!-- 也可以分開寫 -->
  <property name="city">
  <column name="city" sql-type="varchar(20)"></column>
  </property>
  <!-- 如果什么都不寫,那就默認類的屬性名和數(shù)據(jù)庫中的列名一致都為addr,類型為varchar -->
 <property name="addr"></property> 
 </class>
 </hibernate-mapping>

Customer.hbm.xml

第五步:創(chuàng)建SessionFactory對象

第六步:獲取Session對象進行相關(guān)操作

第五步和第六步我和在一起,第六步我們發(fā)現(xiàn)不論增刪改查前面四步都是一樣的,我們其實可以提取到一個工具類,再來調(diào)用這樣加快效率。

import java.util.List;
 import org.hibernate.Query;
 import org.hibernate.SQLQuery;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 import org.junit.Test;
 import com.study.model.Customer;
 public class HibernateTest { 
 /*
  * 保存數(shù)據(jù)
  */
  @Test
  public void testInsert() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務
  Transaction transaction = session.beginTransaction();
  // 編寫自己的邏輯代碼
  Customer customer = new Customer();
  customer.setName("小黃");
  customer.setAge(40);
  customer.setCity("北京");
  // 直接保存
  session.save(customer);
  // 提交事務
  transaction.commit();
  session.close();
  sessionFactory.close();
  } 
 //查詢所有的
 @Test
 public void testFindAllByHQL(){
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務
  Transaction transaction = session.beginTransaction();
  //編寫HQL語句(面向類和屬性的查詢 
  String hql =" from Customer";//這里是Customer不是表名 是類名 查詢Customer
  Query query =session.createQuery(hql);
  List<Customer> customers=query.list();
  System.out.println(customers);
  // 提交事務
  transaction.commit();
 session.close();
  sessionFactory.close();
 }
 // 刪除
 @Test
 public void testDelete() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務
  Transaction transaction = session.beginTransaction();
 Customer customer =new Customer();
 customer.setId(2);
  session.delete(customer);
  // 提交事務
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 // 修改
 @Test
 public void testUpdate() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務
  Transaction transaction = session.beginTransaction();
  Customer customer = (Customer) session.get(Customer.class, 2);
  customer.setCity("杭州");
  session.update(customer);
  // 提交事務
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 // 查詢 根據(jù)id查詢
 @Test
 public void testFindById() {
  // 實例化配置對象 加載映射文件 加載 hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 創(chuàng)建會話工廠
  SessionFactory sessionFactory = configuration.buildSessionFactory();
  // 創(chuàng)建會話
  Session session = sessionFactory.openSession();
  // 開啟事務
  Transaction transaction = session.beginTransaction();
  Customer customer = (Customer) session.get(Customer.class, 1);
  System.out.println(customer);
  // 提交事務
  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 }

運行效果:當你運行第一個增加用戶的時候,運行結(jié)束數(shù)據(jù)庫會自動創(chuàng)建customer表格,和往表格里添加數(shù)據(jù)。

如何對Hibernate進行增刪改查操作

這樣就通過hibernate進行基礎(chǔ)的增刪改查了。

上述就是小編為大家分享的如何對Hibernate進行增刪改查操作了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI