mybatis mybatis-plus传入多个参数处理
在使用mybatis、mybatis-plus开发的过程中,我们经常会遇到传入多个参数的场景,这时如果不加处理的话就可能报错,那么,怎么规避这些问题呢,下面分享下我在项目中的处理:
方案一、
dao层注解处理,也是最常用的一种处理方法,例如:
public boolean updatebean(@param("id")string id, @param("code")string code);
xml mapper 正常接收使用就好(由于多参数传入,所以不需要设置parametertype)
<update id="updatebean"> update tablename set code = #[code] where id = #{id} </update >
方案二、
map封装多参数,将要传递的参数封装进map中,然后正常接收使用,例如
public boolean updatebean(hashmap map);
<update id="updatebean" parametertype="java.util.map"> update tablename set code = #[code] where id = #{id} </update >
其他的方案都是使用比较少或者限制比较多的,就不一一赘述了,这两种方案应该可以满足我们日常开发的大部分需求了
mybatis传多个参数常见解决
最近随着项目的深入,逐渐的对数据库的应用越来越深入,已经开始根据自身的业务写mapper方法了,然后看到这篇帖子,好的东西要吸收进来,原文如下:
据我目前接触到的传多个参数的方案有三种。
第一种方案
dao层的函数方法
public user selectuser(string name,string area);
对应的mapper.xml
<select id="selectuser" resultmap="baseresultmap"> select * from user_user_t where user_name = #{0} and user_area=#{1} </select>
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
第二种方案
此方法采用map传多参数
dao层的函数方法
public user selectuser(map parammap);
应的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层调用
private user xxxselectuser(){ map parammap=new hashmap(); parammap.put(“username”,”对应具体的参数值”); parammap.put(“userarea”,”对应具体的参数值”); user user=xxx. selectuser(parammap);}
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
第三种方案
dao层的函数方法
public user selectuser(@param(“username”)string name,@param(“userarea”)string area);
对应的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>
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
ps:今天在网上看到第三种方案其实还是传个map进去,只不过不用像方案二一样显式的指定,而是通过param注解就可以了。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论