需要实现一个功能,查询结果需要指定特殊数据排到第一位,其他的数据根据排序字段排列。
    使用case when 的方式,指定排列顺序。在from前添加。集成mybatis,使用。
<if test="(params.applicantDeptId !=null and params.applicantDeptId !='') or (params.applicantId !=null and params.applicantId !='')">
			,(
				case when a.applicantDeptId = #{params.applicantDeptId} then 1
					 when a.applicantId = #{params.applicantId} then 2
					 else 3 end
			)as flag
</if>后来查询时,后台一直报索引错误,以及空指针错误。一开始查询资料,还以为是mybatis不能使用同一个参数进行多次引用,导致?标志与参数个数不一致。程序中存在这种一个参数多次引用的,不会导致报错。
根据错误信息查看。原来是这里使用了分页拦截器,在调用统计数量部分代码时报的错误。
分页插件中,查询数量部分的sql是取 select count(1) from where 条件部分 。 where条件部分的sql如下 存在applicantDeptId 和 applicationId 两个参数
<if test="applicantDeptId !=null and applicantDeptId !=''">
			<!-- 还需要查询出在流程中的数据 -->
				and (
					a.applicantDeptId=#{applicantDeptId}
					or
					<!-- 当前用户科室在下一个流程所在的科室中 当前用户的岗位在下一个流程所在的岗位中 -->
					(
					${dept_id} in (select * from Fun_SplitStr2(d.next_deptid,',','|'))
					and
					${job_id} in  (select * from Fun_SplitStr2(d.next_jobid,',','|'))
					)
					<!-- 参与过审核的人 -->
					or
					${user_id} in  (select * from Fun_SplitStr2(d.approve_userid,',','|'))
				)
		</if>
		<if test="applicantId !=null and applicantId !=''"> 
			<!-- 还需要查询出在流程中的数据 -->
				and (
					a.applicantId=#{applicantId}
					or
					<!-- 当前用户科室在下一个流程所在的科室中 当前用户的岗位在下一个流程所在的岗位中 -->
					(
					${dept_id} in (select * from Fun_SplitStr2(d.next_deptid,',','|'))
					and
					${job_id} in  (select * from Fun_SplitStr2(d.next_jobid,',','|'))
					)
					<!-- 参与过审核的人 -->
					or
					${user_id} in  (select * from Fun_SplitStr2(d.approve_userid,',','|'))
				)
</if>而在from前也有这两个参数。在分页插件代码中,pstmt的参数列表中,一共四个参数。统计部分的sql中实际只对应了两个? 。所以就报了索引错误问题。
解决方案:
将#{}改为${}就可以了。 使用${} 是不会加入参数列表的
<if test="(params.applicantDeptId !=null and params.applicantDeptId !='') or (params.applicantId !=null and params.applicantId !='')">
			,(
				case when a.applicantDeptId = '${params.applicantDeptId}' then 1
					 when a.applicantId = '${params.applicantId}' then 2
					 else 3 end
			)as flag
		</if>

 
     
     
     
    
发表评论