获得条数:由于初次在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;
}
分享到:
相关推荐
MySQL通常使用GROUPBY(本质上是排序动作)完成DISTINCT操作,如果DISTINCT操作和ORDERBY操作组合使用,通常会用到临时表.这样会影响性能. 在一些情况下,MySQL可以使用索引优化DISTINCT操作,但需要活学活用.本文涉及一个...
有这样的一个需求:select count(distinct nick) from user_access_xx_xx; 这条sql用于统计用户访问的uv,由于单表的数据量在10G以上,即使在user_access_xx_xx上加上nick的索引, 通过查看执行计划,也为全索引扫描...
完美解决distinct中使用多个字段的方法,完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法
oracle rownum和distinct
mysql中distinct用法【SQL中distinct的用法】.docx
使用Distinct查询.rar使用Distinct查询.rar
EFCore查询不重复数据Distinct,根据两个字段排序
distinct的使用.docx
主要介绍了MongoDB教程之聚合,MongoDB除了基本的查询功能之外,还提供了强大的聚合功能,这里主要介绍count、distinct和group,需要的朋友可以参考下
【DISTINCT】优化之MySQL官方文档翻译
Oracle分页存储过程,根据指定条件分页 并附上使用示例(C#代码) 注:不能查询视图或者包含distinct、group by、inner join的子句
如果我想知道颜值有哪些取值,所以希望从结果集中去掉重复的记录,加上distinct关键字,位置在select和字段列表之间。distinct是从结果集中筛选出唯
用Distinct在MySQL中查询多条不重复记录值,绝对的物有所值
sql中select语句中的distinct的用法
非常经典的网络抓包工具distinct(NetMon),可以设置任意过滤规则,界面易于操作。
总计COUNT_DISTINCT 此扩展提供了COUNT(DISTINCT ...)的替代方法,该方法对于大量数据通常会以排序和不良性能而告终。 职能 有两个多态聚合函数,用于处理按值传递的固定长度数据类型(即,在64位计算机上最多为...
SQL SELECT DISTINCT 语句 SELECT DISTINCT 语句用于返回唯一不同的值。 SQL SELECT DISTINCT 语句 在表中,一个列可能会包含多个重复值,有时您也许希望仅仅列出不同(distinct)的值。 DISTINCT 关键词用于返回...
用distinct关键字只能过滤查询字段中所有记录相同的(记录集相同),而如果要指定一个字段却没有效果,另外distinct关键字会排序,效率很低 。 select distinct name from t1 能消除重复记录,但只能取一个字段,...
首先对于MySQL的DISTINCT的关键字的一些用法: 1.在count 不重复的记录的时候能用到,比如SELECT COUNT( DISTINCT id ) FROM tablename;就是计算talbebname表中id不同的记录有多少条。 2,在需要返回记录不同...