您的位置:首页技术文章
文章详情页

Mybatis传递多个参数的三种实现方法

浏览:5日期:2023-10-24 10:09:36

方案一

Dao层的函数方法

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

对应的Mapper.xml

<select resultMap='BaseResultMap'> select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} </select>

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

方案二(Map传值)

Dao层的函数方法

1 Public User selectUser(Map paramMap);

对应的Mapper.xml 

<select parameterType='map' 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层的函数方法

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

对应的Mapper.xml

<select parameterType='map' resultMap='BaseResultMap'> select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} </select>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Mybatis 数据库
相关文章: