MyBatis 的动态 SQL 包括以下几种元素,如下表所示。

1.if标签
if 语句使用方法简单,常常与 test 属性联合使用,语法如下:
<if test="判断条件">
SQL语句
</if>当判断条件为 true 时,才会执行所包含的 SQL 语句。
最常见的场景是在 if 语句中包含 where 子句,例如:
<!--if标签
test:使用对象的属性值作为条件
id=-1 能避免sql语句错误
-->
<select id="selectByIf" resultType="com.itjuzi.entity.Student">
select * from student
where id=-1
<if test="name != null and name!='' ">
or name like "%" #{name} "%"
</if>
<if test="age > 0">
or age = #{age}
</if>
</select>注意:
使用动态sql,出现 > >= < <= 符号时,最好将其转换为实体符号,否则xml可能解析会出现问题,特别是 < 符号。
2.where标签
使用if标签时容易引起sql语句语法错误,使用where标签可以解决这些问题。
使用where,里面是一个或多个if标签,当有一个if标签判断条件为true,where标签会转为WHERE关键字附加到sql语句后面;如果if没有一个条件为true,则忽略where和里面的if。
<where>
<if test="条件1">sql语句1</if>
<if test="条件2">sql语句2</if>
</where>这里的like concat使用了模糊查询,
<resultMap id="Map" type="com.zs.entity.Book">
<id column="book_id" property="id"/>
<result property="name" column="book_name"/>
<result column="book_author" property="author"/>
<result property="price" column="book_price"/>
<result column="book_pub" property="pub"/>
</resultMap>
<select id="select" resultMap="Map">
select * from book_info
<where>
<if test="name!=null and name!=''">
and book_name like concat('%',#{name},'%')
</if>
<if test="author!=null and author!=''">
and book_author=#{author}
</if>
</where>
</select>测试:
@Test
public void select(){
BookDao bookDao = session.getMapper(BookDao.class);
Map<String,Object> map=new HashMap<String, Object>();//从网页中得到参数值 并且封装到map对象中。
map.put("name","红");
List<Book> list = bookDao.select(map);
System.out.println(list);
}3.[choose when otherwise] 和where
<select id="select2" resultMap="Map">
select * from book_info
<where>
<choose>
<when test="name!=null and name!=''">
and book_name=#{name}
</when>
<when test="author!=null and author!=''">
and book_author=#{author}
</when>
<otherwise>
and book_price>35
</otherwise>
</choose>
</where>
</select>测试:
@Test
public void select2(){
BookDao bookDao = session.getMapper(BookDao.class);
Map<String,Object> map=new HashMap<String,Object>();//从网页中得到参数值 并且封装到map对象中。
/* map.put("name","平凡的世界");
map.put("author","路遥");*/
List<Book> list = bookDao.select2(map);
System.out.println(list);
}4.set标签
用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,在进行表单更新的操作中,哪个字段中有值才去更新,如果某项为 null 则不进行更新,而是保持数据库原值。
<update id="update">
update book_info
<set>
<if test="name!=null and name!=''">
book_name=#{name},
</if>
<if test="author!=null and author!=''">
book_author=#{author},
</if>
<if test="pub!=null and pub!=''">
book_pub=#{pub},
</if>
<if test="price!=null">
book_price=#{price},
</if>
</set>
where book_id=#{id}
</update>测试:
@Test
public void testUpdate(){
BookDao bookDao = session.getMapper(BookDao.class);
Book book=new Book();
book.setAuthor("大冰");
book.setName("我不");
book.setId(1005);
bookDao.update(book);
session.commit();
}5.foreach标签
使用foreach可以循环数组,list集合,一般使用在in语句中。
<foreach collection="集合类型" open="开始的字符" close="结束的字符"
item="集合中的成员" separator="集合成员之间的分割符">
#{item的值}
</foreach>
标签属性:
collection:表示循环的对象是数组还是list集合。如果dao方法的形参是数组,collection="array";
如果dao方法形参是list,collection="list";
open:循环开始的字符。sql.append("(");
close:循环结束的字符。sql.append(")");
item:集合成员,自定义的变量。Integer item = idList.get(i);
separator:集合成员之间的分隔符。sql.append(",");
#{item的值}:获取集合成员的值;<delete id="batchDelete">
delete from book_info where book_id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>测试:
@Test
public void delete(){
BookDao bookDao = session.getMapper(BookDao.class);
int [] arr ={1001,1002,1003};
bookDao.batchDelete(arr);
session.commit();
}版权声明:本文为weixin_47794617原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。