**
Mybatis中多个参数在mapper.xml中的使用方式
**
很多人在使用mapper的时候,经常遇到多参数的问题,此时会有一部分人选择去将这些参数封装,但是小编有时候感觉这比较麻烦,今天小编来介绍三种mapper.xml中的对参数使用方式
第一种方案 使用参数中的下表
DAO层的函数方法
//拥有2个参数,从0开始的下表
public User selectUser(String name,String area);
对应的Mapper.xml
//不需要写parameterType
//#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,
<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{0} and user_area=#{1}
</select>
第二种方案 就是上面提到的封装参数
此方法采用Map传多参数.
Dao层的函数方法
Public User selectUser(Map paramMap);
对应的Mapper.xml
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
Service层调用
Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}
自我感觉这种方法比较麻烦
第三种方案
Dao层的函数方法
注意在接口的地方,一定要对每个参数使用@param
Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);
对应的Mapper.xml
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
以上就是mybatis的三种多参数传参方式