鍍金池/ 問答/Java  C++/ mybatis 保存用戶數(shù)據(jù)報錯

mybatis 保存用戶數(shù)據(jù)報錯

<sql id="key">
        <trim suffixOverrides=",">
            <if test="id!=null">
                id,
            </if>
            <if test="name!=null">
                name,
            </if>
            <if test="password!=null">
                password,
            </if>
            <if test="type!=null">
                type,
            </if>
            <if test="nikename != null">
                nikename,
            </if>
            <if test="age != null">
                age,
            </if>
            <if test="sex != null">
                sex,
            </if>
            <if test="email != null">
                email,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="avatar != null">
                avatar,
            </if>
            <if test="location != null">
                location
            </if>
        </trim>
    </sql>

    <sql id="value">
        <trim suffixOverrides=",">
            <if test="id!=null">
                #{id},
            </if>
            <if test="name!=null">
                #{name},
            </if>
            <if test="password!=null">
                #{password},
            </if>
            <if test="type!=null">
                #{type},
            </if>
            <if test="nikename != null">
                #{nikename},
            </if>
            <if test="age != null">
                #{age},
            </if>
            <if test="sex != null">
                #{sex},
            </if>
            <if test="email != null">
                #{email},
            </if>
            <if test="phone != null">
                #{phone},
            </if>
            <if test="avatar != null">
                #{avatar},
            </if>
            <if test="location != null">
                #{location}
            </if>
        </trim>
    </sql>
 <insert id="saveUser" keyProperty="id" parameterType="User">
        insert into users(<include refid="key"/>) values(<include refid="value"/>)
    </insert>
@Repository
public interface UserDao {

    boolean addUser(@Param("user") User user);

    int saveUser(@Param("user") User user);

    User findByName(@Param("name")String name);

    User findBy(@Param("name")String name,@Param("password")String password);

    List<User> findAllUser();
}

clipboard.png

請問大家,我這樣寫有問題嗎,如果有,如何改呢


改成下面這樣還是報錯

<insert id="saveUser" parameterType="User">
        insert into users(id,name,password) values(#{id},#{name},#{password})
    </insert>
回答
編輯回答
裸橙

id != "" 或者 != 0

如果id是基本類型

2018年5月18日 17:48
編輯回答
厭遇

名稱不匹配錯誤吧

2017年3月22日 15:43
編輯回答
拮據(jù)

@Param("user")刪除,因為這樣和你在xml代碼中的

parameterType="User"

無法建立映射關(guān)系。

2018年5月28日 17:29
編輯回答
北城荒

解決了,把@Param("user")去掉就OK了,唉,弄這么久,還有,如果要加@Param("user")的話,那么xml文件里面對應(yīng)的id就要換成user.id這樣的前綴了

int saveUser(@Param("user") User user);
2017年5月23日 23:57