Mybatis传递多个参数

ibatis3如何传递多个参数有两个方法:一种是使用Map,另一种是使用JavaBean。

  

?
<!-- 
 
   使用HashMap传递多个参数  
 
  parameterType 可以是别名或完全限定名 ,map->java.util.Map,这两个都是可以的 
 
  --> 
 
  <selectid= "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> 
 
  <!-- 使用JavaBean传递多个参数 --> 
 
  <selectid= "selectBlogByBean" parameterType= "Blog" resultType= "Blog"
 
      SELECT t.ID, t.title, t.content 
 
        FROM blog t 
 
       WHERE t.title = #{title} 
 
         AND t.content =#{content} 
 
  </select>
?
/**
 
    * 通过Map传递多个参数
 
    */ 
 
   @Test 
 
   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传递多个参数
 
    */ 
 
   @Test 
 
   public void testSelectByBean() { 
 
       SqlSession session = sqlSessionFactory.openSession(); 
 
       Blog blog= new Blog(); 
 
       blog.setTitle( "oracle" ); 
 
       blog.setContent( "使用序列!" ); 
 
       Blog newBlog = (Blog)session.selectOne( "cn.enjoylife.BlogMapper.selectBlogByBean" ,blog); 
 
       session.close(); 
 
       System.out.println( "new Blog ID:" +newBlog.getId()); 
 
   }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值