动态SQL
1、条件查询
<select id="getBlogIf" parameterType="Map" resultType="Blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author =#{author}
</if>
</select>
如果有条件,会直接拼接在后面,实现动态,即使一个条件也没有,也有where 1=1
2、优化<where>标签
<select id="getBlogChose" parameterType="Map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author =#{author}
</if>
</where>
</select>
将where 1=1 去掉,加上<where>标签,他能够智能的决定加不加“and”
3、<choose>
<select id="getBlogChose" parameterType="Map" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author =#{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
choose标签的作用与switch差不多,选择,但是有顺序,如果第一个、第二个条件满足,只会走第一个条件
本质是个trim
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
set同理
<trim prefix="SET" suffixOverrides=",">
...
</trim>
要持续学习的是:
mysql引擎
InnoDB底层原理
索引
索引优化