获得条数:由于初次在hibernate中用distinct 先写个简单的sql试试。ok的。
public int getSendCount(final String userid) {
String sql = "select count( distinct t.ctid) from t_send t where t.userid='"
+ userid + "'";
int result = 0;
try {
result = Integer.parseInt(String.valueOf(getHibernateTemplate()
.getSessionFactory().getCurrentSession()
.createSQLQuery(sql).uniqueResult()));//这块我再想session是否关闭的问题
} catch (RuntimeException re) {
result = 0;
}
return result;
}
获得list分页:
public List<?> findSendList(final String userid, final int startPoint,
final int recordPerPage) {
List instances = (List) getHibernateTemplate().executeFind(
new HibernateCallback() {
/*
* 重载方法:@see
* org.springframework.orm.hibernate3.HibernateCallback
* #doInHibernate(org.hibernate.Session)
* 方法描述:(这里用一句话描述这个方法的作用) 适用条件:(这里描述这个方法适用条件 – 可选)
*
* @param session
*
* @return
*
* @throws HibernateException
*/
public Object doInHibernate(Session session)
throws HibernateException {
Criteria criteria = session.createCriteria(TSend.class);
if (StringUtils.isNotEmpty(userid)) {
criteria.add(Restrictions.eq("userid", userid));
}
ProjectionList projectionList = Projections
.projectionList();
projectionList.add(Projections.property("contentid"));
projectionList.add(Projections.property("ctid"));
criteria.setProjection(Projections
.distinct(projectionList));
criteria.setFirstResult(startPoint);
criteria.setMaxResults(recordPerPage);
List list = criteria.list();
List result = new ArrayList();
if (list != null && list.size() > 0) {
Iterator it = list.iterator();
while (it.hasNext()) {
Object[] obj = (Object[]) it.next();
TSend tSend = new TSend();
tSend.setContentid(obj[0] + "");
tSend.setCtid(obj[1] + "");
if ("1".equals(tSend.getCtid())) {
Criteria criteriaInfolist = session
.createCriteria(TTest.class);
criteriaInfolist.add(Restrictions.eq(
"infoid", tSend.getContentid()));
criteriaInfolist.add(Restrictions.eq(
"ctid", tSend.getCtid()));
TTest tTest = (TTest) criteriaInfolist
.uniqueResult();
tSend.settTest(tTest );
}
result.add(tSend);
}
}
return result;
}
});
if (instances == null) {
instances = new ArrayList();
}
return instances;
}