mybatis报错 There is no getter for property named X

mybatis报错 There is no getter for property named 'query' in 'class com.qsnxods.controller.dto.StoreDeviceDTO'
MAPPER.XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//0TD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qsnxods.mapper.StoreDeviceMapper">
    <select id="findPage"  resultType="com.qsnxods.entity.StoreDevice">
        select * from (select TMP_PAGE.*,ROWNUMBER() OVER() AS ROW_ID from
        (select a.*,b.name as typename,c.name as brandname from tb_storedevice a
        left join tb_type b on a.type = b.id
        left join tb_brand c on a.brand = c.id
        <where>
            <if test="query.type != null and query.type != -1">
                and a.type = #{query.type}
            </if>
            <if test="query.brand != null and query.brand != -1">
                and a.brand = #{query.brand}
            </if>
            <if test="query.startDate != null and query.endDate != null " >
                and a.date between #{query.startDate} and #{query.endDate}
            </if>
        </where>
        order by a.remain desc,a.type asc
        ) as TMP_PAGE ) TMP_PAGE
        where row_id between #{startNum} and #{endNum}
    </select>

    <select id="count"  resultType="Long">
        select count(*) from tb_storedevice a
        left join tb_type b on a.type = b.id
        left join tb_brand c on a.brand = c.id
        <where>
            <if test="query.type != null and query.type != -1">
                and a.type = #{query.type}
            </if>
            <if test="query.brand != null and query.brand != -1">
                and a.brand = #{query.brand}
            </if>
            <if test="query.startDate != null and query.endDate != null " >
                and a.date between #{query.startDate} and #{query.endDate}
            </if>
        </where>

    </select>
</mapper>


Mapper文件:

 List<StoreDevice> findPage(Integer startNum, Integer endNum, StoreDeviceDTO query);

    Long count(StoreDeviceDTO query);

在执行过程中,先后调用了findPage 和 count 两个方法

 @PostMapping("/page")
    public Result findPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestBody StoreDeviceDTO query) {
        //mybatis方式 多表查询
        Integer startNum = (pageNum - 1) * pageSize + 1;
        Integer endNum = pageNum * pageSize;

        List<StoreDevice> list = storeDeviceMapper.findPage(startNum, endNum, query);
        Long count = storeDeviceMapper.count(query);

        Page<StoreDevice> p = new Page<>();
        p.setRecords(list);
        p.setTotal(count);
        p.setCurrent(pageNum);
        p.setSize(pageSize);
        return Result.success(p);
    }

在执行过程中,findPage成功执行,但是执行到count方法时报错

img

这个报错是由于在StoreDeviceDTO类中没有名为query的属性所引起的。MyBatis在映射结果集时需要根据属性名来匹配数据库查询结果,因此需要在DTO类中定义对应的属性和对应的getter方法。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    问题出在在StoreDeviceDTO类中缺少名为query的属性的getter方法。这个错误是因为MyBatis在执行查询时需要通过getter方法获取query属性的值,但是在StoreDeviceDTO类中找不到对应的getter方法。

    解决办法是在StoreDeviceDTO类中添加一个名为getQuery()的方法来获取query属性的值。

    示例代码:

    public class StoreDeviceDTO {
        private String query;
    
        // 添加getQuery()方法
        public String getQuery() {
            return query;
        }
    
        // 其他属性的getter和setter方法
        // ...
    }
    

    请注意,getQuery()方法的命名需要遵循Java的命名规范,根据属性名query生成以get开头的方法名。

    添加完getQuery()方法后,重新编译并运行代码,报错应该会消失。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^