mybatis参数传递

1.传递一个参数

传递一个参数一般情况下直接用就可以,不需要map,不需要实体

[html]  view plain  copy
  1. public List<PageData> findUserDetail(String id);  
[html]  view plain  copy
  1. <select id="selectUser"  parameterType="java.lang.String" resultMap="pd">  
  2.     select  *  from  t_user   where  user_id=#{id}  
  3. </select>  

2.传递两个或多个参数

(1)通过序号传递,不使用parameterType

[html]  view plain  copy
  1. public List<PageData> findUserDetail(String username,String sex);  
[html]  view plain  copy
  1. <select id="selectUser" resultMap="pd">  
  2.     select  *  from  t_user   where  user_name=#{0} and sex=#{1}  
  3. </select>  

(2)map传参

[html]  view plain  copy
  1. Map paramMap=new hashMap();  
  2. paramMap.put("username","对应具体的参数值");  
  3. paramMap.put("sex","对应具体的参数值");  
  4. List<User> userList=xxx.getUserList(paramMap);  
[html]  view plain  copy
  1. public List<User> getUserList(HashMap map);    
[html]  view plain  copy
  1. <select id="getUserList" parameterType="hashmap" resultType="xxx.xxx.user">  
  2.   
  3.   select * from t_user where username=#{username} and sex= #{sex}    
  4.   
  5. </select>  

(3)实体传参

[html]  view plain  copy
  1. //给用户收藏表赋值  
  2.     User user=new User();  
  3.     user.setUserName("www");  
  4.     user.setSex("女");  
[html]  view plain  copy
  1. List<User> userList=xxx.getUserList(user);  
[html]  view plain  copy
  1. <select id="getUserList" parameterType="xxx.xxx.User" resultType="xxx.xxx.user">  
  2.   
  3.   select * from t_user where username=#{username} and sex= #{sex}    
  4.   
  5. </select>   

(4)@param通过单个参数名传递

[html]  view plain  copy
  1. Public User selectUser(@param("username")String name,@param(“sex”)String sex);  
[html]  view plain  copy
  1. <select id="getUser" resultType="xxx.xxx.user">  
  2.   
  3.   select * from t_user where username=#{username} and sex= #{sex}    
  4.   
  5. </select>   

3.参数是一个list集合,使用in

[html]  view plain  copy
  1. public List<XXXBean> getXXXBeanList(List<String> idlist);    
  2.   
  3. <select id="getUserList" resultType="xxx.xxx.user">  
  4.   select * from t_user where user_id in  
  5.   <foreach item="id" index="index" collection="list" open="(" separator="," close=")">    
  6.     #{id}    
  7.   </foreach>    
  8. </select>  

4.参数既包括单个参数也包括list集合

参数放到map中,然后xml中遍历list

[html]  view plain  copy
  1. Map<String, Object> map = new HashMap<String, Object>();  
  2. map.put("idlist", idlist);   
  3. map.put("sex", "女");  
[html]  view plain  copy
  1. <select id="getUserList" resultType="xxx.xxx.user">  
  2.   select * from t_user where sex=#{sex} and user_id in  
  3.   <foreach item="id" index="index" collection="list" open="(" separator="," close=")">    
  4.     #{id}    
  5.   </foreach>    
  6. </select>  

http://blog.csdn.net/ww130929/article/details/60480449
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值