📄 informationdaohibernate.java
字号:
package com.longtime.wap.module.information.dao.hibernate;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.longtime.wap.common.BaseDao;
import com.longtime.wap.common.web.Page;
import com.longtime.wap.model.Information;
import com.longtime.wap.module.information.common.InformationQueryBean;
import com.longtime.wap.module.information.dao.InformationDao;
/**
* 定义对information的持久化类进行操作的接口
*
* @author Bulc
* @date Nov 15, 2007
*/
public class InformationDaoHibernate extends BaseDao implements InformationDao {
/**
* 根据查询条件返回信息列表
*
* @param queryBean
* 查询条件对象
* @param searchType
* 搜索类型
* @param page
* 分页对象
* @return 信息列表
*/
public List retrieveInformationsByCondition(
final InformationQueryBean queryBean,
final String searchType,
final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"from Information information where ");
if (null != queryBean.getSearchValue()
&& queryBean.getSearchValue().length() > 0) {
hql.append("information." + searchType
+ " like :searchValue and ");
}
if (null != queryBean.getCpId()) {
hql.append("information.business.cp.cpId=:cpId and ");
}
if (null != queryBean.getBusinessId()) {
hql.append("information.business.businessId=:businessId "
+ "and ");
}
if (hql.toString().endsWith(" where ")) {
hql.delete(hql.length() - 6, hql.length());
}
if (hql.toString().endsWith(" and ")) {
hql.delete(hql.length() - 4, hql.length());
}
hql.append(" order by information_id asc");
Query query = session.createQuery(hql.toString());
query.setProperties(queryBean);
return query.setFirstResult(page.getFirstResult())
.setMaxResults(page.getPageSize()).list();
}
});
}
/**
* 根据查询条件返回信息总数
*
* @param queryBean
* 查询条件对象
* @param searchType
* 搜索类型
* @return 信息总数
*/
public int retrieveInformationsCount(final InformationQueryBean queryBean,
final String searchType) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Information info where ");
if (null != queryBean.getSearchValue()
&& queryBean.getSearchValue().length() > 0) {
hql.append("info." + searchType
+ " like :searchValue and ");
}
if (null != queryBean.getCpId()) {
hql
.append("info.business.cp.cpId=:cpId and ");
}
if (null != queryBean.getBusinessId()) {
hql.append("info.business.businessId=:businessId "
+ "and ");
}
if (hql.toString().endsWith(" where ")) {
hql.delete(hql.length() - 6, hql.length());
}
if (hql.toString().endsWith(" and ")) {
hql.delete(hql.length() - 4, hql.length());
}
Query query = session.createQuery(hql.toString());
query.setProperties(queryBean);
return query.uniqueResult();
}
});
}
/**
* 根据信息id,搜索信息列表
*
* @param ids
* 信息id列表
* @return 信息列表
*/
public List retrieveInformationsByIds(final String[] ids) {
return (List) this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"from Information where ");
for (int i = 0; i < ids.length; i++) {
hql.append("informationId=" + ids[i]);
if (i < ids.length - 1) {
hql.append(" or ");
}
}
Query query = session.createQuery(hql.toString());
return query.list();
}
});
}
/**
* 批量更新信息
*
* @param informations
* 信息列表
*/
public void updateInformations(List informations) {
this.getHibernateTemplate().saveOrUpdateAll(informations);
}
/**
* 更新信息
*
* @param information
* 信息对象
*/
public void updateInformation(Information information) {
this.getHibernateTemplate().update(information);
}
/**
* 创建一个information
*
* @param information
* information对象
*/
public void createInformation(Information information) {
this.getHibernateTemplate().save(information);
}
/**
* 批量删除信息
*
* @param informations
* 信息列表
*/
public void deleteInformations(List informations) {
this.getHibernateTemplate().deleteAll(informations);
}
/**
* 根据信息id,搜索信息
*
* @param id
* 信息id
* @return 信息对象
*/
public Information retrieveInformationById(Long id) {
return (Information) getHibernateTemplate().get(Information.class, id);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -