转载 Hibernate中的时间函数
为了表示偶最近没有偷懒,我决定把我前两天写的DAO层的时间函数贴出来一下。
public List delCustomerLastTwelveMonth() {
List a = (List) new java.util.ArrayList();
Calendar c = Calendar.getInstance();
for (int i = 0; i < 12; i++) {
c.add(Calendar.MONTH, 0);
Date after = c.getTime();
c.add(Calendar.MONTH, -1);
Date before = c.getTime();
int number = doSomethingAboutDDate(before, after);
//调用函数doSomethingAboutDDate
a.add(number);
}
return a;
}
//实现一个月删除的用户数,调用此函数后可以用循环实现12个月
private int doSomethingAboutDDate(Date startdate, Date enddate) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String hql = " from TbDelCustomer u where" + " u.DDelDate > '"
+ dateFormat.format(startdate) + "' and u.DDelDate < '"
+ dateFormat.format(enddate) + "' ";
List users = this.getHibernateTemplate().find(hql);
return users.size();
}
//过去12个月每月新增的人数
public List newCustomerLastTwelveMonth() {
List a = (List) new java.util.ArrayList();
Calendar c = Calendar.getInstance();
for (int i = 0; i < 12; i++) {
c.add(Calendar.MONTH, 0);
Date after = c.getTime();
c.add(Calendar.MONTH, -1);
Date before = c.getTime();
int number = doSomethingAboutADate(before, after);
a.add(number);
}
return a;
}
//此函数能统计出每个月增加的用户数,同样可以调用后用循环实现12个每个月新增人数
private int doSomethingAboutADate(Date startdate, Date enddate) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String hql = " from TbCustomer u where" + " u.DCreateDate > '"
+ dateFormat.format(startdate) + "' and u.DCreateDate < '"
+ dateFormat.format(enddate) + "' ";
List users = this.getHibernateTemplate().find(hql);
return users.size();
}
//查询订单成功数目小于successfulOrder而生成订单数大于totalOrder的注册用户
public List searchCustomerByOrder(int successfulOrder, int totalOrder) {
// TODO Auto-generated method stub
String hql = "select l.ICustomerId ,l.CMobileNum,l.CCustomerName,u.IOrderCount," +
"u.IOrderOkCount from TbCustomerOrderLog u,TbCustomer l where u.ICustomerId=l.ICustomerId and u.IOrderCount>"
+ totalOrder + " and u.IOrderOkCount<" + successfulOrder;
Session session = (Session) getHibernateTemplate().getSessionFactory()
.openSession();
Query q = session.createQuery(hql);
List list = q.list();
return list;
}
//根据电话号码查询Customer的所有信息
public List searchCustomerByTelNumber(final String telNumber, final int startRow,final int pageSize) {
// TODO Auto-generated method stub
try {
return (List) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
String k = "'%" + telNumber + "%'";
String hql = " from TbCustomer u where u.CMobileNum like "+k;
List queriedResult = session.createQuery(hql)
.setFirstResult(startRow).setMaxResults(
pageSize).list();
Hibernate.initialize(queriedResult);
return queriedResult;
}
});
} catch (RuntimeException re) {
throw re;
}
}
//按电话号码查询,相匹配的总数。此函数总要是为JSF层的分页做准备
public int getCustomerRowCount(final String telNumber)
{
// TODO Auto-generated method stub
String k = "'%" + telNumber + "%'";
String hql="select count(*) from TbCustomer u where u.CMobileNum like "+k;
return (Integer)this.getHibernateTemplate().find(hql).get(0);
}
//给定月份内没有任何订单的用户。比如,传参是3,表示的就是距现在三个月内没有任何订单用户,返回用户的姓名和电话号码,封装成List.
public List searchNoOrderCustomer(int certainMouth) {
Date date1 = aboutFindDate(certainMouth);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String hql = "select l.CMobileNum ,l.CCustomerName from TbCustomerOrderLog u ,TbCustomer l where u.ICustomerId=l.ICustomerId and u.DLastOkOrderDate< '"
+ dateFormat.format(date1) + "' ";
Session session = (Session) getHibernateTemplate().getSessionFactory()
.openSession();
Query q = session.createQuery(hql);
List list = q.list();
return list;
}
}
//这个函数主要是给上个函数(给定月份没有订单的用户),此函数能将时间返回到我们所需要的点。假设我们输入3,而我们的现在的时间点2007年08月01日,通过这个函数,我们就能得到时间是2007年05月01日。
Date b = null;
Calendar c = Calendar.getInstance();
for (int i = 0; i < cerMouth; i++) {
c.add(Calendar.MONTH, -1);
b = c.getTime();
}
return b;
}
个人感觉DAO层不应该涉及到统计,只是当初师兄是这么定义的,我也就这么实现了,其实觉得一些东西可以放到Service层来做的。因为我在做Service的时候,发现很多东西我在DAO层实现,在Service层就不需要做,很快写完代码。
但是这样做的话Hibenate的好处好像就没有充分显出来。