原文  http://www.cnblogs.com/mingyue1818/p/3714162.html

一、单个参数:

public List<XXBean> getXXBeanList(String xxCode);

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">

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

</select>

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

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。

二、多参数:

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);

<select id="getXXXBeanList" resultType="XXBean">

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

</select>

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始,或者是param0,param1这种形式。

三、Map封装多参数:

public List<XXXBean> getXXXBeanList(HashMap map);

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">

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

</select>

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

四、List封装in:

public List<XXXBean> getXXXBeanList(List<String> list);

<select id="getXXXBeanList" resultType="XXBean">

  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')

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

例子:

public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);

xml配置这样写:

<select id="getAddrInfo"  resultMap="com.xxx.xxx.AddrInfo">

SELECT * FROM addr__info

    where addr_id=#{addrId} and corp_id=#{corpId}</select>

以前在<select>语句中要带parameterType的,现在可以不要这样写。

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

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

List<String> list_3 = new ArrayList<String>();

Map<String, Object> map2 = new HashMap<String, Object>();

list.add("1");

list.add("2");

map.put("list", list); //网址id

map.put("siteTag", "0");//网址类型

public List<SysWeb> getSysInfo(Map<String, Object> map2) {

  return getSqlSession().selectList("sysweb.getSysInfo", map2);

}

<select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">

  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传入多个参数的问题 - mingyue1818的更多相关文章

  1. 小峰mybatis(2)mybatis传入多个参数等..

    一.mybatis传入多个参数: 前面讲传入多个参数都是使用map,hashmap:key value的形式:-- 项目中开发都建议使用map传参: 比如现在通过两个参数,name和age来查询: 通 ...

  2. (转)MyBatis传入多个参数的问题

    背景:记录mybaitis的使用方法,博闻强记,后面尽量记忆使用. MyBatis传入多个参数的问题 MyBatis传入多个参数的问题 详细记录mybatis在传递多个参数时候的使用方法 关于Myba ...

  3. mybatis传入多个参数

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  4. Mybatis 传入多个参数查询数据 (3种方法)

    第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...

  5. MyBatis传入多个参数的问题

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  6. (转载)MyBatis传入多个参数的问题

    原文地址:https://www.cnblogs.com/mingyue1818/p/3714162.html 一.单个参数: public List<XXBean> getXXBeanL ...

  7. mybatis 传入多个参数

    一.单个参数: public List<XXBean> getXXBeanList(@param("id")String id); <select id=&quo ...

  8. MyBatis传入多个参数的问题(转)

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  9. MyBatis传入多个参数 ,List集合

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

随机推荐

  1. 工作中常用的Linux命令:find命令

    本文链接:http://www.cnblogs.com/MartinChentf/p/6056571.html (转载请注明出处) 1.命令格式 find [-H] [-L] [-P] [-D deb ...

  2. iframe 的使用和登陆退出的实现——整个页面跳转

    iframe中如果只是页面跳转的话,我们依然只是部分的加载的了,为了实现整个页面的所有内容跳转,下面提供了整个页面跳转的方法. iframe例子 1.总的iframe页面(访问就访问这个)  all. ...

  3. 一:c语言(数据类型和运算符)

    #include <stdio.h> /*就是一条预处理命令,它的作用是通知C语言编译系统在对C程序进行正式编译之前需做一些预处理工作.*/ int main() /*C程序就是执行主函数 ...

  4. VS2010最常用快捷键

    1.选择类 F8 当前位置变成选定区域的头/尾(再移动光标或者点鼠标就会选定) Ctrl + F8 当前行变成选定区域的头/尾(再移动上下光标或者点鼠标就会选定多行) CTRL + W 选择当前单词 ...

  5. win10 右键菜单添加使用gvim打开方式

    ①打开注册表编辑器,开始-->运行-->regedit ②定位到:HKEY_CLASSSES_ROOT---> * --->Shell,在Shell 上右击,新建---> ...

  6. USACO八皇后

    VIS 0 1 2分别竖线和两个对角线,参见对角线(x,y)的和 差关系表 #include<iostream> #include<cstdio> #include<al ...

  7. J2EE中关于tomcat的maxIdle、maxActive、maxActive相关配置

    一.基本概念 1 maxActive 连接池的最大数据库连接数.设为0表示无限制,一般把maxActive设置成可能的并发量就行了 2 maxIdle 最大的空闲连接数 3 maxWait 最大建立连 ...

  8. windows下使用 linux命令好办法

    1. 安装下载 CygwinPortable一键安装包.7z 2. 把安装路径下/  [D:\cygwinportable\CygwinPortable\App\Cygwin\bin] 加到 Path ...

  9. hdu 2586 How far away

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. hashmap先按照value从大到小排序,value相等时按照key从小到大排序

    hashmap先按照value从大到小排序,value相等时按照key从小到大排序. [2]是从小到大排序,在[2]代码基础上交换o1,o2位置即可. 代码中用到[1]中提到的在value相等时再比较 ...