【Mybatis异常】There is no getter for property named ‘factoryName‘ in ‘class java.lang.String‘“的解决方案

目录

1、传入类型

2、解决方案

3、出现原因

4、注意事项


1、传入类型

当入参为 string类型时 (包括java.lang.String.),也包括入参为 Integer类型时

我们使用#{xxx}引入参数,会抛异常There is no getter for property named 'XXX' in 'class java.lang.String'

<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
			parameterType="java.lang.String">
	select
	    <include refid="Base_Column_List"/>
	from op_outdev_factory
	<where>
	    <if test="factoryName != null">
		and name like concat('%',#{factoryName},'%')
	    </if>
	</where>
</select>

2、解决方案

方案一:把#{xxx}修改为 #{_parameter} 即可

<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
			parameterType="java.lang.String">
	select
	    <include refid="Base_Column_List"/>
	from op_outdev_factory
	<where>
	    <if test="_parameter != null">
		and name like concat('%',#{_parameter},'%')
	    </if>
	</where>
</select>

方案二:可以在方法中提前定义

/**
 * 查询厂家列表
 * @param factoryName
 * @return
 */
List<OutdevFactory> getFactoryList(@Param("factoryName") String factoryName);
<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
			parameterType="java.lang.String">
	select
	    <include refid="Base_Column_List"/>
	from op_outdev_factory
	<where>
	    <if test="factoryName!= null">
		and name like concat('%',#{factoryName},'%')
	    </if>
	</where>
</select>

3、出现原因

Mybatis默认采用OGNL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错

4、注意事项

其他mybatis的版本不知道有没有这个问题,暂时不知道会不会出现这个问题。

完结!

原文地址:https://www.cnblogs.com/no8g/p/13415536.html