Mybatis 同時傳入多個參數和對象


流程

1,mapper 接口文件使用 @param 注解(一個參數就不用使用注解,多個參數要么使用注解,要么使用數組的方式取值)

2,mapper xml 文件使用

mapper 接口文件傳參

public interface AccountMapper {
    List<Account> selectBySearch(@Param("record") Account record, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("companyName") String companyName);
}

mapper xml 文件使用參數

傳入的參數不止一個,就不要指定參數類型

<select id="selectBySearch" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from main_account
    where 1 = 1
    <!-- 使用參數 1 (是個對象) -->
    <if test="record.email != null">
        and email = #{record.email}
    </if>
    <!-- 使用參數 2 -->
    <if test="startDate != null">
        and create_date &gt; str_to_date(concat('', #{startDate}),'%Y-%m-%d %H %i %s')
    </if>
    <!-- 使用參數 3 -->
    <if test="endDate != null">
        and create_date &lt; str_to_date(concat('', #{endDate}),'%Y-%m-%d %H %i %s')
    </if>
    <!-- 使用參數 4 -->
    <if test="companyName != null">
        and (full_name like "%"#{companyName}"%" or short_name like "%"#{companyName}"%")
    </if>
</select>

 


免責聲明!

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



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