我用這個mybatis自帶的resultMap是因為它對查詢樹狀結構,組織架構有良好的支持而不需要去做遞歸或其它繁瑣操作,
直接可以一步到位
進入正題
<resultMap id="Organ" type="com.pskj.GSLZ.pojo.zfzb.Organ" > <result column="OR_ID" property="OR_ID" jdbcType="VARCHAR"></result> <result column="OR_NAME" property="OR_NAME" jdbcType="VARCHAR"></result> <result column="PARENT_ID" property="PARENT_ID" jdbcType="VARCHAR"></result> <association column="OR_ID" property="count" javaType="Integer" select="count"/> <collection column="OR_ID" property="subOrgan" ofType="Organ" javaType="java.util.ArrayList" select="selectChildren"></collection> </resultMap> <!--查詢一級組織架構--> <select id="checkList" parameterType="pd" resultMap="Organ" useCache="false"> SELECT * FROM sys_organ t WHERE t.OR_ID = #{OR_ID} AND t.OR_STATE = '2' </select> <!--查詢下級組織架構--> <select id="selectChildren" parameterType="pd" resultMap="Organ" useCache="false"> SELECT * FROM sys_organ t WHERE t.PARENT_ID = #{OR_ID} AND t.OR_STATE = '2' </select> <!--統計各支隊故障數量--> <select id="count" parameterType="pd" resultType="Integer" useCache="false"> SELECT IFNULL(sum(num),0) count from zfzb_subcheck zsk LEFT JOIN zfzb_equip_check zec on zec.EC_ID=zsk.EC_ID <where> zec.STATE='1' <!--查詢下級組織所有id,這是寫的一個MySQL查詢函數--> and ORGAN_ID in(SELECT OR_ID from sys_organ where OR_STATE='2' and FIND_IN_SET(OR_ID,findOrgan(#{OR_ID}))) </where> </select>
resultMap怎么用應該不用我去具體介紹吧,百度一下你就知道,主要看<association/>,<collection/>中的select對應的查詢,
還用我的parameterType="pd"中pd用的map封裝的,可以把它認為一個map
當然我沒展示全部,主要就是查詢組織架構的樹狀結構及統計
<!--統計各支隊故障數量--> <select id="count" parameterType="pd" resultType="Integer" useCache="false"> SELECT IFNULL(sum(num),0) count from zfzb_subcheck zsk LEFT JOIN zfzb_equip_check zec on zec.EC_ID=zsk.EC_ID <where> zec.STATE='1' <!--查詢下級組織所有id,這是寫的一個MySQL查詢函數--> and ORGAN_ID in(SELECT OR_ID from sys_organ where OR_STATE='2' and FIND_IN_SET(OR_ID,findOrgan(#{OR_ID}))) <if test="OR_ID!=null and OR_ID!=''"><!-- 1--> </if> </where> </select>
用組織id做if判斷
出現了報錯
<resultMap id="Organ" type="com.pskj.GSLZ.pojo.zfzb.Organ" > <result column="OR_ID" property="OR_ID" jdbcType="VARCHAR"></result> <result column="OR_NAME" property="OR_NAME" jdbcType="VARCHAR"></result> <result column="PARENT_ID" property="PARENT_ID" jdbcType="VARCHAR"></result> <association column="{OR_ID=OR_ID}" property="count" javaType="Integer" select="count"/> <collection column="OR_ID" property="subOrgan" ofType="Organ" javaType="java.util.ArrayList" select="selectChildren"></collection> </resultMap>
這里主要將association的column改成了多參數方式傳遞就可以正常查詢了,隨后我試驗了OR_NAME等字段都可以正常查詢,但有時候我們需要時間查詢,關鍵字查詢怎么辦,
將startDate和endDate兩個時間查詢的參數放進去試試
<resultMap id="Organ" type="com.pskj.GSLZ.pojo.zfzb.Organ" > <result column="OR_ID" property="OR_ID" jdbcType="VARCHAR"></result> <result column="OR_NAME" property="OR_NAME" jdbcType="VARCHAR"></result> <result column="PARENT_ID" property="PARENT_ID" jdbcType="VARCHAR"></result> <!-- 這里我想實現將時間查詢的參數能夠傳遞至統計查詢里,並且每次遍歷時參數的值不會改變--> <association column="{OR_ID=OR_ID,startDate=startDate,endDate=endDate}" property="count" javaType="Integer" select="count"/> <collection column="OR_ID" property="subOrgan" ofType="Organ" javaType="java.util.ArrayList" select="selectChildren"></collection> </resultMap>
直接報錯,startDate找不到,這樣一想也確實,startDate和endDate本來就不是組織實體類里的字段,換一種思路
<resultMap id="Organ" type="com.pskj.GSLZ.pojo.zfzb.Organ" > <result column="OR_ID" property="OR_ID" jdbcType="VARCHAR"></result> <result column="OR_NAME" property="OR_NAME" jdbcType="VARCHAR"></result> <result column="PARENT_ID" property="PARENT_ID" jdbcType="VARCHAR"></result> <!-- 這里我想實現將時間查詢的參數能夠傳遞至統計查詢里,並且每次遍歷時參數的值不會改變--> <association column="{OR_ID=OR_ID,startDate=startDate,endDate=endDate}" property="count" javaType="Integer" select="count"/> <collection column="OR_ID" property="subOrgan" ofType="Organ" javaType="java.util.ArrayList" select="selectChildren"></collection> </resultMap> <!--查詢一級組織架構 startDate和endDate為構造的虛擬字段--> <select id="checkList" parameterType="pd" resultMap="Organ" useCache="false"> SELECT OR_ID,OR_NAME,PARENT_ID, IFNULL(#{startDate},'') startDate, IFNULL(#{endDate},'') endDate FROM sys_organ t WHERE t.OR_ID = #{OR_ID} AND t.OR_STATE = '2' </select> <!--查詢下級組織架構 這里的startDate和endDate是接受自一級組織傳來的值並一級一級往下傳遞--> <select id="selectChildren" parameterType="pd" resultMap="Organ" useCache="false"> SELECT OR_ID,OR_NAME,PARENT_ID, IFNULL(#{startDate},'') startDate, IFNULL(#{endDate},'') endDate FROM sys_organ t WHERE t.PARENT_ID = #{OR_ID} AND t.OR_STATE = '2' </select> <!--統計各支隊故障數量--> <select id="count" parameterType="pd" resultType="Integer" useCache="false"> SELECT IFNULL(sum(num),0) count from zfzb_subcheck zsk LEFT JOIN zfzb_equip_check zec on zec.EC_ID=zsk.EC_ID <where> zec.STATE='1' <!--查詢下級組織所有id,這是寫的一個MySQL查詢函數--> and ORGAN_ID in(SELECT OR_ID from sys_organ where OR_STATE='2' and FIND_IN_SET(OR_ID,findOrgan(#{OR_ID}))) <if test="startDate!=null and startDate!=''"><!--開始時間 --> and zec.CHECK_TIME >= #{startDate} </if> <if test="endDate!=null and endDate!=''"><!--結束時間--> and zec.CHECK_TIME <= #{endDate} </if> </where> </select>
這樣就可以解決resultMap中的嵌套查詢多參數無法傳遞的問題了,當然還有許多不足的地方,如果有好的建議和方法歡迎指出