<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.sunreal.mapper.BlogMapper"><!--SQL片段,将常用SQL进行封装--><sqlid="if-title-author"><iftest="title != null and title != ''">
title = #{title}
</if><iftest='state != null and state != "1"'>
and author = #{author}
</if></sql><!--如果实体类(createTime),数据库(create_time),
需要的<settings>标签中设置,开启驼峰转换
<setting name="mapUnderscoreToCamelCase" value="true"/>,
或使用<resultMap>标签进行映射--><insertid="addBlog"parameterType="Blog">
INSERT INTO mybatis.blog (id, title, author, create_time, views)
VALUES (#{id}, #{title}, #{author}, #{createTime}, #{views})
</insert><!--<where>标签可以自动过滤多余的and--><!--注意:<test>标签中参数要是有双引号("")state != "1",否则无效--><selectid="queryBlog"resultType="Blog"parameterType="map">
select *
from mybatis.blog
<where><includerefid="if-title-author"></include></where></select><!--<choose>标签,相当于JAVA的switch语句,
即使多个条件符合要求,也只会选择优先级最高的一条进行执行,
同时也可以自动过滤多余(and)--><selectid="queryBlogChoose"resultType="Blog">
select * from mybatis.blog
<where><choose><whentest="title != null and title != ''">
title = #{title}
</when><whentest="author != null and author != ''">
and author = #{author}
</when><otherwise>
and views = '1000'
</otherwise></choose></where></select><!--<set>标签,在更新操作中自动过滤多余的逗号(,)--><updateid="updateBlog"parameterType="map">
UPDATE MYBATIS.BLOG
<set><iftest="title != null and title != ''">
TITLE = #{title},
</if><iftest="author != null and author != ''">
AUTHOR = #{author}
</if></set>
WHERE ID = #{id}
</update><!--foreach,需要传入集合(ids)--><!--select * from mybatis.blog WHERE title = ? and ( id = ? or id = ? )--><selectid="queryBlogFReach"resultType="Blog"parameterType="map">
select *
from mybatis.blog
<where><iftest="title != null and title != ''">
title = #{title}
</if><foreachcollection="ids"item="id"open="and ("close=")"separator="or">
id = #{id}
</foreach></where></select></mapper>