一、单个参数:

MyBatis传入多个参数 ,List集合
public List<XXBean> getXXBeanList(String xxCode);  

<select >

  select t.* from tableName t where t.id= #{id}  

</select>  

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
MyBatis传入多个参数 ,List集合

 

二、多参数:

MyBatis传入多个参数 ,List集合
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select >

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
MyBatis传入多个参数 ,List集合

 

三、Map封装多参数:  

MyBatis传入多个参数 ,List集合
public List<XXXBean> getXXXBeanList(HashMap map);  

<select >

  select 字段... from XXX where id=#{xxId} code = #{xxCode}  

</select>  

其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。 
MyBatis传入多个参数 ,List集合

 

 四、List封装in:

MyBatis传入多个参数 ,List集合
public List<XXXBean> getXXXBeanList(List<String> list);  

<select >
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4') 
MyBatis传入多个参数 ,List集合

 

五、多参数传递之注解方式示:    

 

MyBatis传入多个参数 ,List集合
例子:
 
public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);
 
xml配置这样写:
 
<select >
       SELECT * FROM addr__info 
    where addr_id=#{addrId} and corp_id=#{corpId} </select> 以前在<select>语句中要带parameterType的,现在可以不要这样写。
MyBatis传入多个参数 ,List集合

 

 

六、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:

将参数放入Map,再取出Map中的List遍历。如下:

MyBatis传入多个参数 ,List集合
List<String> list_3 = new ArrayList<String>();
Map<String, Object> map2 = new HashMap<String, Object>();
list.add("1");
list.add("2");
map2.put("list", list); //网址id

map2.put("siteTag", "0");//网址类型
MyBatis传入多个参数 ,List集合

 

public List<SysWeb> getSysInfo(Map<String, Object> map2) {
  return getSqlSession().selectList("sysweb.getSysInfo", map2);
}

 

MyBatis传入多个参数 ,List集合
<select >
  select t.sysSiteId, t.siteName, t1.mzNum as siteTagNum, t1.mzName as siteTag, t.url, t.iconPath
   from TD_WEB_SYSSITE t
   left join TD_MZ_MZDY t1 on t1.mzNum = t.siteTag and t1.mzType = 10
   WHERE t.siteTag = #{siteTag } 
   and t.sysSiteId not in 
   <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item}
   </foreach>
 </select>
MyBatis传入多个参数 ,List集合
 
 
 
 
 
 
 
 
 
 
 

1.由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

<update >
        UPDATE ams_user 
        SET login_pwd = #{0}, update_time =#{1,jdbcType=TIMESTAMP} 
        WHERE user_id = #{2,jdbcType=INTEGER}
</update>
  1. 通过注解的方式 
    调用方法:
Integer modifyPwd(@Param("userId")Integer userId,@Param("pwd") String pwd,@Param("upTime") Date updateTime);

xml中的写法:

<update >
        UPDATE ams_user 
        SET login_pwd = #{pwd}, update_time = #{upTime,jdbcType=TIMESTAMP} 
        WHERE user_id = #{userId,jdbcType=INTEGER}
</update>

3.通过Map的方式传递多个参数 
map中key的名字就是在#{}中使用的那个

Integer modifyPwd(HashMap map);  

<update id="modifyPwd" parameterType="hashmap">
UPDATE ams_user 
        SET login_pwd = #{pwd}, update_time = #{upTime,jdbcType=TIMESTAMP} 
        WHERE user_id =#{userId,jdbcType=INTEGER}  
</update>  
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shi1451042748/article/details/51786207

相关文章: