Mybatis solution for passing multiple parameters (three)

first option

Function method of DAO layer

[java]  view plain copy  
 
  1. Public User selectUser(String name,String area);  

Corresponding Mapper.xml

[java]  view plain copy  
 
  1. <select id="selectUser" resultMap="BaseResultMap">  
  2. select * from user_user_t where user_name = #{0} and user_area=#{1}  
  3. </select>  

Among them, #{0} represents the first parameter in the dao layer, #{1} represents the second parameter in the dao layer, and more parameters can be added later.

The second option

This method uses Map to pass multiple parameters.

Function method of Dao layer

[java]  view plain copy  
 
  1. Public User selectUser(Map paramMap);  

Corresponding Mapper.xml

[java]  view plain copy  
 
  1. <select id=" selectUser" resultMap="BaseResultMap">  
  2. select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}  
  3. </select>  

Service layer call

[java]  view plain copy  
 
  1. Private User xxxSelectUser(){  
  2. Map paramMap=new hashMap();  
  3. paramMap.put("userName", "corresponding to the specific parameter value");  
  4. paramMap.put("userArea", "corresponding to the specific parameter value");  
  5. User user=xxx. selectUser(paramMap);}  

Personally, I think this method is not intuitive enough, seeing the interface method cannot directly know what the parameters to be passed are.

The third option

Function method of Dao layer

[java]  view plain copy  
 
  1. Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);  

Corresponding Mapper.xml

[java]  view plain copy  
 
  1. <select id=" selectUser" resultMap="BaseResultMap">  
  2. select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}  
  3. </select>   

Personally, I think this method is better. It allows developers to know what parameters to pass when they see the dao layer method. It is more intuitive, and I personally recommend this method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325794230&siteId=291194637