码迷,mamicode.com
首页 > 移动开发 > 详细

mapper 传多个参数

时间:2016-11-17 10:26:27      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:plain   nbsp   方案   and   多态   index   开发   ase   class   

Mybatis的Mapper接口的参数,一般是一个对象,但如果不是对象,并且有多个参数的时候呢?我们第一个的想法是把参数封装成一个java.util.Map类型,然后在方法的注释上面写上map的key是什么,但是,这样的做法明显不够直观,不能够一眼看出这个方法的参数是什么,并且,影响到了java方法的多态性(方法名相同,参数数量或类型不同)。下面的方法一和方法二能够解决问题!

DAO层的函数方法 

 
1
Public User selectUser(String name,String area);

对应的Mapper.xml  

 
1
2
3
<select id="selectUser" resultMap="BaseResultMap">
    select  from user_user_t   where user_name = #{0} and user_area=#{1}
</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

此方法采用Map传多参数.

Dao层的函数方法

 
1
Public User selectUser(Map paramMap);

对应的Mapper.xml

 
1
2
3
<select id=" selectUser" resultMap="BaseResultMap">
   select  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

Service层调用

 
1
2
3
4
5
Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}

个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

Dao层的函数方法

 
1
Public User selectUser(@Param(“userName”)String name,@Param(“userArea”)String area);

对应的Mapper.xml

 
1
2
3
<select id=" selectUser" resultMap="BaseResultMap">
   select  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select

个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案

mapper 传多个参数

标签:plain   nbsp   方案   and   多态   index   开发   ase   class   

原文地址:http://www.cnblogs.com/lizhonghua34/p/6072459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!