存储过程还不会写的同学可以参考我另一篇文章:https://www.cnblogs.com/liuboyuan/p/9375882.html
网上已经有很多用mybatis调用的教程了,但是大部分是xml方式调用,最近项目中用mybatis plus,没有xml配置文件。本次分享下用@Select注解方式调用。
创建存储过程(语法oracle)
create or replace procedure test(name in varchar,gender out varchar) as begin if name = 'xiaoming' then gender := 'man'; else gender := 'woman'; end if; end;
测试一下
declare name varchar(16); gender varchar(16); begin name:='xiaoming'; test(name,gender); dbms_output.put_line(gender); --控制台打印man end;
如果mybatis使用的连接数据库用户权限比较低,还需要授权才能执行
grant execute on 库名.存储过程名称 to 用户; --别忘了给用户授权
准备测试pojo
import lombok.*; @Data @NoArgsConstructor @ToString public class People { private String name; private String gender; }
mapper的写法
import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.StatementType; @Mapper public interface TestMapper { @Select("{call 库名.test(#{name,jdbcType=VARCHAR,mode=IN},#{gender,jdbcType=VARCHAR,mode=OUT})}") @Options(statementType = StatementType.CALLABLE) void getGengder(People people); }
测试类
@Resource private TestMapper testMapper; @Test public void test() { People people = new People(); people.setName("xiaoming"); testMapper.getGengder(people); System.out.println(people); //(name=xiaoming,gender=man) }