parent
cbc1b9adfd
commit
e9f81f3d7b
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,23 @@ |
||||
package com.app.bean; |
||||
|
||||
public class Count { |
||||
private String date; |
||||
|
||||
private Integer count; |
||||
|
||||
public String getDate() { |
||||
return date; |
||||
} |
||||
|
||||
public void setDate(String date) { |
||||
this.date = date; |
||||
} |
||||
|
||||
public Integer getCount() { |
||||
return count; |
||||
} |
||||
|
||||
public void setCount(Integer count) { |
||||
this.count = count; |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
package com.app.bean; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class CountSplit { |
||||
List<String> dateList; |
||||
List<Integer> countList; |
||||
|
||||
public CountSplit(List<String> dateList, List<Integer> countList) { |
||||
this.dateList = dateList; |
||||
this.countList = countList; |
||||
} |
||||
|
||||
public List<String> getDateList() { |
||||
return dateList; |
||||
} |
||||
|
||||
public void setDateList(List<String> dateList) { |
||||
this.dateList = dateList; |
||||
} |
||||
|
||||
public List<Integer> getCountList() { |
||||
return countList; |
||||
} |
||||
|
||||
public void setCountList(List<Integer> countList) { |
||||
this.countList = countList; |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
package com.app.service; |
||||
|
||||
import com.app.bean.Count; |
||||
|
||||
import java.util.List; |
||||
|
||||
public interface CountService { |
||||
//统计时间段
|
||||
int count(String startTime, String endTime,Class c); |
||||
//统计时间段并按日期分组
|
||||
List<Count> countWithDate(String startTime, String endTime, Class c); |
||||
} |
@ -0,0 +1,48 @@ |
||||
package com.app.service; |
||||
|
||||
import com.app.bean.Count; |
||||
import org.hibernate.HibernateException; |
||||
import org.hibernate.Query; |
||||
import org.hibernate.Session; |
||||
import org.hibernate.criterion.DetachedCriteria; |
||||
import org.hibernate.criterion.Projections; |
||||
import org.hibernate.criterion.Restrictions; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.orm.hibernate3.HibernateCallback; |
||||
import org.springframework.orm.hibernate3.HibernateTemplate; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.sql.SQLException; |
||||
import java.util.List; |
||||
|
||||
@Service |
||||
public class CountServiceImpl implements CountService{ |
||||
@Autowired |
||||
private HibernateTemplate hibernateTemplate; |
||||
|
||||
@Override |
||||
public int count(String startTime, String endTime,Class c) { |
||||
List list= hibernateTemplate.findByCriteria(DetachedCriteria.forClass(c). |
||||
add(Restrictions.ge("created",startTime)) |
||||
.add(Restrictions.le("created",endTime)) |
||||
.setProjection(Projections.rowCount())); |
||||
if(list.isEmpty()){ |
||||
return 0; |
||||
}else{ |
||||
return (Integer) list.get(0); |
||||
} |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
@Override |
||||
public List<Count> countWithDate(String startTime, String endTime, final Class c) { |
||||
return (List<Count>) hibernateTemplate.execute(new HibernateCallback() { |
||||
@Override |
||||
public Object doInHibernate(Session session) throws HibernateException, SQLException { |
||||
Query query=session.createSQLQuery( |
||||
"select substring(created,1,10) as date,count(*) as count from "+c.getSimpleName()+" group by substring(created,1,10)"); |
||||
return query.list(); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,5 @@ |
||||
|
||||
|
||||
|
||||
public class SpringTest { |
||||
} |
Loading…
Reference in new issue