mybatis的where、choose、set、foreach使用和sql片段的使用

代码:

 1 <sql id="if-title-author">
 2         <if test="title !=null">
 3             title=#{title}
 4         </if>
 5         <if test="author != null">
 6             and author=#{author}
 7         </if>
 8     </sql>
 9 
10     <select id="queryBlogIF" parameterType="map" resultType="blog">
11         select * from mybatis.blog
12         <where>
13             <include refid="if-title-author"></include>
14         </where>
15     </select>
16 
17     <select id="queryBlogChoose" parameterType="map" resultType="Blog">
18         select * from mybatis.blog
19         <where>
20             <choose>
21 
22                 <when test="title != null">
23                     title =#{title}
24                 </when>
25                 <when test="author != null">
26                     and author = #{author}
27                 </when>
28 
29                 <otherwise>
30                     and views = #{views}
31                 </otherwise>
32 
33             </choose>
34         </where>
35     </select>
36 
37     <update id="updateBlog" parameterType="map">
38         update mybatis.blog
39         <set>
40             <if test="title != null">
41                 title = #{title},
42             </if>
43             <if test="author != null">
44                 author = #{author}
45             </if>
46         </set>
47         where id = #{id}
48     </update>
49 
50     <select id="queryBlogForeach" parameterType="map" resultType="blog">
51         select * from mybatis.blog
52         <where>
53             <foreach collection="ids" item="id" open="(" separator="or" close=")">
54                 id=#{id}
55             </foreach>
56         </where>
57     </select>

 

posted @ 2021-08-26 22:31  现在开始JAVA  阅读(118)  评论(0)    收藏  举报