Hibernate中實現(xiàn)增刪改查的步驟詳解
1.首先我們要知道什么是Hibernate
Hibernate是一個輕量級的ORMapping對象。主要用來實現(xiàn)Java和數(shù)據(jù)庫表之間的映射,除此之外還提供數(shù)據(jù)查詢和數(shù)據(jù)獲取的方法,
可以大幅度減少開發(fā)時人工使用SQL和JDBC處理數(shù)據(jù)的時間,解放編程人員95%的任務(wù)。
2.什么是ORM Object-Relational-Mapping對象關(guān)系映射
ORM:是通過java對象映射到數(shù)據(jù)庫表,通過操作Java對象可以完成對數(shù)據(jù)表的操作。(假如你用的是Dbutils那么還需要在Java類中寫sql語句,而orm就不用)
Hibernate是一個完全的ORM框架只需要對對象的操作即可生成底層的SQL。
接下來直接進入主題:
先看看使用hibernate的基本流程!下面是簡單的流程圖
1.創(chuàng)建項目:
用myeclipse創(chuàng)建一個web project
2.導入hibernate相關(guān)的架包到項目
第三步: 配置hibernate
在src目錄下新建一個xml文件,名稱為hibernate.cfg.xml(當然,你也可以不叫這個名稱,不過在代碼中要作相應(yīng)的修改),拷貝如下內(nèi)容:
<?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> <!-- 配置會話工廠 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> <!-- 開啟事務(wù) --> <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 實體
映射文件和實體對象在同一個包下:
<?xml version="1.0" encoding="UTF-8"?> <!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(); // 開啟事務(wù) Transaction transaction = session.beginTransaction(); // 編寫自己的邏輯代碼 Customer customer = new Customer(); customer.setName("小黃"); customer.setAge(40); customer.setCity("北京"); // 直接保存 session.save(customer); // 提交事務(wù) 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(); // 開啟事務(wù) 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); // 提交事務(wù) 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(); // 開啟事務(wù) Transaction transaction = session.beginTransaction(); Customer customer =new Customer(); customer.setId(2); session.delete(customer); // 提交事務(wù) 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(); // 開啟事務(wù) Transaction transaction = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class, 2); customer.setCity("杭州"); session.update(customer); // 提交事務(wù) 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(); // 開啟事務(wù) Transaction transaction = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class, 1); System.out.println(customer); // 提交事務(wù) transaction.commit(); session.close(); sessionFactory.close(); } }
運行效果:當你運行第一個增加用戶的時候,運行結(jié)束數(shù)據(jù)庫會自動創(chuàng)建customer表格,和往表格里添加數(shù)據(jù)。
這樣就通過hibernate進行基礎(chǔ)的增刪改查了。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
SpringBoot后端進行數(shù)據(jù)校驗JSR303的使用詳解
這篇文章主要介紹了SpringBoot后端進行數(shù)據(jù)校驗JSR303的使用詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03SpringBoot中的@CacheEvict 注解的實現(xiàn)
本文主要介紹了SpringBoot中的@CacheEvict注解的實現(xiàn),@CacheEvict 注解用于清空緩存,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2024-03-03OpenFeign調(diào)用服務(wù)請求頭丟失Token的解決
這篇文章主要介紹了OpenFeign調(diào)用服務(wù)請求頭丟失Token的解決方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06一文深入理解Java中的java.lang.reflect.InvocationTargetException錯誤
這篇文章主要給大家介紹了關(guān)于Java中java.lang.reflect.InvocationTargetException錯誤的相關(guān)資料,java.lang.reflect.InvocationTargetException是Java中的一個異常類,它通常是由反射調(diào)用方法時拋出的異常,需要的朋友可以參考下2024-03-03Spring Boot高級教程之Spring Boot連接MySql數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了Spring Boot高級教程之Spring Boot連接MySql數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交
這篇文章主要為大家詳細介紹了Spring MVC接口防數(shù)據(jù)篡改和重復(fù)提交,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08