📄 cpdaohibernate.java
字号:
package com.longtime.wap.module.cp.dao.hibernate;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
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.Cp;
import com.longtime.wap.module.cp.common.ReportSQL;
import com.longtime.wap.module.cp.dao.CpDao;
/**
* CpDao层代码实现类
*
* @author liuzb
* @date Nov 15, 2007
*/
public class CpDaoHibernate extends BaseDao implements CpDao {
/**
* 查询单位表记录数
*
* @return 单位表的记录数
*/
public int retrieveCpsCount() {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Cp cp ");
Query query = session.createQuery(hql.toString());
return query.uniqueResult();
}
});
}
/**
* 查询业务表记录数
*
* @return 业务表的记录数
*/
public int retrieveBusinessCount() {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Business business ");
Query query = session.createQuery(hql.toString());
return query.uniqueResult();
}
});
}
/**
* 查询当前页单位列表
*
* @param page
* 分页参数
* @return 当前分页的单位列表
*/
public List retrieveCpsList(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder("from Cp cp ");
hql.append(" order by cp_id asc");
Query query = session.createQuery(hql.toString());
return query.setFirstResult(page.getFirstResult())
.setMaxResults(page.getPageSize()).list();
}
});
}
/**
* 查询单位列表
*
* @param ids
* 选择的编号列表
* @return 被选择的单位列表
*/
public List retrieveCpsByIds(final String[] ids) {
return (List) this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder("from Cp where ");
for (int i = 0; i < ids.length; i++) {
hql.append("cpId=" + ids[i]);
if (i < ids.length - 1) {
hql.append(" or ");
}
}
Query query = session.createQuery(hql.toString());
return query.list();
}
});
}
/**
* 查询单位
*
* @param id
* 单位的编号
* @return 输入编号的单位对象
*/
public Cp retrieveCpById(Long id) {
return (Cp) getHibernateTemplate().get(Cp.class, id);
}
/**
* 更新单位
*
* @param cp
* 单位对象
*/
public void updateCp(Cp cp) {
this.getHibernateTemplate().saveOrUpdate(cp);
}
/**
* 删除被选中的单位列表
*
* @param cps
* 单位列表
*/
public void deleteCps(List cps) {
this.getHibernateTemplate().deleteAll(cps);
}
/**
* 取得按单位统计的列表
*
* @param page
* 分页参数
* @param startDate
* 起始日期
* @param toDate
* 结束日期
* @return 结果列表
*/
public List retrieveReportByCp(final Page page, final String startDate,
final String toDate) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery sql = session.createSQLQuery(ReportSQL
.getReportSqlByCp(startDate, toDate));
sql.addScalar("COMPANY_NAME", Hibernate.STRING);
sql.addScalar("COUNT1", Hibernate.STRING);
sql.addScalar("COUNT2", Hibernate.STRING);
sql.addScalar("COUNT3", Hibernate.STRING);
sql.addScalar("COUNT4", Hibernate.STRING);
List list = sql.list();
return list;
}
});
}
/**
* 取得按业务统计的列表
*
* @param page
* 分页参数
* @param startDate
* 起始日期
* @param toDate
* 结束日期
* @return 结果列表
*/
public List retrieveReportByService(final Page page,
final String startDate, final String toDate) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery sql = session.createSQLQuery(ReportSQL
.getReportSqlByService(startDate, toDate));
sql.addScalar("CATEGORY", Hibernate.STRING);
sql.addScalar("COUNT1", Hibernate.STRING);
sql.addScalar("COUNT2", Hibernate.STRING);
sql.addScalar("COUNT3", Hibernate.STRING);
List list = sql.list();
return list;
}
});
}
/**
* 根据单位编号和单位编码取得单位记录数
*
* @param id
* 单位编号
* @param companyCode
* 单位代码
* @return 单位的记录数
*/
public int getCountByCompanyCode(final long id, final String companyCode) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Cp cp ");
hql.append("where cp_Id!=:cpId ");
hql.append("and company_Code=:companyCode ");
Query query = session.createQuery(hql.toString())
.setLong("cpId", id).setString("companyCode",
companyCode);
return query.uniqueResult();
}
});
}
/**
* 根据单位编号和单位编码取得单位记录数
*
* @param companyCode
* 单位代码
* @return 单位的记录数
*/
public int getCountByCompanyCode(final String companyCode) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Cp cp ");
hql.append("where company_Code=:companyCode ");
Query query = session.createQuery(hql.toString())
.setString("companyCode", companyCode);
return query.uniqueResult();
}
});
}
/**
* 根据单位编号和单位编码取得单位记录数
*
* @param id
* 单位编号
* @param companyName
* 单位名称
* @return 单位的记录数
*/
public int getCountByCompanyName(final long id, final String companyName) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Cp cp ");
hql.append("where cp_Id!=:cpId ");
hql.append("and company_Name=:companyName ");
Query query = session.createQuery(hql.toString())
.setLong("cpId", id).setString("companyName",
companyName);
return query.uniqueResult();
}
});
}
/**
* 根据单位编号和单位编码取得单位记录数
*
* @param companyName
* 单位名称
* @return 单位的记录数
*/
public int getCountByCompanyName(final String companyName) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(*) from Cp cp ");
hql.append("where company_Name=:companyName ");
Query query = session.createQuery(hql.toString())
.setString("companyName", companyName);
return query.uniqueResult();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -