<select id="getPersons" resultType="cn.bjut.entity.Person">
    SELECT * FROM persons
    <where>
        <if test="age != null">
            age = #{age}
        </if>
    </where>
</select>
对应的Mapper接口:

List<Person> getPersons(Integer age);

调用:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonMapper personMapper = ctx.getBean(PersonMapper.class);

List<Person> list = personMapper.getPersons(24);
list.forEach(System.out::println);
出现异常:

MyBatis:There is no getter for property named 'XXX' in 'class java.lang.XXXX'

解决:

1、使用@Param注解

List<Person> getPersons(@Param("age") Integer age);
2、使用_parameter参数

<select id="getPersons" resultType="cn.bjut.entity.Person">
    SELECT * FROM persons
    <where>
        <if test="_parameter != null">
            age = #{age}
        </if>
    </where>
</select>

原因:

Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取Integer.age值,引起报错。


相关文章:

  • 2022-12-23
  • 2022-02-04
  • 2021-10-03
  • 2021-08-25
  • 2021-11-03
  • 2021-11-21
  • 2021-10-10
猜你喜欢
  • 2021-04-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
相关资源
相似解决方案