Oracle 遞迴sql,mybatis的遞迴查詢,與儲存過程呼叫
阿新 • • 發佈:2019-09-30
Oralce 遞迴sql
一、查詢所有子節點
SELECT * FROM district START WITH NAME ='平昌縣' CONNECT BY PRIOR parent_id=ID
二、查詢所有父節點
SELECT * FROM district START WITH NAME ='平昌縣' CONNECT BY PRIOR parent_id=ID
這個語法很好理解,就是遞迴語法,從什麼節點依次去找。。
引用文獻:https://www.cnblogs.com/Soprano/p/10659127.html
Mybatis 遞迴查詢
<resultMap id="getSelf" type="net.chunxiao.vo.GoodsCategoryVo"> <id column="goodscateid" property="goodscateid"></id> <result column="name" property="name"></result> <collection property="catelist" select="getCategory" column="goodscateid"></collection> <!--查到的cid作為下次的pid--> </resultMap> <select id="getCategory" resultMap="getSelf"> select * from goods_category where status=0 AND parentid=#{pid} ORDER BY displayorder,goodscateid
mybatis 介面:
List<GoodsCategoryVo> getCategory(Integer pid);
實體類:
public class GoodsCategoryVo { private Integer goodscateid; private String name; private Integer parentid; private String description; private Integer displayorder; private Double commissionrate; private Integer enabled; private List<GoodsCategoryVo> catelist; …… }
參考文獻:https://blog.csdn.net/janet796/article/details/79500349
mybatsi 呼叫儲存過程:
mybatis 介面:
void addDep(@Param("dep") Department department);
xml中寫法:
<select id="addDep" statementType="CALLABLE"> call addDep(#{dep.name,mode=IN,jdbcType=VARCHAR},#{dep.parentId,mode=IN,jdbcType=INTEGER},#{dep.enabled,mode=IN,jdbcType=BOOLEAN},#{dep.result,mode=OUT,jdbcType=INTEGER},#{dep.id,mode=OUT,jdbcType=BIGINT}) </select>
解釋:
注意statementType呼叫表示這是一個儲存過程,mode=IN表示這是輸入引數,mode=OUT表示這是輸出引數,呼叫成功之後,在service中獲取department的id和result欄位,就能拿到相應的呼叫結果了。