• Mybatis Plus 中 参数传递的优化之路


    从项目开始使用 Mybatis Plus 到现在,对 Mapper 传递参数的方式做了多个版本的改进和优化。这篇文章主要讲解在改版和优化过程中遇到的问题,以及当时的一些想法。

    第一版:单个参数传递

    传递方式如下:

    UserMapper.java

    List<UserVO> getUserList(String name);
    

    UserMapper.xml

    <!--查询所有用户信息-->
    <select id="getUserList" resultMap="UserVOMap">
        select
        <include refid="col"/>
        from user
        where is_deleted = '0'
        <if test="name != null">
            and name like concat(concat('%', #{name}), '%')
        </if>
    </select>
    

    注:项目开始阶段,功能比较简单,需求也比较简单,所以没有使用太多查询条件。

    第二版:多个参数传递

    在此先说下错误的使用方式:

    List<UserVO> getUserList(String name, Integer age, String email);
    

    报错信息如下:

    "nested exception is org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg2, arg1, arg0, param3, param1, param2]"
    

    报错的原因是:这样的传参方式,Mybatis 是无法识别参数名的,必须进行参数绑定。具体原因可以自行上网查询。

    正确的传参方式需要把每一个参数与 Mapper.xml 中的参数进行绑定,如下:

    List<UserVO> getUserList(@Param("name") String name, @Param("age") Integer age, @Param("email") String email);
    

    UserMapper.xml

    <!--查询所有用户信息-->
    <select id="getUserList" resultMap="UserVOMap">
        select
        <include refid="col"/>
        from user
        where is_deleted = '0'
        <if test="name != null">
            and name like concat(concat('%', #{name}), '%')
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
        <if test="email != null">
            and email like concat(concat('%', #{email}), '%')
        </if>
    </select>
    

    第三版:优化多参数传递

    当参数特别多的时候,你会发现整个参数列表会写得很长。这时候就想,能不能通过一个对象去传参数

    UserMapper.java

    List<UserVO> getUserList(@Param("userDTO") UserDTO userDTO);
    

    UserMapper.xml

    <!--查询所有用户信息-->
    <select id="getUserList" resultMap="UserVOMap">
        select
        <include refid="col"/>
        from user
        where is_deleted = '0'
        <if test="userDTO.name != null">
            and name like concat(concat('%', #{userDTO.name}), '%')
        </if>
        <if test="userDTO.age != null">
            and age = #{userDTO.age}
        </if>
        <if test="userDTO.email != null">
            and email like concat(concat('%', #{userDTO.email}), '%')
        </if>
    </select>
    

    第四版:再次优化多参数传递

    虽然第三版已经够精简了,但是有个问题。所有的参数都必须放进一个对象中,这个对象势必非常臃肿。如果使用多个对象进行传递,又会出现之前的问题,参数列表中的参数过多。

    想到在修改第二版的时候,有个报错,报错信息如下:

    "nested exception is org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg2, arg1, arg0, param3, param1, param2]"
    

    通过网上查找得知,Mybatis 的参数映射方式是通过 Map。于是修改为以下的版本。

    UserServiceImpl.java

    public List<UserVO> getUserList(UserDTO userDTO) {
        Map<String, Object> sqlMap = new HashMap<>(2);
        sqlMap.put("name", userDTO.getName());
        sqlMap.put("age", userDTO.getAge());
        sqlMap.put("email", userDTO.getEmail());
        return userMapper.getUserList(sqlMap);
    }
    

    UserMapper.java

    List<UserVO> getUserList(Map<String, Object> sqlMap);
    

    UserMapper.xml

    <!--查询所有用户信息-->
    <select id="getUserList" resultMap="UserVOMap">
        select
        <include refid="col"/>
        from user
        where is_deleted = '0'
        <if test="name != null">
            and name like concat(concat('%', #{name}), '%')
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
        <if test="email != null">
            and email like concat(concat('%', #{email}), '%')
        </if>
    </select>
    

    改版大致经历了以上四个过程,希望能对大家有所帮助。


    如果文章有帮助到了你,欢迎点赞、转发。

    如果文章有错误的地方,欢迎留言交流。

    image

  • 相关阅读:
    3747 [POI2015]Kinoman
    1303 [CQOI2009]中位数图
    3769 [spoj 8549] BST again
    1015 [JSOI2008]星球大战starwar
    1193 [HNOI2006]马步距离
    合并Git仓库不相关历史版本解决方案
    vue-cli项目实现动态锚点定位
    jQuery加css3实现菜单栏组件(可无限添加子列表)
    JavaScript文件转成base64编码
    Ajax获取服务器响应头部信息
  • 原文地址:https://www.cnblogs.com/zhenggc/p/13655491.html
Copyright © 2020-2023  润新知