ibatis 传递多个参数

 

ibatis 传递多个参数

分类: iBATIS 645人阅读 评论(0) 收藏 举报

不知何时,ibatis3改为mybatis3了,听说mybatis3不用再需要自己手动实现DAO的实现类了,Service层可以直接使用DAO接口中的方法。

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

sqlXml配置:

  1. <!--  
  2.      使用HashMap传递多个参数   
  3.     parameterType 可以是别名或完全限定名 ,map->java.util.Map,这两个都是可以的  
  4.     -->  
  5.     <select id="selectBlogByMap" parameterType="map" resultType="Blog">  
  6.         SELECT t.ID, t.title, t.content  
  7.           FROM blog t  
  8.          WHERE t.title = #{h_title}  
  9.            AND t.content =#{h_content}  
  10.     </select>  
  11.     <!-- 使用JavaBean传递多个参数 -->  
  12.     <select id="selectBlogByBean" parameterType="Blog" resultType="Blog">  
  13.         SELECT t.ID, t.title, t.content  
  14.           FROM blog t  
  15.          WHERE t.title = #{title}  
  16.            AND t.content =#{content}  
  17.     </select>  
测试代码:
  1. /** 
  2.      * 通过Map传递多个参数 
  3.      */  
  4.     @Test  
  5.     public void testSelectByMap() {  
  6.         SqlSession session = sqlSessionFactory.openSession();  
  7.         Map<String, Object> param=new HashMap<String, Object>();  
  8.         param.put("h_title""oracle");  
  9.         param.put("h_content""使用序列!");  
  10.         Blog blog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByMap",param);  
  11.         session.close();  
  12.         System.out.println("blog title:"+blog.getTitle());  
  13.     }  
  14.     /** 
  15.      * 通过JavaBean传递多个参数 
  16.      */  
  17.     @Test  
  18.     public void testSelectByBean() {  
  19.         SqlSession session = sqlSessionFactory.openSession();  
  20.         Blog blog=new Blog();  
  21.         blog.setTitle("oracle");  
  22.         blog.setContent("使用序列!");  
  23.         Blog newBlog = (Blog)session.selectOne("cn.enjoylife.BlogMapper.selectBlogByBean",blog);  
  24.         session.close();  
  25.         System.out.println("new Blog ID:"+newBlog.getId());  
  26.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值