記錄一次MyBatis插入數據的Bug(org.apache.ibatis.binding.BindingException: Parameter ‘username’ not found)

接口

public interface UserMapper {

    int addUser(@Param("user") User user); #
}

XML文件

<insert id="addUser" parameterType="com.rong.blog.entity.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            id,
        </if>
        <if test="username != null">
            username,
        </if>
        <if test="password != null">
            password,
        </if>
        <if test="address != null">
            address,
        </if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">
            #{id},
        </if>
        <if test="username != null">
            #{username},
        </if>
        <if test="password != null">
            #{password},
        </if>
        <if test="address != null">
            #{address}
        </if>
    </trim>

</insert>

上述方式彙報如下錯誤:
org.apache.ibatis.binding.BindingException: Parameter ‘username’ not found

原因

在接口插入的方法實體類參數前面添加了@Param(“user”),去掉這個註解即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章