一般在mybatis中遇到这个问题,首先按报错提示看实体类是否缺少getter(一般很少出现),接着考虑是不是SQL中字段与实体类中的字段拼写不同,其次考虑是不是传多个参数,且没有添加@param("")的参数
错误再现
Dao层接口
List<InvestigationalSiteEntity> getInvestigationalSiteListByDdcId(@Param("ddcId") Long ddcId,@Param("related") Integer related);
mybatis中的SQL
select t.*
from investigational_site t
left join ddc_investigational_site t1
on t.Investigational_site_id = t1.Investigational_site_id
where t1.ddc_id = #{ddcId}
<if test="related!= null">
and t.related =#{related,jdbcType=INTEGER}
</if>
问题出现的很突然,原以为只是拼接或者没有加注解,但是检查以后发现都没有问题,然后网上找资料看有一种答案是动态SQL中<if test=“xxx != null " > 改成<if test=”_parameter != null " >,然后用这种方式试,发现还是报错,这就很难受了,然后我把该接口和SQL中的报错的字段去除掉发现,发现还是报同样的错,最后我把每一个xml中用到该字段且带if标签的SQL都看了一遍(不包括对象中带有属性字段的SQL),然后都改成了“_parameter”,最终问题解决了。
正确的SQL
select t.*
from investigational_site t
left join ddc_investigational_site t1
on t.Investigational_site_id = t1.Investigational_site_id
where t1.ddc_id = #{ddcId}
<if test="_parameter != null">
and t.related =#{related,jdbcType=INTEGER}
</if>