zoukankan      html  css  js  c++  java
  • 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());
    }
    

    大功告成...

  • 相关阅读:
    shell getopt getopts获取参数
    apache+svn+ladp认证
    SVN 迁移项目分支
    iptables 优先级
    很实用的一篇HTTP状态码
    套路还在——矩阵计算估值
    CU上看到的一个简单的算法帖子
    linux下服务端实现公网数据转发
    c++接口实现与分离(转载)
    c++继承概念
  • 原文地址:https://www.cnblogs.com/Draymonder/p/10514218.html
Copyright © 2011-2022 走看看