接收sql查出的内容
结果集可以封装成一个对象,然后将获取的内容封装在对象中,这个接收方法比较好理解。笔者一般是把数据表做相应的接收对象,其他笔者更喜欢用map来接收。对象接收,很多东西都比较好理解,然后框架也帮你做了相应的处理,不需要人为再去处理顺序或者其他特殊情况。而map接收,则有相关注意点。
1. 有序或者无序
`<select id="getXXX" resultType="java.util.HashMap">`
这样接收出来的结果是无排序的,具体跟map的存储机制有关。
要想有序,要使用:
<select id="getXXX" resultType="java.util.LinkedHashMap">
2. map一般使用方式:
<select id="getRankByTime" resultMap="ByCTime">
select d.collect_time as collect_time,d.new_score as new_rank
from (select a.*,
((select count(DISTINCT left(collector_id,12)) from lj_assess h
where left(collector_id,#{number})=left(#{regionid},#{number}) and
date_format(h.collect_stime,'%Y-%m-%d') = date_format(a.collect_time,'%Y-%m-%d'))- a.new_rank +1)
/(select count(DISTINCT left(collector_id,12)) from lj_assess h where left(collector_id,#{number})=left(#{regionid},#{number})
and date_format(h.collect_stime,'%Y-%m-%d') = date_format(a.collect_time,'%Y-%m-%d')) as new_score
from
(select r.*,
IF(@tmp=date_format(r.collect_time,'%Y-%m-%d'),@rank:=@rank + 1,@rank:=1) as new_rank,
@tmp:=date_format(r.collect_time,'%Y-%m-%d') as tmp,
r.household_num/(select count(household_id) from lj_household where left(household_id,12)=r.collector_id) as avg_local_rate
from( (select @tmp:=date_format(now(),'%Y-%m-%d'), @rank:=0) k,
(select left(collector_id,12) as collector_id,
date_format(collect_stime,'%Y-%m-%d') as collect_time,
sum(household_num) as household_num,
sum(kitchen_num) as kitchen_num,
sum(other_weight) as other_weight,
sum(kitchen_weight) as kitchen_weight,
sum(kitchen_weight)/(sum(kitchen_weight)+sum(other_weight) ) as decrease_rate,
sum(other_weight)/sum(household_num) as avg_other_weight,
sum(kitchen_weight)/sum(kitchen_num) as avg_kitchen_weight,
sum(kitchen_qualify)/sum(kitchen_num) as avg_kitchen_qualify,
avg(date_format(collect_stime,'%k')) as avg_stime,
avg(date_format(collect_etime,'%k')) as avg_etime,
avg(date_format(collect_ftime,'%k')) as avg_ftime,
sum(kitchen_qualify)/sum(kitchen_num) as collect_qualify,
if (sum(household_num)/sum(estimate_num)>1,1,sum(household_num)/sum(estimate_num)) as avg_collect_rate
from lj_assess
where left(collector_id,#{number})=left(#{regionid},#{number}) and (date_format(collect_stime,'%Y-%m-%d')
between date_format(#{sdate},'%Y-%m-%d') and date_format(#{edate},'%Y-%m-%d'))
group by left(collector_id,12),date_format(collect_stime,'%Y-%m-%d')
order by date_format(collect_stime,'%Y-%m-%d'),${sign} desc
) r)) a) d where d.collector_id = #{regionid}
</select>
<resultMap id="ByCTime" type="java.util.LinkedHashMap">
<result column="collect_time" property="collect_time" jdbcType="VARCHAR"/>
<result column="new_rank" property="new_rank" jdbcType="VARCHAR"/>
</resultMap>
resultMap的方式来使用。
3. 在order by后面加参数
一般来说,mybatis使用参数是用#{demo},但是在order by后面,使用这个格式是无效的,必须使用${demo}
这种格式才能识别和排序。这主要原因就是#会进行预编译,防注入性强,而**${demo}**则不会去进行预编译