原理这里我就不多说了,我这里直接上代码 :
原理可以看下博客 : https://blog.csdn.net/u010046908/article/details/69944959 https://www.cnblogs.com/mark-chan/p/5384139.html
首先,在数据库定义mysql存储过程 :
drop PROCEDURE myfuncdictproc2; create PROCEDURE myfuncdictproc2(in p_dict_id int) begin select dict_id,value_code,value_name from func_dict_value where dict_id=p_dict_id; end;
然后执行下看看 :
然后,我们编写mybatis端的代码,首先在xml当中定义出来 :
注意到,mode=IN代表入参,还有个是 必须指定 statementType 为 CALLABLE 。
最后,我们写个测试类来测试下 :
public class TestMybatis { public static void main(String[] args) throws Exception { SqlSessionFactory sessionFactory = null; // System.out.println(ClassLoader.getSystemResource("configuration.xml")); String resource = "configuration.xml"; sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource)); //从类路径中加载资源 // sessionFactory.getConfiguration().getMapperRegistry().addMapper(UserMapper.class); // 已经在configuration.xml 文件当中写了,不需要重复注册 SqlSession sqlSession = sessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 测试存储过程 List<Map<String,Object>> mapList = userMapper.callProcTest(13); for(Map map : mapList ){ for(Object tempKey : map.keySet()){ System.out.println(tempKey+"-->"+map.get(tempKey)); } } }}
最后,看下我们的结果 :