JavaWeb踩坑记录

  1. org.apache.ibatis.binding.BindingException: Parameter 'XXXX' not found.There is no getter for property named ‘XXX‘ in ‘class XXX

    • 原因分析(首先这个问题在 Dao 层)
      1. 检查 SQL 语句,#{}中的内容是否存在拼写错误,或者与参数中@param注解中不一致的情况
      2. @param的使用错误,使用方法参考:MyBatis参数传递
  2. Mapper method 'priv.dandelion.dao.BrandMapper.selectTotalCountByCondition attempted to return null from a method with a primitive return type (int).

    • 原因分析:

      1. 是未配置 导致数据库与需要使用的名称不一致,需要配置@ResultMap使其名称一致就可以了

      2. 第二种情况就是误配置,如selectTotalCount 无返回值类型 就不要使用 @ResultMap,使用resultType,二者都不用会报错

        <select id="selectTotalCountByCondition" resultType="java.lang.Integer">
            select count(*) from tb_brand
            <where>
                <if test="brand.brandName != null and brand.brandName != ''">
                    brandName like #{brand.brandName}
                </if>
                <if test="brand.companyName != null and brand.companyName != ''">
                    and companyName like #{brand.companyName}
                </if>
                <if test="brand.status != null">
                    and status = #{brand.status}
                </if>
            </where>
        </select>
        
  3. Unknown column 'companyName' in 'where clause'

    • 原因分析:并没有按照resultMap进行解析,where 标签中的内容必须要和数据库中一致

      • 错误代码

        <!-- 条件查询 -->
        <select id="selectByPageAndCondition" resultMap="brandResultMap">
            select * from tb_brand
            <where>
                <if test="brand.brandName != null and brand.brandName != ''">
                    brandName like #{brand.brandName}
                </if>
                <if test="brand.companyName != null and brand.companyName != ''">
                    and companyName like #{brand.companyName}
                </if>
                <if test="brand.status != null">
                    and status = #{brand.status}
                </if>
            </where>
            limit #{begin}, #{pageSize}
        </select>
        
      • 更整后代码

        <!-- 条件查询 -->
        <select id="selectByPageAndCondition" resultMap="brandResultMap">
            select * from tb_brand
            <where>
                <if test="brand.brandName != null and brand.brandName != ''">
                    brand_name like #{brand.brandName}
                </if>
                <if test="brand.companyName != null and brand.companyName != ''">
                    and company_name like #{brand.companyName}
                </if>
                <if test="brand.status != null">
                    and status = #{brand.status}
                </if>
            </where>
            limit #{begin}, #{pageSize}
        </select>
        
posted @ 2022-11-19 12:32  Dandelion_000  阅读(47)  评论(0编辑  收藏  举报