欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

MybatisMybatis-Plus传入多个参数的处理方式,

来源: javaer 分享于  点击 35822 次 点评:174

MybatisMybatis-Plus传入多个参数的处理方式,


目录
  • Mybatis Mybatis-Plus传入多个参数处理
    • 方案一、
    • 方案二、
  • Mybatis传多个参数常见解决
    • 第一种方案
    • 第二种方案
    • 第三种方案
  • 总结

    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注解就可以了。

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

    您可能感兴趣的文章:
    • mybatis-plus 查询传入参数Map,返回List<Map>方式
    • mybatis-plus分页传入参数后sql where条件没有limit分页信息操作
    • mybatis-plus通用枚举@JsonValue接收参数报错No enum constant
    • mybatis-plus 如何判断参数是否为空并作为查询条件
    • springboot下mybatis-plus如何打印sql日志和参数到日志文件
    相关栏目:

    用户点评