hibernate多條件動態查詢
阿新 • • 發佈:2019-02-03
在hibernate框架中,常規查詢方法是:
Configuration configuration = new Configuration();
configuration.configure();
Session session = configuration.buildSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery ("from Studnet where sid=:sid");
query.setParameter("sid", 1L);
List<Student> students = query.list();
for (Student student : students) {
//得到Student物件
System.out.println(student.getName());
}
transaction.commit();
}
但是在專案中,查詢條件往往有很多,某些查詢條件也不能確定,如果使用上面的方法,那麼就要寫大量基本重複的查詢方法.為了簡化工作量,封裝了一種通用的查詢方法,將查詢條件封裝到一個Map<String, Object>集合中, 動態的生成查詢語句傳送給資料庫得到查詢結果
下面這個是查詢表或者物件封裝的方法.
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class QueryTable {
SessionFactory sessionFactory;
{
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
}
/**
* 單表單條記錄查詢
* @param clazz
* @param varables
* @return
*/
public <T> T querySingleResult(Class<T> clazz, Map<String, Object> varables){
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
Query query = selectStatement(clazz, varables, session);
return (T) query.uniqueResult();
}
/**
* 單表多條記錄查詢
* @param className 要查詢的物件
* @param varables 封裝查詢條件的map
* @return 返回查詢結果的List集合
*/
public <T> List<T> queryResultList(Class<T> className, Map<String,Object> varables){
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
List<T> valueList = selectStatement(className, varables, session).list();
transaction.commit();
return valueList;
}
/**
* 拼接SQL查詢字串,得到Query並賦值查詢條件
* @param className
* @param varables
* @param session
* @return Query
*/
private <T> Query selectStatement(Class<T> className, Map<String,Object> varables, Session session) {
StringBuilder stringBuilder = new StringBuilder();
/*
* 通過className得到該實體類的字串形式,
*/
stringBuilder.append("from " + sessionFactory.getClassMetadata(className).getEntityName());
stringBuilder.append(" where 1=1 ");
/*
* 動態的拼接sql語句,如果一個屬性的值為"", 則不往條件中新增.
*/
for(Entry<String, Object> entry : varables.entrySet()){
if(!entry.getValue().equals("")){
stringBuilder.append(" and " + entry.getKey()+"=:" + entry.getKey());
}
}
Query query = session.createQuery(stringBuilder.toString());
/*
* 動態的給條件賦值
*/
for(Entry<String, Object> entry : varables.entrySet()){
if(!entry.getValue().equals("")){
query.setParameter(entry.getKey(), entry.getValue());
}
}
return query;
}
}
上面一共提供了兩個方法,一個返回單個結果,一個返回多個結果,Demo如下:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.ruinan.domain.Course;
import com.ruinan.domain.Student;
public class testQueryTable {
QueryTable queryTable = new QueryTable();
/**
* 多個結果查詢
* @throws InstantiationException
* @throws IllegalAccessException
*/
@Test
public void testQueryResultList() throws InstantiationException, IllegalAccessException {
/*
* 將查詢條件封裝到map中.
* 例 description=www
*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("description", "www");
// List<Student> students = queryClass(Student.class, map);
// for (Student student : students) {
// System.out.println(student.getName()+ "/" + student.getDescription());
// }
/*
* 得到查詢結果,是一個List集合
*/
List<Course> courses = queryTable.queryResultList(Course.class, map);
for (Course course : courses) {
System.out.println(course.getName());
}
}
/**
* 查詢單個結果
*/
@Test
public void testQuerySingleResult() {
/*
* 將查詢條件封裝到map中.
* 例 description=www
*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("sid", 3L);
/*
* 得到查詢結果
*/
Student student = queryTable.querySingleResult(Student.class, map);
System.out.println(student.getDescription());
}
}