首先在数据库中写一个过程函数,然后测试能否正常运行。
DROP PROCEDURE IF EXISTS pro_getdes ;
CREATE PROCEDURE pro_getdes (in height int ,out describtion varchar(50))
BEGIN
if height >=180 then
set describtion='身材高挑';
ELSEIF height>=170 and height<180 then
set describtion='身高适中';
ELSE
set describtion='你矮!!!';
END if;
END
call pro_getdes(195,@ms);
select @ms;
此时的运行结果为
现在在spring中使用一下过程函数(注意:这个过程函数是要在连接的数据库中已经运行过的)
mapper层接口:
public interface GuochengMapper {
Map getPro(Map map);
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.mapper.GuochengMapper">
<select id="getPro" resultType="map" statementType="CALLABLE" parameterType="map">
{call pro_getdes(#{height,jdbcType=INTEGER,mode=IN},#{describtion,jdbcType=VARCHAR,mode=OUT})}
</select>
</mapper>
在测试类中运行:
@SpringBootTest
class SpringbootTestApplicationTests {
@Resource
private GuochengMapper guochengMapper;
//测试过程函数
@Test
void contextLoads() {
System.out.println("ddd");
Map m = new HashMap();
m.put("height", 180);
m.put("describtion", null);
guochengMapper.getPro(m);
System.out.println(m);
}
}
测试结果