关于 hibernate 查询中实现order by的 NULLS LAST 和 NULLS FIRST
1:创建一个CustomNullsFirstInterceptor类
public class CustomNullsFirstInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = -3156853534261313031L;
private static final String ORDER_BY_TOKEN = "order by";
public String onPrepareStatement(String sql) {
int orderByStart = sql.toLowerCase().indexOf(ORDER_BY_TOKEN);
if (orderByStart == -1) {
return super.onPrepareStatement(sql);
}
orderByStart += ORDER_BY_TOKEN.length() + 1;
int orderByEnd = sql.indexOf(")", orderByStart);
if (orderByEnd == -1) {
orderByEnd = sql.indexOf(" UNION ", orderByStart);
if (orderByEnd == -1) {
orderByEnd = sql.length();
}
}
String orderByContent = sql.substring(orderByStart, orderByEnd);
String[] orderByNames = orderByContent.split("\\,");
for (int i=0; i<orderByNames.length; i++) {
if (orderByNames[i].trim().length() > 0) {
if (orderByNames[i].trim().toLowerCase().endsWith("desc")) {
orderByNames[i] += " NULLS LAST";
} else {
orderByNames[i] += " NULLS FIRST";
}
}
}
orderByContent = StringUtils.join(orderByNames, ",");
sql = sql.substring(0, orderByStart) + orderByContent + sql.substring(orderByEnd);
return super.onPrepareStatement(sql);
}
}
2:加入配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.skywin.law.workflow.domain</value>
<!--<value>com.skywin.common.fileupload.domain</value>-->
<value>com.skywin.common.domain</value>
<value>com.skywin.chat.domain</value>
<value>com.skywin.law.assistflow.domain</value>
</list>
</property>
<property name="mappingResources" ref="sessionFactoryMappingResources"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- <prop key="hibernate.cache.provider_configuration_file_resource_path">diss-override-ehcache.xml</prop> -->
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
<prop key="hibernate.hbm2ddl.import_files">import-install.sql</prop>
</props>
</property>
<property name="entityCacheStrategies">
<props>
<prop key="org.jbpm.pvm.internal.task.TaskImpl">read-write</prop>
</props>
</property>
<property name="entityInterceptor">
<bean id ="customNullsFirstInterceptor" class="com.skywin.common.hibernate3.CustomNullsFirstInterceptor" />
</property>
</bean>
分享到:
相关推荐
适用情况: oracle 对查询结果进行排序时,被排序的栏位存在null值,且要指定NULL值排在最前面或者最后面 关键字:Nulls First;Nulls Last 默认情况:null 默认为最大值(即:asc 升序<小–>大>,null值排在最后...
将字符串排序为 SQL 这是一个 rust crate,用于将“排序表达式”转换为可在“ORDER BY”中使用的 SQL 表达式。 例如, -date,id将转换为date DESC NULLS LAST, id ASC NULLS LAST 。 它可用于将 HTTP 查询参数转换...
nulls_for_goals
ArcEngine二次开发代码,调试可运行,可实现属性查询、空间查询和统计功能!供初学者练习使用.........
Special version of lists, where end of list is not a NULL pointer, but a nulls marker, which can have many different values.
抽象精品ppt模板nulls_for_goals166
SQL Order By SQL insert SQL update SQL delete SQL 高级 SQL Top SQL Like SQL 通配符 SQL In SQL Between SQL Aliases SQL Join SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL ...
iterate over a hlist continuing current point.
主要介绍了Java函数式编程(九):Comparator,本文是系列文章的第9篇,其它文章请参阅本文底部的相关文章,需要的朋友可以参考下
A clustered index is like a telephone directory in which all of the rows for customers with the same last name are clustered together in the same part of the book. Just as the organization of a ...
在oracle中,有rank,dense_rank,row_number,以及分组排名partition。 说明: rank:排名会出现并列第n名,它...row_number() over(partition by bb.channel_name order by sum(aa.dk_serv_num) desc nulls last) p1_ra
select * from dy_info t order by t.degree nulls first 1.8.4.2 说明 控制null显示行位置 1.8.5 Nvl 1.8.5.1 语法 select nvl(t.sap,'空') from dy_info t; 1.8.5.2 说明 如果sap号为空,则返回‘空’,否则返回...
SET ANSI_NULLS ON GO ALTER PROCEDURE test @From varchar(100)="zhang.zhanling@itgemini.net" , @To varchar(100)="zhang.zhanling@itgemini.net" ,@eng varchar(1000)=" ", @Bcc varchar(500)="xu.ming;...
jquery_aja_plsql_xml_hibernate、
Consistent Query Answering Based on Repairing Inconsistent Attributes with Nulls
NULL 博文链接:https://caohong286.iteye.com/blog/1275437
替换_white_space_with_nulls_in_excel 用NULL替换空白和空字符串的代码
主要介绍了关于mybatis callSettersOnNulls 配置,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
11.2. 利用JDBC核心类实现JDBC的基本操作和错误处理 11.2.1. JdbcTemplate类 11.2.2. NamedParameterJdbcTemplate类 11.2.3. SimpleJdbcTemplate类 11.2.4. DataSource接口 11.2.5. SQLExceptionTranslator接口 ...
The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs ...