HibernateTemplate常用的方法
Serializable save(Object entity): 添加操作
void update(Object entity) : 修改操作
void delete (Object entity) :删除操作
<T> T get(Class <T> entityClass,Serializable id) ,根據id查詢
<T> T Ioad(Class<T> entityClass , Serializable id) ,根據id查詢
List find (String queryString ,Object...values) :查詢操作的方法
(1)第一個參數是hql語句
(2) 語句參數值
條件查詢
模糊查詢:
下面是HibernateTemplate具體的一些方法
/**
* 插入對象
*
* @param entity
* @return
*/
public int save(Object entity) {
return (Integer) hibernateTemplate.save(entity);
}
/**
* 删除對象
*
* @param entity
*/
public void delete(Object entity) {
hibernateTemplate.delete(entity);
}
/**
* 修改對象
*
* @param entity
*/
public void update(Object entity) {
hibernateTemplate.update(entity);
}
/**
* 根據ID查詢對象
*
* @param entityClass
* 類名,如:String.class
* @param id
* ID值
*/
public <T> T query(Class<T> entityClass, int id) {
return hibernateTemplate.get(entityClass, id);
}
/**
* 查詢全部
*
* @param className
* 類名
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryAll(Class<?> className) {
return hibernateTemplate.find("from " + className.getName());
}
/**
* 分頁
*
* @param entityClass
* 類名
* @param index
* 目前頁數
* @param size
* 每頁顯示的大小
* @param order
* 排序類型
* @param propertyName
* 要排序的屬性名
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> paging(final Class<?> entityClass, final int index,
final int size, final Order order, final String... propertyName) {
List<T> list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(entityClass);
if (propertyName != null && propertyName.length > 0) {
switch (order) {
case ASC:
criteria.addOrder(org.hibernate.criterion.Order
.asc(propertyName[0]));
break;
case DESC:
criteria.addOrder(org.hibernate.criterion.Order
.desc(propertyName[0]));
break;
}
}
criteria.setFirstResult((index - 1) * size);
criteria.setMaxResults(size);
return criteria.list();
}
});
return list;
}
@SuppressWarnings("unchecked")
public long getSumRecord(final Class<?> className) {
List<Long> count = new ArrayList<Long>();
count = hibernateTemplate.find("select COUNT(*) from "
+ className.getName());
return count.size() > 0 ? (count.get(0)) : 0L;
}
/**
* 擷取總頁數
*
* @param className
* 類名
* @param size
* 每頁顯示的大小
* @return
*/
@SuppressWarnings("unchecked")
public long sumPage(final Class<?> className, int size) {
List<Long> count = new ArrayList<Long>();
count = hibernateTemplate.find("select COUNT(*) from "
+ className.getName());
return count.size() > 0 ? (count.get(0) - 1 + size) / size : 0L;
}
/**
* 根據字段查詢
*
* @param params
* 字段清單
* @param values
* 值清單
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryByProperty(final Class<?> className,
final Map<String, Object> params) {
return hibernateTemplate.executeFind(new HibernateCallback() {
public List<?> doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(className);
for (String field : params.keySet())
criteria.add(Property.forName(field).eq(params.get(field)));
return criteria.list();
}
});
}
/**
* 根據字段查詢可以分的頁數
*
* @param className
* 要查詢的實體類
* @param params
* 屬性清單
* @param size
* 每頁顯示的大小
* @return
*/
@SuppressWarnings("unchecked")
public long queryByPropertyGetSumPage(final Class<?> className,
final Map<String, Object> params, final int size) {
List<Integer> object = hibernateTemplate
.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(className);
for (String field : params.keySet())
criteria.add(Property.forName(field).eq(
params.get(field)));
criteria.setProjection(Projections.rowCount());
return criteria.list();
}
});
int count = object == null ? 0 : object.get(0);
return count > 0 ? (count + size - 1) / size : 0L;
}
/**
* 擷取總記錄數根據屬性
*
* @param className
* @param params
* @param size
* @return
*/
@SuppressWarnings("unchecked")
public long queryByPropertyGetSumRecord(final Class<?> className,
final Map<String, Object> params) {
List<Integer> object = hibernateTemplate
.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(className);
for (String field : params.keySet())
criteria.add(Property.forName(field).eq(
params.get(field)));
criteria.setProjection(Projections.rowCount());
return criteria.list();
}
});
return object == null ? 0 : object.get(0);
}
/**
* 根據字段查詢并分頁顯示
*
* @param className
* 要分頁的實體類
* @param params
* 字段清單
* @param index
* 目前頁
* @param size
* 每頁顯示的大小
* @param order
* 排序
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryByPropertyPaging(final Class<?> className,
final Map<String, Object> params, final int index, final int size,
final Order order, final String... field) {
return hibernateTemplate.executeFind(new HibernateCallback() {
public List<?> doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(className);
for (String f : params.keySet())
criteria.add(Property.forName(f).eq(params.get(f)));
if (field != null && field.length != 0) {
switch (order) {
case ASC:
criteria.addOrder(org.hibernate.criterion.Order
.asc(field[0]));
break;
case DESC:
criteria.addOrder(org.hibernate.criterion.Order
.desc(field[0]));
break;
}
}
criteria.setFirstResult((index - 1) * size);
criteria.setMaxResults(size);
return criteria.list();
}
});
}
/**
* 儲存或更新對象
*
* @param entity
* 對象
*/
public void saveOrUpdate(Object entity) {
hibernateTemplate.saveOrUpdate(entity);
}
/**
* 批量修改
*
* @param queryString
* HQL語句
* @return 受影響行數
*/
public int bulkUpdate(String queryString) {
return hibernateTemplate.bulkUpdate(queryString);
}
/**
* 批量修改
*
* @param queryString
* HQL語句
* @param values
* 參數的值
* @return 受影響行數
*/
public int bulkUpdate(String queryString, Object... values) {
return hibernateTemplate.bulkUpdate(queryString, values);
}
/**
* 批量删除
*
* @param collection
* 要删除的集合
*/
public void deleteAll(Collection<?> collection) {
hibernateTemplate.deleteAll(collection);
}
/**
* 模糊查詢
*
* @param className
* 類名
* @param field
* 字段名
* @param value
* 值
* @param matchMode
* 比對模式:ANYWHERE->任意位置、END->結束、START->開始、EXACT->精确比對
*/
@SuppressWarnings("unchecked")
public <T> List<T> Querylike(final Class<?> className, final String field,
final String value, final MatchMode matchMode) {
List objects = new ArrayList<Object>();
objects = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(className);
criteria.add(Property.forName(field).like(value, matchMode));
return criteria.list();
}
});
return objects;
}
/**
* 執行hql查詢語句
*
* @param hql
* hql語句
* @param values
* 值清單
* @return
*/
@SuppressWarnings("unchecked")
public <T> Object executeQuery(final String hql, final Object... values) {
return hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
setParams(query, values);
return query.list();
}
});
}
/**
* 查詢單個值
*
* @param hql
* hql語句
* @param values
* 參數清單
* @return 傳回單個值
*/
@SuppressWarnings("unchecked")
public <T> Object executeSacale(final String hql, final Object... values) {
return hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
setParams(query, values);
query.setMaxResults(1);
return query.uniqueResult();
}
});
}
/**
* 執行hql删除、修改語句
*
* @param hql
* hql語句
* @param values
* 值清單
* @return
*/
@SuppressWarnings("unchecked")
public int executNonQuery(final String hql, final Object... values) {
return hibernateTemplate.execute(new HibernateCallback() {
public Integer doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
setParams(query, values);
return query.executeUpdate();
}
});
}
/**
* 删除表資料
* @param tableName 表名
*/
@SuppressWarnings("unchecked")
public void truncate(final String tableName) {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
session.createSQLQuery("truncate table " + tableName).executeUpdate();
return new ArrayList();
}
});
}
private void setParams(Query query, Object... values) {
if (!isEmptyOrNull(values)) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
}
/**
* 判斷值是否為空或
*
* @param values
* @return
*/
private boolean isEmptyOrNull(Object... values) {
if (values == null || values.length == 0)
return true;
return false;
}