mybatis 保存用户数据报错

<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>
阅读 3.4k
4 个回答

解决了,把@Param("user")去掉就OK了,唉,弄这么久,还有,如果要加@Param("user")的话,那么xml文件里面对应的id就要换成user.id这样的前缀了

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

@Param("user")删除,因为这样和你在xml代码中的

parameterType="User"

无法建立映射关系。

名称不匹配错误吧

新手上路,请多包涵

id != "" 或者 != 0

如果id是基本类型

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题