Mybatis使用问题:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.Integer'

Mybatis问题:在使用条件语句动态设置SQL语句时出现如下错误

Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.Integer'
at books.com.BooksApplicationTests.getBookTest(BooksApplicationTests.java:56)

解决方法:

在映射接口中,方法的中的参数添加@Param(“parameter name”)即可。

例子:

 映射接口:

public interface BookMapper {
Book getBookById(int id);
Book getBook(@Param("id") int id);
}
映射配置文件:
<select id="getBook" parameterType="int" resultType="books.com.boot.model.Book">
SELECT * FROM book
<where>
<if test="id>0">id=#{id}</if>
</where>
</select>

若映射配置文件这样写则不需要添加@Param注解
<select id="getBookById" parameterType="int" resultType="books.com.boot.model.Book">
SELECT * FROM book WHERE id=#{id}
</select>

猜你喜欢

转载自www.cnblogs.com/ysp99/p/9288056.html
今日推荐