Hibernate用於持久化的一個框架,但是在實際專案中進行查詢有很多的地方是需要進行一些封裝的,比如他的configure以及session不可能有一個運算元據庫呼叫dao層就建立一個,而且在獲取資料的時候有很多是相同的操作,那麼這些都需要進行一些封裝,以下為自個進行的一個簡易封裝,在前面筆記Hibernate入門筆記的基礎上做的一個簡易專案,建立HibernateUtils工具類,封裝程式碼如下
package com.HibernateUtils;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import com.whong.cust_customer;
public class HibernateUtils {
private static Session session;
private static SessionFactory sessionFactory;
private static Transaction transaction;
static {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
static {
session = sessionFactory.openSession();
}
static {
transaction = session.beginTransaction();
}
// 插入資料
public void insertData(String name,String phone) {
cust_customer cust_customer = new cust_customer();
cust_customer.setCust_name(name);
cust_customer.setCust_phone(phone);
session.save(cust_customer);
transaction.commit();
session.close();
}
// 查詢所有的資料
public void getAllData() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Query query = session.createQuery("from cust_customer");
List<cust_customer>list = query.list();
this.PrintListArray(list);
}
// 根據條件查詢(通過序列號1 2 3 等數字來進行查詢)
public void getDatawithName(String name) {
Query query = session.createQuery("from cust_customer where cust_name = ?");
query.setString(0, name);
List<cust_customer>list = query.list();
this.PrintListArray(list);
}
// 條件查詢(根據設定屬性名稱來進行查詢)
public void getDataWithNameAndPhone(String cust_name,String cust_phone) {
Query query = session.createQuery("from cust_customer where cust_name = :cust_name and cust_phone = :cust_phone");
query.setString("cust_name", cust_name);
query.setString("cust_phone", cust_phone);
List<cust_customer>list = query.list();
this.PrintListArray(list);
}
// 分頁查詢
public void getDataSeparater(Integer minipage,Integer maxpage ) {
Query query = session.createQuery("from cust_customer");
query.setFirstResult(minipage);
query.setMaxResults(maxpage);
List<cust_customer>list = query.list();
this.PrintListArray(list);
}
// criterita查詢所有資料
public void getDataCriterita() {
Criteria criteria = session.createCriteria(cust_customer.class);
List<cust_customer>list = criteria.list();
this.PrintListArray(list);
}
// criterita條件查詢
public void getDataCriteritaWithName(String cust_name) {
Criteria criteria = session.createCriteria(cust_customer.class);
criteria.add(Restrictions.eq("cust_name", cust_name));
List<cust_customer>list = criteria.list();
this.PrintListArray(list);
}
// criterita條件查詢
public void getDataCriteritaWithNameAndMobile(String name,String mobile) {
Criteria criteria = session.createCriteria(cust_customer.class);
criteria.add(Restrictions.eq("cust_name", name));
criteria.add(Restrictions.eq("cust_mobile", mobile));
List<cust_customer>list = criteria.list();
this.PrintListArray(list);
}
// criterita分頁查詢
public void getDataMinAndMax(Integer min,Integer max) {
Criteria criteria = session.createCriteria(cust_customer.class);
criteria.setFirstResult(min);
criteria.setMaxResults(max);
List<cust_customer>list = criteria.list();
this.PrintListArray(list);
}
// 列印所有的查詢結果
public void PrintListArray(List<cust_customer>list) {
for (int i = 0; i < list.size(); i++) {
cust_customer cust_customer = list.get(i);
System.out.println(cust_customer.getCust_id()+cust_customer.getCust_name()+cust_customer.getCust_mobile());
}
}
}
複製程式碼
在main中呼叫程式碼如下
package com.testPackage;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;
import com.HibernateUtils.HibernateUtils;
import com.whong.cust_customer;
public class testHibernate {
public static void main(String[] args) {
// TODO Auto-generated method stub
HibernateUtils hibernateUtils = new HibernateUtils();
// hibernateUtils.insertData("偉鴻", "185*******9");
// hibernateUtils.getAllData();
// hibernateUtils.getDatawithName("weihong");
// hibernateUtils.getDataWithNameAndPhone("偉鴻", "185******69");
// hibernateUtils.getDataSeparater(3, 10);
// hibernateUtils.getDataCriterita();
// hibernateUtils.getDataCriteritaWithName("weihong");
// hibernateUtils.getDataCriteritaWithNameAndMobile("偉鴻", "18******69");
hibernateUtils.getDataMinAndMax(3, 10);
}
}
複製程式碼