<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();
}
请问大家,我这样写有问题吗,如果有,如何改呢
改成下面这样还是报错
<insert id="saveUser" parameterType="User">
insert into users(id,name,password) values(#{id},#{name},#{password})
</insert>
解决了,把@Param("user")去掉就OK了,唉,弄这么久,还有,如果要加@Param("user")的话,那么xml文件里面对应的id就要换成user.id这样的前缀了