UserDao.java代码如下:
package soc.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import soc.entity.User;
public interface UserDao {
@Select("select * from user where id=#{id}")
public User getUserById(@Param("id") Integer id);
public List<User> query();
public List<Map> querySection(Integer id);
}
userMapper.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="soc.dao.UserDao">
<cache readOnly="true" eviction="FIFO" flushInterval="60000" />
<resultMap id="BaseResultMap" type="soc.entity.User">
<result column="id" property="id" />
<result column="name" property="name" />
<result column="salary" property="salary" />
<result column="birthday" property="birthday" />
</resultMap>
<sql id="Base_Column_List">
id,name,salary,birthday
</sql>
<select id="query" resultMap="BaseResultMap" useCache="true">
select
<include refid="Base_Column_List" />
from user
</select>
<select id="querySection" resultType="map" parameterType="integer"
useCache="true">
select
name,salary
from user where
<if test="id !=null"></if>
id=#{id}
</select>
</mapper>
结果运行querySection()后报错如下:
Mybatis There is no getter for property named 'id in 'class java.lang.Integer.
解决方法是:
1.在userMapper.xml中将<if test="id !=null"></if>去掉,即不加判断条件;
2.在userMapper.xml中将<if test="id !=null"></if>id=#{id}改为<if test="_parameter !=null"></if>id=#{_parameter}
3.仍然是<if test="id !=null"></if>id=#{id},但在userDao.java里将querySection(Integer id)方法改成querySection(@Param("id") Integer id).