mybatis传入多个参数
来源:互联网 发布:德国二战军装淘宝 编辑:程序博客网 时间:2024/06/05 07:28
需要查阅本文的基本都是需要传入多个参数的,这里记住一句话:无论你传的参数是什么样的,最后mybtis都会将你传入的转换为map的,那么既然这样,当我们要传入多个参数时,何不直接给与map类型即可,然后mapper.xml通过#{map.key}来获取值即可,这个特别适合动态搜索,或者多个参数的查询,并且可以在mapper的xml语句中通过if判断来实现若为空,则不添加查询条件,
<if test="userId != null"> #{userId,jdbcType=VARCHAR},</if>
还可以通过for来进行遍历。
一、单个参数:
public List<XXBean> getXXBeanList(@param("id")String id);
<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select>
其中方法名和ID一致,#{}中的参数名与方法中的参数名一致, 这里采用的是@Param这个参数,实际上@Param这个最后会被Mabatis封装为map类型的。
select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
二、多参数:
方案1
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开始
方案2(推荐)基于注解
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
<select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{id} and name = #{code} </select>
由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个
三、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’)
五、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:
将参数放入Map,再取出Map中的List遍历。如下:
List<String> list = new ArrayList<String>();Map<String, Object> map = new HashMap<String, Object>();list.add("1");list.add("2");map.put("list", list); //网址idmap.put("siteTag", "0");//网址类型public List<SysWeb> getSysInfo(Map<String, Object> map) { return getSqlSession().selectList("sysweb.getSysInfo", map);}
<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>
转自:http://www.cnblogs.com/ningheshutong/p/5828854.html
- MyBatis传入多个参数
- Mybatis 传入多个参数
- MyBatis传入多个参数
- mybatis传入多个参数
- mybatis传入多个参数
- mybatis传入多个参数
- mybatis传入多个参数
- MyBatis传入多个参数
- mybatis传入多个参数
- mybatis传入多个参数
- mybatis使用map传入多个参数
- MyBatis传入多个参数的问题
- MyBatis传入多个参数的问题
- MyBatis传入多个参数的问题
- MyBatis传入多个参数的问题
- Mybatis传入多个参数处理方式
- MyBatis传入多个参数的问题
- MyBatis传入多个参数的问题
- JAVA八大基础数据?String为什么不能被继承?
- 一个好的行政MM能给公司带来巨大的影响
- 9.JMS规范
- ajax无法返回json数据报错406的解决方法
- jQuery事件绑定on()、bind()与delegate() 方法详解
- mybatis传入多个参数
- 关于 nginx 的一些优化(突破十万并发)
- javascript点击按钮,增加文本框和按钮
- options
- Ecliple+SSM+Maven 完美整合+图片详解
- 13 WEB开发笔记
- Ubuntu16.04完美安装搜狗输入法(解决输入法问题,系统栏显示两个输入法)
- c 语言输入,输出简单使用
- git用新分支替换掉master分支