Hibernate Criteria查询

来源:互联网 发布:谁说菜鸟不会数据分析 编辑:程序博客网 时间:2024/05/03 11:26
 

查询指定字段
Criteria criteria = session.createCriteria(Person.class);ProjectionList plist = Projections.projectionList();plist.add(Projections.property("entity_column1"));plist.add(Projections.property("entity_column2"));plist.add(Projections.property("entity_column3"));criteria.setProjection(plist);List<Object[]> result = criteria.list(); // 这样的结果集结构为List<Object[]>// Object[0] = entity_column1 value// Object[1] = entity_column1 value// Object[2] = entity_column1 value


分组
单列分组:

criteria.setProjection(Projections.groupProperty("entity_column"));

多列分组:

ProjectionList plist = Projections.projectionList();plist.add(Projections.groupProperty("entity_column1"));plist.add(Projections.groupProperty("entity_column2"));plist.add(Projections.groupProperty("entity_column3"));criteria.setProjection(plist);


计算总行数:

Integer totalLines = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
Integer totalLines = (Integer)criteria.setProjection(Projections.count("entity_column")).uniqueResult();
 
//1,首先需要在程序中可以拿到要count的几个列。可以在某个类中设置静态属性。//2,创建类MyEmptyInterceptor,覆盖onPrepareStatement方法:MyEmptyInterceptor extends EmptyInterceptor{onPrepareStatement(String sql){//对sql进行处理。//处理结果为:select count(value(char(alias.table_column1),'') || value(char(alias.table_column2),'') || ...) from ...//这里的函数为DB2函数,数据库不同使用不同的函数处理,value函数是指如果参数一为null,则使用''来标识,在DB2中'||'两边均不能为null和非字符串类型}}

//3,在sessionFactory的bean配置中,配置property<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">...<property name="entityInterceptor"><bean id="myEmptyInterceptor" class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean></property>...</bean>

 
//3,在sessionFactory的bean配置中,配置property<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">...<property name="entityInterceptor"><bean id="myEmptyInterceptor" class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean></property>...</bean>

原理:AnnotationSessionFactoryBean的超类LocalSessionFactoryBean有entityInterceptor属性,在hibernate将sql语句发送到数据库执行前
     调用了entityInterceptor的onPrepareStatement方法。且参数为已经生成好的sql语句。所以可以在sessionFactory中将entityInterceptor
     给覆盖掉,注入自己的interceptor。那么调用的是子类的方法,MyEmptyInterceptor的onPrepareStatement方法。原理:AnnotationSessionFactoryBean的超类LocalSessionFactoryBean有entityInterceptor属性,在hibernate将sql语句发送到数据库执行前
     调用了entityInterceptor的onPrepareStatement方法。且参数为已经生成好的sql语句。所以可以在sessionFactory中将entityInterceptor
     给覆盖掉,注入自己的interceptor。那么调用的是子类的方法,MyEmptyInterceptor的onPrepareStatement方法。
 
 
 
 

//3,在sessionFactory的bean配置中,配置property<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">...<property name="entityInterceptor"><bean id="myEmptyInterceptor" class="com.silone.dao.interceptor.MyEmptyInterceptor"></bean></property>...</bean>


 

Integer totalLines = criteria.setProjection(Projections.countDistinct("entity_column1")).uniqueResult();
这样只是对entity_column1去除了重复并统计行数。如果要统计多列不重复的数据。那么就要countDistinct多列。
那么借助count多列的做法,最终处理的结果为
select count(distinct value(char(alias.table_column1),'') || value(char(alias.table_column2),'') || ...) from ...
在第一列的前方使用distinct关键字。不同数据库不同做法。

去除重复数据:
1,使用group by,要去除重复的数据都用group by 。
2,使用distinct关键字。
 ProjectionList plist = Projections.projectionList();
 plist.add(Projections.property("entity_column1"));
 plist.add(Projections.property("entity_column2"));
 plist.add(Projections.property("entity_column3"));
 Projection p = Projections.distinct(plist);
 criteria.setProjection(p).list();

 

怎么排序?
criteria.addOrder(Order.desc("entity_column"));
如果数据库中有null,'',一般数据。那么排序如何排?
参数count多列的方案,使用Interceptor。
处理结果为
select value(table_column1 tc1,''),table_column2 tc2,... from ... order by tc1
select table_column1 tc1,value(table_column2,'') tc2,... from ... order by tc2
order by 哪个字段,那么这个字段就要嵌套value函数。不同数据库。不同函数

                                             
0 0
原创粉丝点击