mybatis xml方式insert语句动态拼接需要插入的字段
- xml代码
<insert id="addUser" parameterType="bean.user" useGeneratedKeys="true" keyProperty="id">
insert into user(name,sex
<include refid="getTestTbField" />
createTime)
values(#{name},#{sex}
<include refid="getTestTbValue" />
SYSDATE())
</insert>
<sql id="getTestTbField">
<trim suffix=",">
<if test="tel!=null and tel!='null' and tel!=''">
tel,
</if>
<if
test="address!=null and address!='null' and address!=''">
address,
</if>
<if test="remark!=null and remark!='null' and remark!=''">
remark
</if>
</trim>
</sql>
<sql id="getTestTbValue">
<trim suffix=",">
<if test="tel!=null and tel!='null' and tel!=''">
#{tel},
</if>
<if
test="address!=null and address!='null' and address!=''">
#{address},
</if>
<if test="remark!=null and remark!='null' and remark!=''">
#{remark},
</if>
</trim>
</sql>
useGeneratedKeys=true表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。
keyProperty这里指定自动增长的字段