mybatisxml⽅式insert语句动态拼接需要插⼊的字段mybatis xml⽅式insert语句动态拼接需要插⼊的字段
1. 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>insert语句字段顺序
<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这⾥指定⾃动增长的字段
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论