SpringBoot 调用 mysql存储过程的实战

本文详细介绍了如何使用SQL存储过程进行深度优先搜索,并在MyBatis框架下实现对存储过程的调用。通过具体的代码示例,展示了如何在Java层面对存储过程进行参数设置和结果处理,最后通过单元测试验证了功能的正确性。

网络上写的一堆都不能用的 好吧..

首先创建 存储过程

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());
}

大功告成...

转载于:https://www.cnblogs.com/Draymonder/p/10514218.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值