前言
如果是相對於復雜的SQL邏輯我們肯定是基於存儲過程開發,這篇學習下執行存儲過程,調用存儲過程如果參數較多我們可以創建parameterMap。
搭建開發環境
開發環境和上篇文章保持相同
創建存儲過程
存儲過程執行一對多的關聯查詢
修改mapper.xml
<select id="testProc" parameterType="int" resultMap="authorResultMap"> exec usp_getAuthorBlogsById #{id} </select>
單元測試
@Test public void testProc(){ SqlSession sqlSession=null; try{ sqlSession=sqlSessionFactory.openSession(); Author author = sqlSession.selectOne("com.autohome.mapper.Author.testProc",1); System.out.println("作者信息 id:"+author.getId()+",name:"+author.getName()); System.out.println("作者博客:"); for(Blog blog:author.getBlogs()){ System.out.println("id:"+blog.getId()+",title:"+blog.getTitle()+",category:"+blog.getCategory()); } }catch(Exception e){ e.printStackTrace(); }finally { sqlSession.close(); } }
附單元測試截圖