根据不同的条件需要执行不同的SQL命令,成为动态SQL。
MyBatis中动态SQL在mapper.xml中添加逻辑判断。
常用的逻辑判断:
①<if>
②<where>
③<chose><where><otherwise>
④<set>
⑤<Trim>
⑥<bind>
⑦<foreach>
⑧<sql>和<include>
1.<if>
使用
(1) 接口
public interface FlowerMapper {
Flower selByIdName(Flower flower);
}
(2) mapper.xml
<select id="selByIdName" resultType="flower">
select * from flower where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
</select>
2. <where>
使用
①当编写where标签时,如果内容第一个是and,可以去掉不写
②使用<where>
标签时,如果有内容会生成where关键字,如果没有内容,不会生成where关键字
(1) 接口
public interface FlowerMapper {
Flower selByIdName(Flower flower);
}
(2) mapper.xml
<select id="selByIdName" resultType="flower">
select * from flower
<where>
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
</where>
</select>
3. <choose><when><otherwise>
使用
【注意】只要一个条件成立,其他任何条件都不执行
(1) 接口
public interface FlowerMapper {
Flower selByIdName(Flower flower);
}
(2) mapper.xml
<select id="selByIdName" resultType="flower">
select * from flower
<where>
<choose>
<when test="id!=null and id!=''">
id=#{id}
</when>
<when test="name!=null and name!=''">
and name=#{name}
</when>
</choose>
</where>
</select>
4. <set>
使用
①如果
<set>
里面有内容,生成set关键字,没有则不生成
②可以去掉最后一个逗号
【注意】id=#{id},目的是防止 <set>
中没有内容,mybatis不生成set关键字,导致SQl语法错误。
mapper.xml
<update id="upd" parameterType="flower">
update flower
<set>
id=#{id},
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="production!=null and production!=''">
production=#{production},
</if>
</set>
where id=#{id}
</update>
5. <Trim>
使用
(1)prefix:在前面添加内容
(2)prefixOverrides:去掉前面的内容
(3)suffix:在后面添加内容
(4)suffixOverrides:去掉后边的内容
【注意】执行顺序去掉内容后添加内容
<update id="upd" parameterType="flower">
update flower
<trim prefix="set" suffixOverrides=",">
name=#{name},
</trim>
where id = #{id}
</update>
6. <bind>
使用
(1)作用给参数重新赋值
(2)使用场景:
①在原内容前或者后添加内容
②模糊查询
① 在原内容前或者后添加内容
<select id="selByFlower" parameterType="flower" resultType="flower">
<bind name="price" value="'$'+price"/>
#{price}
</select>
输出日志结果:
② 模糊查询
<select id="selByFlower" parameterType="flower" resultType="flower">
<bind name="name" value="'%'+name+'%'"/>
</select>
7. <foreach>
使用
(1)循环参数内容,同时可以在内容的前后添加内容,添加分隔符的功能。
(2)适用场景:in查询,批量新增
(mybatis中foreach效率比较低)
①in查询
SQL语句:
select * from flower where in (1,2,3)
java代码设置遍历的集合:
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
(1)collection=""要遍历的集合
(2)item 迭代变量 #{变量名}获取内容
(3)open 循环后左侧添加的内容
(4)close 循环后右侧添加的内容
(5)separator每次循环时元素之间的分隔符
<select id="selIn" parameterType="list" resultType="flower">
select * from flower where id in
<foreach collection="list" item="a" open="(" close=")" separator=",">
#{a}
</foreach>
</select>
②批量新增
SQL语句:
insert into flower values
(default,"名字","价格","产地"),
(default,"名字","价格","产地"),
(default,"名字","价格","产地")
批量新增openSession()必须指定
factory.openSession(ExecutorType.BATCH);
8. <sql>
和<include>
使用(多表联合查询用的多)
(1)某些SQL片段如果希望复用,可以使用<sql>
定义这个片段
<sql id="mysql">
id,name,price,production
</sql>
(2)在<select>
或<delete>
或<update>
或<insert>
中使用<include>
引用
<select id="selAll">
select <include refid="mysql"></include>
from flower
</select>