springboot2.0 + myBatis 调用有返回值的SQL 存储过程

springboot2.0 + myBatis 调用SQL 存储过程

1、 写在前面
最近项目需要,公司大佬写了一个很流弊的存储过程,第一次调用记录一下!

2、代码

存储过程:

 --    in/out 类型, in是入参 ,out是返回的参数
create or replace procedure P_DEMO
 (                 
     V1 IN VARCHAR2,   V2  IN VARCHAR2,  V3   OUT VARCHAR2,   V4     OUT VARCHAR2
  )  is

mybatis:

<select id="addCubeSqlMap" parameterType="java.util.Map" statementType="CALLABLE" >
	{CALL  P_DEMO
	(#{V1,mode=IN,jdbcType=VARCHAR},
	 #{V2,mode=IN,jdbcType=VARCHAR},
	 #{V3,mode=OUT,jdbcType=VARCHAR},
	#{V4,mode=OUT,jdbcType=VARCHAR}
	)}
</select>

map只需要封装入参(就是mode=in)的就好了


   public int addCubeSqlMap(Map<String,String> parm);

	
  调用:
  Map<String,String > parm = new HashMap<String,String>();
  parm.put("V1","xxx");
  parm.put("V2","00");
    #todo 
   your_service.addCubeSqlMap(parm);
    //直接get你要传入xml的map get mode=out 的那个参数名就拿到了存储过程给你返回的值
   parm.get("V3");       
   parm.get(" V4");
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Spring Boot + MyBatis调用MySQL 8的存储过程,需要遵循以下步骤: 1. 在MySQL 8中创建存储过程。例如: ```sql CREATE PROCEDURE example_procedure(IN param1 VARCHAR(20), OUT result INT) BEGIN -- 存储过程主体 END; ``` 2. 在Spring Boot项目中引入MySQL 8的JDBC驱动和MyBatis框架的相关依赖。 3. 编写MyBatis的Mapper接口,用于调用存储过程。例如: ```java @Mapper public interface ExampleProcedureMapper { @Options(statementType = StatementType.CALLABLE) @Select("{CALL example_procedure(#{param1, mode=IN, jdbcType=VARCHAR}, #{result, mode=OUT, jdbcType=INTEGER})}") void exampleProcedure(@Param("param1") String param1, @Param("result") Integer result); } ``` 其中,@Mapper注解用于标识该接口为MyBatis的Mapper接口,@Options注解用于设置SQL语句的类型为存储过程调用,@Select注解用于定义调用存储过程名称和参数列表。 4. 在Spring Boot的配置文件中配置数据源和MyBatis的相关配置。例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/example_db?useSSL=false&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # MyBatis相关配置 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.entity ``` 其中,url、username、password和driver-class-name分别为MySQL 8的连接信息,mapper-locations用于指定Mapper接口和SQL语句的XML文件位置,type-aliases-package用于指定实体类的包名。 5. 在代码中调用Mapper接口的方法,即可执行存储过程并获取返回。例如: ```java @Service public class ExampleService { @Autowired private ExampleProcedureMapper exampleProcedureMapper; public Integer exampleProcedure(String param1) { Integer result = null; exampleProcedureMapper.exampleProcedure(param1, result); return result; } } ``` 其中,ExampleService为业务逻辑层的类,ExampleProcedureMapper为MyBatis的Mapper接口,exampleProcedure方法用于调用存储过程返回结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行人已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值