<insert id="insertProduct" parameterType="Production">
insert into py
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="wc.name!=null and wc.name!=''">
name,
</if>
<if test="wc.price!=null and wc.price!=''">
price,
</if>
<if test="wc.coreproject!=null and wc.coreproject!=''">
coreproject,
</if>
<if test="wc.features!=null and wc.features!=''">
features
</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="wc.name!=null and wc.name!=''">
#{wc.name},
</if>
<if test="wc.price!=null and wc.price!=''">
#{wc.price},
</if>
<if test="wc.coreproject!=null and wc.coreproject!=''">
#{wc.coreproject},
</if>
<if test="wc.features!=null and wc.features!=''">
#{wc.features}
</if>
</trim>
</insert>
- 首先prefix="(" suffix=")"你们应该懂啥意思,就是在语句前后拼接前后大括号.
- 当我们进行插入操作时,可选部分字段进行插入,这时候就要判断该死的前端把这些字段正确传过来了没有. 字段为null或者空串没必要执行插入操作.我们知道我们在写插入语句时括号里面字段之间要用逗号间隔,且最后不能已逗号结尾的. 可是如果恰巧只有第二个和第三个满足条件 语句就会变成:
insert into py(price,coreproject,) values(#{wc.price},#{wc.coreproject},)
这样就会报SQL语句错误
suffixOverrides=","这个方法就是解决这种问题的,用来去除最后一个后缀,这里也就是逗号.