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>
复制代码
相关文章
相关标签/搜索