一、結果集展示—摸清需求,搞清楚最終結果
很明顯,我們要的結果是一對多的結果,如何使用一條sql查出來呢
controller返回json 本文最後寫
二、設計實體類—用於封裝sql返回結果
三、書寫SQL
圖片展示說明
具體sql代碼分析
此處不採取常規的sql查詢方式,而是將返回結果在resultMap中,進行一對多拆分
collection 中用於存放我們想要的“一對多”中的“多”。也就是我們實體類中的List<> result
mapper.xml
<mapper namespace="picc.web.enterpriseinquiries.dao.DwPolicyInfoDao">
<resultMap id="dw_policy_info" type="picc.web.enterpriseinquiries.entity.DwPolicyInfo2" >
<result column="uw_end_date" property="uwEndDate" jdbcType="VARCHAR" />
<result column="total_premium" property="totalPremium" jdbcType="VARCHAR" />
<result column="rec_premium" property="recPremium" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="codeResultMap" type="picc.web.enterpriseinquiries.entity.Code" >
<result column="organizeCode" property="organizeCode" jdbcType="VARCHAR" />
<result column="comCode" property="comCode" jdbcType="VARCHAR" />
</resultMap>
<!--近5年簽單保費和應收保費分佈情況-->
<select id="GetTotalRecPremiumCase" resultMap="resultMap" parameterType="picc.web.enterpriseinquiries.entity.Code">
select DISTINCT (riskcode) as riskcode,organize_code as organizeCode,make_com as comCode,order_no
from dw_policy_info
where uw_end_date >= year(current) - 4
and organize_code = #{organizeCode}
<if test="comCode!= null and comCode!=''" >
and make_com = #{comCode}
</if>
group by uw_end_date,riskcode,organize_code,make_com
</select>
<resultMap id="resultMap" type="picc.web.enterpriseinquiries.entity.DwPolicy" extends="codeResultMap">
<result column="riskCode" property="riskCode" jdbcType="VARCHAR" />
<result column="organizeCode" property="organizeCode" jdbcType="VARCHAR" />
<result column="comCode" property="comCode" jdbcType="VARCHAR" />
<collection property="value" ofType="picc.web.enterpriseinquiries.entity.DwPolicyInfo2" column="{organizeCode=organizeCode,riskCode=riskCode, comCode=comCode}" select="getDwPolicy"></collection> <!--傳多個參數-->
</resultMap>
<select id="getDwPolicy" resultType="picc.web.enterpriseinquiries.entity.DwPolicyInfo2" parameterType="java.util.Map">
select uw_end_date as uwEndDate,
nvl(round(sum(total_premium) *0.0001,2),0) totalPremium ,
nvl(round(sum(rec_premium)*0.0001,2),0) recPremium
from dw_policy_info
where uw_end_date >= year(current) - 4
and organize_code = #{organizeCode}
and riskcode=#{riskCode}
<if test="comCode!= null and comCode!=''" >
and make_com = #{comCode}
</if>
group by uw_end_date
</select>
</mapper>
四、Controller
返回json類型的結果給頁面,此處用了JSONObject.toJSONString()
引入jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
@RequestMapping("/GetTotalRecPremiumCase")
@ResponseBody
public String GetTotalRecPremiumCase(HttpServletRequest request){
HeadResponse hr = new HeadResponse();
hr.setMessage("查詢失敗!");
hr.setStatus("500");
List<DwPolicyInfo2> list = new ArrayList<>();
Code code = new Code();
try {
code = PassParamtersUtil.GetCode(request,dwPolicyInfoService);
String organizeCode = code.getOrganizeCode();
String comCode = code.getComCode();
Code code2 = new Code();
code2.setOrganizeCode(organizeCode);
code2.setComCode(comCode);
list = dwPolicyInfoService.GetTotalRecPremiumCase(code2);
hr.setMessage("查詢成功!");
hr.setStatus("200");
hr.setResult(list);
return JSONObject.toJSONString(hr);
}catch (Exception e){
System.out.println("e:"+e);
return JSONObject.toJSONString(hr);
}
}
返回結果展示
這裏的 1 2 3 4等數字 對應sql那張圖裏的查詢結果