天天看點

Mybatis傳遞多個參數

ibatis3如何傳遞多個參數有兩個方法:一種是使用java.Map,另一種是使用JavaBean。

通過Map傳遞多個參數 

parameterType 可以是别名或完全限定名,map或者java.util.Map,這兩個都是可以的

<select id="selectBlogByMap" parameterType="map" resultType="Blog">             

 select  t.ID, t.title, t.content               

 FROM blog t              

 where t.title = #{h_title}                

 and  t.content =#{h_content}         

</select>        

public void testSelectByMap() {             

     SqlSession session = sqlSessionFactory.openSession();             

     Map<String, Object> param=new HashMap<String, Object>();             

     param.put("h_title", "oracle");             

     param.put("h_content", "使用序列");             

     Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param);            

     session.close();             

     System.out.println("blog title:"+blog.getTitle());         

}        

通過JavaBean傳遞多個參數     

<select id="selectBlogByBean" parameterType="Blog" resultType="Blog">

 select t.ID, t.title, t.content

 from blog t

 wheret.title = #{title}

 and t.content =#{content}         

</select>

public void testSelectByBean() {             

     Blog blog=new Blog();             

     blog.setTitle("oracle");              

     blog.setContent("使用序列!");             

     Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog);            

     System.out.println("new Blog ID:"+newBlog.getId());         

}