informationdaohibernate.java
来自「这个是完整的wap项目的源码 开发语言 Java 系统架构 Struts +」· Java 代码 · 共 271 行
JAVA
271 行
package com.longtime.wap.module.business.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.business.common.InformationQueryBean;
import com.longtime.wap.module.business.dao.InformationDao;
/**
* InformationDao层代码实现类
*
* @author bulc
* @date Nov 15, 2007
*/
public class InformationDaoHibernate extends
BaseDao implements InformationDao {
/**
* 列表分页显示出所有符合搜索条件的信息对象
*
* @param queryBean
* 搜索参数
* @param searchType
* 搜索类型
* @param isOverdue
* 判断是否超时
* @param page
* 分页参数
* @return 信息对象列表
*/
public List retrieveInformationsByCondition(
final InformationQueryBean queryBean, final String searchType,
final boolean isOverdue, 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.getIsPub()) {
if(queryBean.getIsPub() == 0) {
hql.append("information.isPub=:isPub and ");
} else if(queryBean.getIsPub() == 1) {
hql.append("information.isPub=:isPub and "
+ "(information.pubDate+"
+ "information.business.duration)>SYSDATE"
+ " and ");
}
}
if (null != queryBean.getFromDate()
&& queryBean.getFromDate().length() > 0
&& null != queryBean.getToDate()
&& queryBean.getToDate().length() > 0) {
hql.append("information.pubDate between TO_DATE"
+ "(:fromDate,'YYYY-MM-DD HH24:MI:SS') "
+ "and TO_DATE(:toDate,'YYYY-MM-DD "
+ "HH24:MI:SS') and ");
}
if (isOverdue) {
hql.append("(information.pubDate+"
+ "information.business.duration)<SYSDATE 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 isOverdue
* 判断是否超时
* @param searchType
* 搜索类型
* @return 信息数
*/
public int retrieveInformationsCount(final InformationQueryBean queryBean,
final boolean isOverdue, 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 information where ");
if (null != queryBean.getSearchValue()
&& queryBean.getSearchValue().length() > 0) {
hql.append("information." + searchType
+ " like :searchValue and ");
}
if (null != queryBean.getIsPub()) {
if(queryBean.getIsPub() == 0) {
hql.append("information.isPub=:isPub and ");
} else if(queryBean.getIsPub() == 1) {
hql.append("information.isPub=:isPub and "
+ "(information.pubDate+"
+ "information.business.duration)>SYSDATE"
+ " and ");
}
}
if (null != queryBean.getFromDate()
&& queryBean.getFromDate().length() > 0
&& null != queryBean.getToDate()
&& queryBean.getToDate().length() > 0) {
hql.append("information.pubDate between TO_DATE"
+ "(:fromDate,'YYYY-MM-DD HH24:MI:SS')"
+ "and TO_DATE"
+ "(:toDate,'YYYY-MM-DD HH24:MI:SS') "
+ "and ");
}
if (isOverdue) {
hql.append("(information.pubDate+"
+ "information.business.duration)<"
+ "SYSDATE 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();
}
});
}
/**
* 统计信息数
*
* @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 "
+ "information where ");
if (null != queryBean.getSearchValue()
&& queryBean.getSearchValue().length() > 0) {
hql.append("information." + searchType
+ " like :searchValue and ");
}
if (null != queryBean.getIsPub()) {
hql.append("information.isPub=:isPub and ");
}
if (null != queryBean.getFromDate()
&& queryBean.getFromDate().length() > 0
&& null != queryBean.getToDate()
&& queryBean.getToDate().length() > 0) {
hql.append("information.pubDate between "
+ "TO_DATE(:fromDate,'YYYY-MM-DD "
+ "HH24:MI:SS') and TO_DATE(:toDate,"
+ "'YYYY-MM-DD HH24:MI:SS') 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();
}
});
}
/**
* 获取信息对象列表
*
* @param ids
* 信息编号
* @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().saveOrUpdate(information);
}
/**
* 批量删除信息对象
*
* @param informations
* 信息对象列表
*/
public void deleteInformations(List informations) {
this.getHibernateTemplate().deleteAll(informations);
}
/**
* 获取信息对象
*
* @param id
* 信息编号
* @return 信息编号对应的信息对象
*/
public Information retrieveInformationById(Long id) {
return (Information) getHibernateTemplate().get(Information.class, id);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?