mybatis java
UserDao.java代碼以下:sql
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內容以下:apache
<?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
解決方法是:
app
1.在userMapper.xml中將<if test="id !=null"></if>去掉,即不加判斷條件;spa
2.在userMapper.xml中將<if test="id !=null"></if>id=#{id}改成<if test="_parameter !=null"></if>id=#{_parameter}code
3.仍然是<if test="id !=null"></if>id=#{id},但在userDao.java裏將querySection(Integer id)方法改爲querySection(@Param("id") Integer id).xml