MyBatis參數報錯--Parameter 'realName' not found.

今天寫一個條件查詢的時候遇到了問題,也是好久沒寫了,最後終於解決了java

  • 首先在dao層接口的參數上加上@Param註解,查詢單個參數能夠不加,多個必須加上
  • @Param註解的做用是給參數命名,參數命名後就能根據名字獲得參數值,正確的將參數傳入sql語句中

正確寫法sql

List<GmVipMember> selectByRealNameMemIdTelNum(@Param("realName") String realName,@Param("memId") String memId,@Param("telNum") String telNum)throws Exception;
複製代碼

錯誤寫法bash

List<GmVipMember> selectByRealNameMemIdTelNum(String realName,String memId,String telNum)throws Exception;
複製代碼

還有動態sql語句的寫法ui

  • 1.注意:第一個if標籤裏面不用and鏈接,由於是第一個參數因此不用鏈接
<select id="selectByRealNameMemIdTelNum" resultType="com.gmos.vip.system.model.GmVipMember"
            parameterType="java.lang.String">
        select
        *
        from gm_vip_member
        where
        <if test="realName!=null and realName!=''">
            real_name = #{realName,jdbcType=VARCHAR}
        </if>
        <if test="memId!=null and memId!=''">
            and mem_id = #{memId,jdbcType=VARCHAR}
        </if>
        <if test="telNum!=null and telNum!=''">
            and tel_num = #{telNum,jdbcType=VARCHAR}
        </if>
    </select>
複製代碼
  • 2.使用where標籤能夠解決and的問題
<select id="selectByRealNameMemIdTelNum" resultType="com.gmos.vip.system.model.GmVipMember"
            parameterType="java.lang.String">
        select
        *
        from gm_vip_member
        <where>
            <if test="realName!=null and realName!=''">
                and real_name = #{realName,jdbcType=VARCHAR}
            </if>
            <if test="memId!=null and memId!=''">
                and mem_id = #{memId,jdbcType=VARCHAR}
            </if>
            <if test="telNum!=null and telNum!=''">
                and tel_num = #{telNum,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
複製代碼
相關文章
相關標籤/搜索