Mybatis发生There is no getter for property named 'xxx' in 'class com.xxx.xxx.xxx'异常处理方案

Mybatis版本:mybatis-3.4.0.jar

一 、发现问题异常如下:

严重: Servlet.service() for servlet [spring] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'dto' in 'class com.mes.dto.SearchOrderDto'] with root cause
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'dto' in 'class com.mes.dto.SearchOrderDto'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:409)
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:94)
	at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:108)
	(以下省略一些字)

二 、解决方法1:在.xml之中使用的时候把参数名改成"_parameter"

原代码:

<sql id="queryConditions">
		<where>
			<if test="dto.search_status != null">
				AND order_status = #{dto.search_status}
			</if>
			<if test="dto.keyword != null and dto.keyword != ''">
				AND ((order_id like #{dto.keyword}) or (order_clientname like #{dto.keyword}) or
				(order_productname like #{dto.keyword}) or (order_contractid like
				#{dto.keyword})or (order_materialname like #{dto.keyword})or (order_materialsource like
				#{dto.keyword}) or (order_imgid like #{dto.keyword}))
			</if>
			<if test="dto.fromTime != null and dto.toTime != null">
				AND order_committime BETWEEN #{dto.fromTime} AND #{dto.toTime}
			</if>
		</where>
	</sql>

修改之后的代码:

<sql id="queryConditions">
		<where>
			<if test="_parameter != null">
				AND order_status = #{_parameter}
			</if>
			<if test="dto.keyword != null and dto.keyword != ''">
				AND ((order_id like #{_parameter}) or (order_clientname like #{_parameter}) or
				(order_productname like #{_parameter}) or (order_contractid like
				#{_parameter})or (order_materialname like #{_parameter})or (order_materialsource like
				#{_parameter}) or (order_imgid like #{_parameter}))
			</if>
			<if test="_parameter != null and _parameter != null">
				AND order_committime BETWEEN #{_parameter} AND #{_parameter}
			</if>
		</where>
	</sql>

三 、解决方案二:在这个.xml的接口中提前定义(个人推荐使用)
原代码:

public interface OrderCustomerMapper {
	Long getOrderCount();

	int countBySearchDto(SearchOrderDto dto);

	List<MesOrder> getPageListBySearchDto(SearchOrderDto dto, PageQuery page);

}

修改之后的代码:

public interface MesOrderCustomerMapper {
	Long getOrderCount();

	int countBySearchDto(@Param("dto")SearchOrderDto dto);

	List<MesOrder> getPageListBySearchDto(@Param("dto")SearchOrderDto dto, @Param("page")PageQuery page);

}

四、异常原因分析

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


参考博客:
https://blog.csdn.net/woshixuye/article/details/8820387

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题看起来是在尝试访问Java的String类中的一个属性(名为"month"),但是Java的String类中并没有这个属性。在Java中,String类没有提供名为"month"的属性或方法。如果你想获取月份,你可能需要使用java.time包中的LocalDate类或者使用循环或其他方式手动提取出日期字符串中的月份信息。 如果你的目的是从某个特定格式的日期字符串中提取月份,那么你可以通过编程的方式手动解析出这个信息。下面是一个基本的例子: ```java public class DateParser { public static void main(String[] args) { String dateString = "your_date_string"; int month = extractMonth(dateString); System.out.println("Month: " + month); } public static int extractMonth(String dateString) { String[] parts = dateString.split("/"); // 假设日期格式为"day/month/year" return Integer.parseInt(parts); // 返回月份部分 } } ``` 在这个例子中,我们假设日期字符串的格式是"day/month/year",并且我们通过分割字符串的方式提取出月份部分。这种方法对于一些常见日期格式应该是有效的,但并不是所有的日期字符串都可以这样解析。对于更复杂的日期格式,你可能需要使用更复杂的解析逻辑。 如果你的需求是在特定的环境下,例如你在使用某种特定框架或者库,并且有获取月份的方法,那么请提供更多的信息,我将很高兴为你提供更具体的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值