Mybatis passes multiple parameters (three solutions)

Mybatis transmits multiple parameters (three solutions)
2014-09-26 2 comments Source: Accumulated and learned, I want to contribute I want to contribute
According to the solutions I have come across, there are three ways to pass multiple parameters.

The function method of the first scheme

DAO layer

Public User selectUser(String name,String area);
the corresponding Mapper.xml 
<select id="selectUser" resultMap="BaseResultMap">
    select * from user_user_t where user_name = #{0} and user_area=#{1}
</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 scheme

This method uses Map to pass multiple parameters.

The function method of Dao layer

Public User selectUser(Map paramMap);
the corresponding 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 layer calls

Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put ("userName", "corresponding to specific parameter values");
paramMap.put("userArea", "corresponding to specific parameter values");
User user=xxx. selectUser(paramMap);}
Personally, I think this method is not intuitive enough, see The interface method cannot directly know what the parameters to be passed are.

The third scheme

Dao layer function method
Public User selectUser(@param("userName")Stringname,@param("userArea")String area);
corresponding 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>
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.

For example:
when entering and leaving the map, mapper.xml can directly use the key
//Internet rate in the last 30 days
map.put("gatewayId", objectNo);
DeviceGatewayHistory dgh = deviceGatewayHistoryService.findOnlineByMap(map);

mapper.xml configuration
<select id= "selectOnlineByMap" resultMap="BaseResultMap" parameterType="java.util.HashMap" useCache="true">
SELECT GATEWAY_ID, SUM(ONLINE_MINUTE) as ONLINE_MINUTE, SUM(UNDERLINE_MINUTE) as UNDERLINE_MINUTE
FROM device_gateway_history
WHERE STIME>=#{stime} AND <![CDATA[ETIME<=#{etime}]]> AND GATEWAY_ID=#{gatewayId} GROUP BY GATEWAY_ID
</select>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326999195&siteId=291194637