mybatis-如何调用存储过程

本文介绍如何使用MyBatis框架调用数据库存储过程,包括存储过程的创建、参数配置及结果集处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近因为公司的需求,所以整理了一下mybatis调用存储过程的资料分享给大家。

1.创建存储过程 例如

存储过程中 in,out, in out 表示;

in 是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变。

out模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程

in out 表示高参数可以向该过程中传递值,也可以将某个值传出去

DROP PROCEDURE IF EXISTS `proc_adder`;

CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_adder`(IN a int, IN b int, OUT sum int)
BEGIN
    #Routine body goes here...
    DECLARE c int;
    if a is null then set a = 0; 
    end if;
  
    if b is null then set b = 0;
    end if;
    set sum  = a + b;
    select sum; #查询,返回sum结果集
END

2.存储过程写完之后就是与mybatis框架进行交接 

     给a 和 b 赋值 为 1 和 2 如果结果集 大于1就返回 0 给页面

/**
* @param request
* @param map
* @return
*/
@RequestMapping("getUserDeman")
@ResponseBody
public ModelMap getUserDeman(HttpServletRequest request, ModelMap map) {
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("a", "1");
maps.put("b", "2");	
//存储过程
DxwCustomerInterview interview=dxwMonService.storedProcedure(maps);
System.out.println(interview.getSum());}
if (interview != null && interview.getTyp() > 1) 
{
map.put("data","0");
}
else
{
map.put("data","1");
}
return map;
}

  3.mybatis的xml 操作

<resultMap id="CustomerInterview" type="com.web.bean.dxw.DxwCustomerInterview" >
        <result column="sum" property="sum"/>
 </resultMap>  
call proc_adder 执行这个存储过程 
mode=IN a和b  说明 a和b 是传的参数; 
mode=OUT sum 说明 sum 可以返回的值; 
jdbcType=INTEGER 是对应数据库类型 与上面创建存储过程的类型一致
例如 :
IN a int 说明  jdbcType=INTEGER;
IN a varchar(50) 说明 jdbcType=VARCHAR;
resultMap="CustomerInterview";返回值的内容在这里

<select id="storedProcedure"  resultMap="CustomerInterview" parameterType="java.util.Map" statementType="CALLABLE">
	   {call hlj.customer_interview(
	    #{a,jdbcType=INTEGER,mode=IN},
	    #{b,jdbcType=INTEGER,mode=IN},
	    #{sum,jdbcType=INTEGER,mode=OUT}
	    )}
</select>  
发现其实和基本的 mybatis 数据库调用是一样的,只是赋值的时候多了一些参数配置


使用Mybatis-Plus调用存储过程需要先定义一个Mapper接口,然后在接口中定义一个方法,方法名和存储过程名一致,使用@Select注解标注该方法,同时在@Select注解中指定存储过程的名称和参数类型。 例如,假设有一个存储过程名为"get_user_by_id",接收一个int类型的参数,返回一个User对象,那么可以定义一个Mapper接口如下: ``` public interface UserMapper extends BaseMapper<User> { @Select("call get_user_by_id(#{id, mode=IN, jdbcType=INTEGER}, #{result, mode=OUT, jdbcType=CURSOR, resultMap=userResultMap})") void getUserById(@Param("id") int id, @Param("result") ResultSet[] result); } ``` 在@Select注解中,使用"call"关键字指定调用存储过程,然后使用"#{参数名, mode=IN, jdbcType=参数类型}"指定输入参数,"#{参数名, mode=OUT, jdbcType=参数类型, resultMap=结果集映射}"指定输出参数,其中"resultMap"指定输出结果集的映射关系。 调用该方法时,可以使用Mybatis-Plus提供的SqlSessionTemplate执行调用: ``` @Autowired private SqlSessionTemplate sqlSessionTemplate; public User getUserById(int id) { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); ResultSet[] result = new ResultSet[1]; mapper.getUserById(id, result); User user = null; try { if (result[].next()) { user = new User(); user.setId(result[].getInt("id")); user.setName(result[].getString("name")); user.setAge(result[].getInt("age")); } } catch (SQLException e) { e.printStackTrace(); } return user; } ``` 在调用getUserById方法时,先获取UserMapper的实例,然后创建一个ResultSet数组作为输出参数,调用getUserById方法,最后从输出参数中获取结果集并转换为User对象返回。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值