本篇介绍MyBatis里如何使用动态SQL,了解如何去简单使用动态标签;如有错误,请在评论区指正,让我们一起交流,共同进步!
使用动态SQL的好处:根据不同的条件拼接 SQL 语句,提高了SQL的灵活性;
示例:
<insert id="add4">
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username != null">
username,
</if>
<if test="password">
password,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username != null">
#{username},
</if>
<if test="password != null">
#{password},
</if>
</trim>
</insert>
示例:
<select id="testWhere" resultType="com.example.demo.model.Userinfo">
select * from userinfo
<where>
<if test="id > 0">
id=#{id}
</if>
<if test="username != null">
and username=#{username}
</if>
</where>
</select>
<update id="update">
update userinfo
<set>
<if test="username != null">
username=#{username},
</if>
<if test="password != null">
password=#{password}
</if>
</set>
where id=#{id}
</update>
<delete id="delByIds">
delete from userinfo
where id in
<foreach collection="lists" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</delete>
✨✨✨各位读友,本篇分享到内容如果对你有帮助给个赞鼓励一下吧!!
感谢每一位一起走到这的伙伴,我们可以一起交流进步!!!一起加油吧!!!