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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM