<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);出现异常:
解决:
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值,引起报错。