<select id="findCustomerById" parameterType="Integer" resultType="com.itheima.po.Customer" > SELECT * FROM t_customer WHERE id = #{id} </select> <insert id="addCustomer" parameterType="com.itheima.po.Customer" keyProperty="id" useGeneratedKeys="true" > INSERT INTO t_customer(username,jobs,phone) VALUES (#{username}, #{jobs}, #{phone}) </insert> <!-- update和delete和insert实现非常接近 -->
insert 和 update 的子标签 selectKey
提供给你一个与数据库中自动生成主键类似的行为,同时保持了 Java 代码的简洁。
1 2 3 4 5 6 7
<insertid="addCustomer"parameterType="com.itheima.po.Customer"> <selectKeykeyProperty="id"resultType="Integer"order="BEFORE"> select if(max(id) is null, 1, max(id)+1) as new newId from t_customer </selectKey> INSERT INTO t_customer(id,username,jobs,phone) VALUES (#{id},#{username}, #{jobs}, #{phone}) </insert>
<!-- 普通键值对存储 --> <!-- 没有显式指定 resultMap --> <selectid="selectUsers"resultType="map"> select id, username, hashedPassword from some_table where id = #{id} </select> <!-- 将规范的JavaBean 映射到 ResultSet --> <selectid="selectUsers"resultType="com.someapp.model.User"> select id, username, hashedPassword from some_table where id = #{id} </select> <!-- 如果列名和属性名没有精确匹配(不规范的JavaBean),可以在 SELECT 语句中对列使用别名 --> <selectid="selectUsers"resultType="com.someapp.model.User"> select user_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_table where id = #{id} </select>
<!-- 非常复杂的语句 --> <selectid="selectBlogDetails"resultMap="detailedBlogResultMap"> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id, P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id, T.name as tag_name from Blog B left outer join Author A on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = #{id} </select>
<selectid="findActiveBlogWithTitleLike"resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <iftest="title != null"> AND title like #{title} </if> </select>
这条语句提供了一种可选的查找文本功能。如果没有传入“title”,那么所有处于“ACTIVE”状态的 BLOG 都会返回;反之若传入了“title”,那么就会对“title”一列进行模糊查找并返回 BLOG 结果
<selectid="findActiveBlogLike"resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <whentest="title != null"> AND title like #{title} </when> <whentest="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!-- 应对使用if时出现的语句错误 --> <!-- where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。 --> <selectid="findActiveBlogLike"resultType="Blog"> SELECT * FROM BLOG <where> <iftest="state != null"> state = #{state} </if> <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>
foreach
动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。
1 2 3 4 5 6 7 8 9 10 11 12 13
<selectid="selectPostIn"resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")" > #{item} </foreach> </select>