问题描述
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'cwRecalculate' in 'class org.springblade.modules.cwxt.cwrecalculate.vo.CwRecalculateVO'
mapper文件的代码:
List<CwOperateRecordVO> selectCwOperateRecord(CwOperateRecordVO cwOperateRecord);
mapper.xml文件的代码:
<select id="selectCwOperateRecord" resultMap="cwOperateRecordResultMap">
select * from xmkf_cw_operate_record a
where a.is_deleted = 0
<include refid="selectCwOperateRecordWhere"/>
</select>
<sql id="selectCwOperateRecordWhere">
<if test="cwOperateRecord.startTime != null">
and a.create_time >= #{cwOperateRecord.startTime}
</if>
<if test="cwOperateRecord.endTime != null">
and a.create_time <= #{cwOperateRecord.endTime}
</if>
<if test="cwOperateRecord.operator != null and cwOperateRecord.operator != ''">
<bind name="name" value="'%'+ cwOperateRecord.operator +'%'"/>
and a.operator like #{name}
</if>
<if test="cwOperateRecord.operateFunction != null">
and a.operate_function = #{cwOperateRecord.operateFunction}
</if>
<if test="cwOperateRecord.type != null">
and a.type = #{cwOperateRecord.type}
</if>
</sql>
解决方案:
方案1:
mapper.xml文件不识别#{objectName.propertyName}语法,将objectName.去掉
<select id="selectCwOperateRecord" resultMap="cwOperateRecordResultMap">
select * from xmkf_cw_operate_record a
where a.is_deleted = 0
<include refid="selectCwOperateRecordWhere"/>
</select>
<sql id="selectCwOperateRecordWhere">
<if test="startTime != null">
and a.create_time >= #{startTime}
</if>
<if test="endTime != null">
and a.create_time <= #{endTime}
</if>
<if test="operator != null and operator != ''">
<bind name="name" value="'%'+ operator +'%'"/>
and a.operator like #{name}
</if>
<if test="operateFunction != null">
and a.operate_function = #{operateFunction}
</if>
<if test="type != null">
and a.type = #{type}
</if>
</sql>
在Mapper.xml文件中,使用对象来传递数据,可以将数据从Java代码传输到数据库,也可以将数据从数据库传输到Java代码。通常情况下,我们将对象中的属性名与数据库表的列名进行对应,以便在Mapper文件中使用这些属性来构建SQL语句。
在使用对象的属性名填充Mapper.xml文件数据时,不需要在属性名前面添加对象名称。这是因为对象是一个Java类,而不是一个数据库表,它的属性名称是在Java类中定义的,而不是在数据库表中定义的。
在Mapper文件中,我们使用#{propertyName}来引用DTO的属性。如果我们在属性名前面添加了对象名称,例如#{objectName.propertyName},则会导致语法错误,因为Mapper.xml文件不能识别这个语法。
注:个人认为,如果条件查询的条件太多,这么改太浪费时间,可以采取方案2
方案2:
直接在mapper文件的方法中,参数列表加上@Param
List<CwOperateRecordVO> selectCwOperateRecord(@Param("cwOperateRecord") CwOperateRecordVO cwOperateRecord);
在mapper接口上面添加@Param
用来给传入参数命名,那么参数就被转化为Mybatis
内置对象。