Mybatis 参数传递的优化之路

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

从项目开始使用 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

相关文章
|
6月前
|
SQL Java 数据库连接
Mybatis实战练习六【批量删除&Mybatis参数传递】(下)
Mybatis实战练习六【批量删除&Mybatis参数传递】
|
6月前
|
XML Java 数据库连接
mybatis之配置优化and映射器~
mybatis之配置优化and映射器~
mybatis之配置优化and映射器~
|
6月前
|
SQL 存储 Java
Mybatis实战练习六【批量删除&Mybatis参数传递】(上)
Mybatis实战练习六【批量删除&Mybatis参数传递】
|
8月前
|
Java 数据库连接 mybatis
MyBatis参数传递
MyBatis参数传递
46 0
|
4月前
|
Java 数据库连接 数据库
MyBatis与Spring的集成整合加优化分页功能
MyBatis与Spring的集成整合加优化分页功能
|
8月前
|
SQL Java 数据库连接
掌握 MyBatis <choose>标签:优化动态查询条件的利器
当谈到在Java应用程序中进行数据库访问时,MyBatis 是一个备受欢迎的持久层框架。它的强大之处在于提供了灵活性和可定制性,使得数据库操作变得更加简便。在这篇文章中,我们将深入介绍 MyBatis 中的`&lt;choose&gt;` 标签,它是一个有趣且功能强大的元素,用于在 SQL 映射文件中进行条件选择。
123 0
|
8月前
|
SQL 安全 Java
详解Mybatis之参数传递问题
详解Mybatis之参数传递问题
|
9月前
|
SQL Java 数据库连接
【MyBatis】day02参数传递和映射(下)
【MyBatis】day02参数传递和映射(下)
58 0
|
9月前
|
SQL 存储 缓存
【MyBatis】day02参数传递和映射(上)
【MyBatis】day02参数传递和映射
65 0
|
9月前
|
SQL Java 数据库连接
mybatis多参数传递报错问题分析+硬核mybatis底层源码分析+@Param注解+图文实战环境分析【4500字详解打通,没有比这更详细的了!】(二)
mybatis多参数传递报错问题分析+硬核mybatis底层源码分析+@Param注解+图文实战环境分析【4500字详解打通,没有比这更详细的了!】
89 0