在MyBatis中使用存储过程可以通过Mapper接口的方式来实现。下面是一个使用存储过程的示例:
CREATE PROCEDURE GetUserInfo(IN userId INT, OUT userName VARCHAR(255))
BEGIN
SELECT user_name INTO userName
FROM user
WHERE user_id = userId;
END
public interface UserMapper {
@Select("{call GetUserInfo(#{userId, jdbcType=INTEGER, mode=IN}, #{userName, jdbcType=VARCHAR, mode=OUT})}")
void getUserInfo(@Param("userId") int userId, @Param("userName") String userName);
}
<mapper namespace="com.example.UserMapper">
<select id="getUserInfo" statementType="CALLABLE">
{call GetUserInfo(#{userId, mode=IN, jdbcType=INTEGER}, #{userName, mode=OUT, jdbcType=VARCHAR})}
</select>
</mapper>
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
String userName = null;
userMapper.getUserInfo(1, userName);
System.out.println("User name: " + userName);
通过以上步骤,就可以在MyBatis中使用存储过程来获取数据。