原理這里我就不多說了,我這里直接上代碼 :
原理可以看下博客 : 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)); } } }}
最后,看下我們的結果 :