當前位置:首頁 » 操作系統 » hibernate資料庫操作

hibernate資料庫操作

發布時間: 2022-12-09 09:41:56

A. Hibernate:hibernate操作資料庫的時候一定要用到對象嗎我只是單純的想把資料庫的內容輸出怎麼辦

Hibernate的原理是創建一個跟你資料庫里的表對應的實體類,在對資料庫進行操作時,可以通過對這個實體類的屬性進行操作,然後利用hibernate來對資料庫進行數據操作·····你假如想用hibernate而又不想創建實體類的話,那就不大可能的事,因為hibernate就是面向對象的,這種查詢方法也叫HQL(面向對象語句).

B. hibernate中,操作資料庫,比如修改,刪除等操作,一定要使用開啟事務並提交事務的方式嗎

Hibernate中修改、刪除、添加操作要開啟事務,查詢操作不要開啟事務,原因是:修改、刪除、添加要對資料庫中持久化數據進行變動,而查詢操作不要對資料庫中的數據進行更改所以不要開啟事務。

C. hibernate怎樣查詢資料庫裡面的所有數據

Hibernate查詢所有數據的操作方式有三種。
1、Query
(1)使用該方法查詢時,不需要編寫sql語句,但是需要編寫hql(Hibernate Query Language)語句,該語句是Hibernate查詢語言。
(2)hql語言操作的是實體類和實體類的屬性,比如查詢所有數據的hql語句為:from 實體類名稱。
(3)使用方法:首先創建Query對象,然後調用該對象的List方法返回數據集合。

@Test
public void test11(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getFactory();
session = sessionFactory.getCurrentSession();
tx = session.beginTransaction();
/**
* 使用session對象的createQuery方法創建Query對象。
* 參數為hql語句
* 使用QUERY對象的list方法獲取數據集合
*/
Query query =session.createQuery("from UserEntity");
List<UserEntity> list = query.list();
//使用forEach遍歷集合
for (UserEntity userEntity : list) {
System.out.println(userEntity);
}
tx.commit();
} catch (Exception e) {
tx.rollback();
}finally{
sessionFactory.close();
}
}

2、criteria
(1)使用該對象不需要寫hql語句,只需要指定實體類。
(2)使用方法:首先創建criteria對象,然後調用list返回數據集合。

@Test
public void test12(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getFactory();
session = sessionFactory.getCurrentSession();
tx = session.beginTransaction();
/**
* 使用session對象的createCriteria方法創建criteria對象。
* 使用criteria對象的list方法獲取數據集合
*/
Criteria criteria =session.createCriteria(UserEntity.class);
List<UserEntity> list = criteria.list();
//使用forEach遍歷集合
for (UserEntity userEntity : list) {
System.out.println(userEntity);
}
tx.commit();
} catch (Exception e) {
tx.rollback();
}finally{
sessionFactory.close();
}
}

3、SQLQuery
(1)使用該對象,需要寫底層的SQL語句。
(2)實現方法:首先創建該對象,然後調用list。

@Test
public void test13(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getFactory();
session = sessionFactory.getCurrentSession();
tx = session.beginTransaction();
/**
* 使用session對象的createSQLQuery方法創建SQLQuery對象。
* 使用qQLQuery對象的list方法獲取數據集合,集合裡面不是對象,而是數組
*/
SQLQuery qQLQuery =session.createSQLQuery("select * from t_user");
List<Object[]> list = qQLQuery.list();
//使用forEach遍歷集合
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
tx.commit();
} catch (Exception e) {
tx.rollback();
}finally{
sessionFactory.close();
}
}

(3)數組轉換成對象

@Test
public void test13(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getFactory();
session = sessionFactory.getCurrentSession();
tx = session.beginTransaction();
/**
* 使用session對象的createSQLQuery方法創建SQLQuery對象。
* 使用qQLQuery對象的list方法獲取數據集合,集合裡面不是對象,而是數組
*/
SQLQuery qQLQuery =session.createSQLQuery("select * from t_user");
//將數組裝載進實體中
qQLQuery.addEntity(UserEntity.class);
List<UserEntity > list = qQLQuery.list();
//使用forEach遍歷集合
for (UserEntity userEntity : list) {
System.out.println(userEntity);
}
tx.commit();
} catch (Exception e) {
tx.rollback();
}finally{
sessionFactory.close();
}
}

熱點內容
多看怎麼上傳雲 發布:2024-04-25 15:45:31 瀏覽:38
山東ftp 發布:2024-04-25 15:44:46 瀏覽:260
怎麼用ios玩安卓區 發布:2024-04-25 15:40:33 瀏覽:921
內網搭建ftp伺服器 發布:2024-04-25 15:35:26 瀏覽:968
伺服器硬體搭建 發布:2024-04-25 15:33:49 瀏覽:791
騰訊招聘php 發布:2024-04-25 15:17:02 瀏覽:444
雲伺服器多個公網ip搭建 發布:2024-04-25 15:13:15 瀏覽:845
phpmysqlupdate 發布:2024-04-25 15:08:15 瀏覽:253
隨時解壓 發布:2024-04-25 14:58:11 瀏覽:68
三率源碼 發布:2024-04-25 14:42:41 瀏覽:468