if:
<!-- 动态sql:if -->
<select id="selectNewsIf" parameterType="news" resultType="news">
select * from news where 1=1
<if test="title!=null"> and title=#{title}</if>
<if test="owner!=null">and owner=#{owner}</if>
<!-- 另一种写法 -->
<if test="title!=null and owner!=null"> and title=#{title} and owner=#{owner}</if>
</select>
where:
where 标记会自动将其后第一个条件的and或者是or给忽略掉
<select id="selectNews" parameterType="news" resultType="news">
select * from news
<where>
<if test="title!=null">title=#{title}</if>
<if test="owner!=null">and owner=#{owner}</if>
</where>
</select>
choose:
相当于java的swtich,只会执行一种情况
<!-- 动态sql:choose -->
<select id="selectNewsChoose" parameterType="news" resultType="news">
select * from news where 1=1
<choose>
<when test="title!=null">and title=#{title}</when>
<when test="owner!=null">and owner=#{owner}</when>
<otherwise>and owner="中山气象"</otherwise>
</choose>
</select>
trim:
属性 描述
prefix: 给sql语句拼接的前缀
suffix: 给sql语句拼接的后缀
prefixOverrides: 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND"
suffixOverrides: 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定
<!-- 动态sql:trim -->
<select id="selectNewsTrim" parameterType="news" resultType="news">
select * from news
<trim prefix="where" prefixOverrides="and|or">
<if test="title!=null">title=#{title}</if>
<if test="owner!=null">and owner=#{owner}</if>
</trim>
</select>
set:
如果包含的语句是以逗号结束的话将会把该逗号忽略,如果 set 包含的内容为空的话则会出错
<!-- 动态sql:set -->
<update id="updateNewsSet" parameterType="news">
update news
<set>
<if test="title!=null">title=#{title},</if>
<if test="owner!=null">owner=#{owner}</if>
</set>
where id=#{id}
</update>
foreach:
foreach元素的属性主要有item,index,collection,open,separator,close。
item:集合中元素迭代时的别名,该参数为必选。
index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选
open:foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选
separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
collection: 要做foreach的对象,作为入参时,List对象默认用"list"代替作为键,数组对象有"array"代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param(“keyName”)来设置键,设置keyName后,list,array将会失效。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。
举个例子:如果User有属性List ids。入参是User对象,那么这个collection = “ids”.如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = “ids.id”
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
针对最后一条,我们来看一下官方说法:
注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。
所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一个list为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则colloection里放的是自己封装的map里的key值
接口:
//list
public List<News> selectNewsForeach(List<Integer> newsId);
//map
public List<News> selectNewsMap(Map<String,Object> map);
mapper映射文件:
<!-- 动态sql:foreach list -->
<select id="selectNewsForeach" parameterType="List" resultType="news">
select * from news where id in
<foreach collection="list" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</select>
<!-- 动态sql:foreach map-->
<select id="selectNewsMap" parameterType="List" resultType="news">
select * from news where id in
<foreach collection="ids" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</select>
test测试文件:
//foreach list
@Test
public void selectForeachList() {
SqlSession sqlSession = MybatisUtils.getSession();
NewsDao mapper = sqlSession.getMapper(NewsDao.class);
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(3);
List<News> selectNewsForeach = mapper.selectNewsForeach(arrayList);
System.out.println("------------------------------");
System.out.println("查询结果:");
for (News news : selectNewsForeach) {
System.out.println(news);
}
sqlSession.close();
}
//foreach map
@Test
public void selectForeachMap() {
SqlSession sqlSession = MybatisUtils.getSession();
NewsDao mapper = sqlSession.getMapper(NewsDao.class);
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(3);
Map<String, Object> map = new HashMap<String,Object>();
map.put("ids", arrayList);
List<News> selectNewsMap = mapper.selectNewsMap(map);
System.out.println("------------------------------");
System.out.println("查询结果:");
for (News news : selectNewsMap) {
System.out.println(news);
}
sqlSession.close();
}
bind:
接口:
public List<News> selectNewsBind(News news);
映射文件:
<!-- 动态sql:bind value值为实体类中的属性-->
<select id="selectNewsBind" parameterType="news" resultType="news">
<bind name="bindValue" value="'%' +title+'%'"/>
select * from news where title like #{bindValue}
</select>