其次where标签可以判断第一个查询条件前面有没有and,如果有则会删除
<select id="login2" resultType="com.example.demo.model.UserInfo"> select * from userinfo <where> <if test="username!=null"> username=#{username} </if> <if test="password!=null"> and password=#{password} </if> </where> </select>
set标签
和where的使用基本一样
可以自动帮助你处理最后一个逗号,并且自动写set
<update id="update" parameterType="map"> update blog <set> <if test="newTitle != null"> title=#{newTitle}, </if> <if test="newAuthor != null"> author=#{newAuthor}, </if> <if test="newViews != null"> views = #{newViews} </if> </set> <where> <if test="id != null"> id=#{id} </if> <if test="title != null"> and title=#{title} </if> <if test="author != null"> and author=#{author} </if> <if test="views != null"> and views = #{views} </if> </where> </update>
foreach标签
- foreach属性:
- collection:参数集合的名字
- item:给接下来要遍历的集合起的名字
- open:加的前缀是什么
- close:加的后缀是什么
- separator:每次遍历之间间隔的字符串
<delete id="dels"> delete from userinfo where id in <foreach collection="list" item="item" open="(" close=")" separator="," > #{item} </foreach> </delete>
到此这篇关于Mybatis详解动态SQL以及单表多表查询的应用的文章就介绍到这了,更多相关Mybatis动态SQL内容请搜索
更多SQL内容来自木庄网络博客