SpringBoot 調用 mysql存儲過程的實戰


網絡上寫的一堆都不能用的 好吧..

首先創建 存儲過程

DROP PROCEDURE IF EXISTS dfsSons;

CREATE PROCEDURE dfsSons(IN rootid INT)
BEGIN
	DECLARE dep INT;
	DROP table if exists tmplist;
	create table tmplist(
			id int,
			depth int
	);
	SET dep = 0;
	insert into	tmplist select file_relation_id, dep from file_relation 
		where file_relation_id = rootid;
	WHILE ROW_COUNT() > 0 DO
		SET dep = dep+1;
		insert into tmplist 
			select A.file_relation_id, dep from file_relation as A, tmplist as B
			where A.parent_id = B.id and B.depth = dep - 1;
	END WHILE;
END$$

CALL dfsSons(7);

然后在Dao層編寫具體的方法

需要maven中引入jpa,

本例中實際上不需要返回值,但是mybatis會有返回值,使用void會報錯,這里用HashMap兼容了。

使用的注解和查詢一樣,但是要指定statementType為CALLABLE。

@SuppressWarnings("rawtypes")
@Select("call dfsSons(#{rootid})")
@Options(statementType= StatementType.CALLABLE )
public HashMap getTableOfDelete(@Param("rootid") int parent_id);

單元測試

@Test
public void testDelete() {
    int parent_id = 7;
    fileRelationDao.getTableOfDelete(parent_id);
    List<Integer> lists = fileRelationDao.selectNeedDeleteId();
    System.out.println(lists.size());
}

大功告成...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM