<select id=”findActiveBlogWithIf” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</select>
例如:只搜索有提供查询标题或只搜索有提供查询作者的数据。
如果两者都没有提供,那只返回加精的 Blog 。
<select id=”findActiveBlogWithChoose” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test=”title != null”>
AND title like #{title}
</when>
<when test=”author != null and author.name != null”>
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
<select id=”findActiveBlogWithWhere” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
<where>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</where>
</select>
<select id=”findActiveBlogWithTrim” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
<trim prefix=”WHERE” prefixOverrides=”AND |OR”>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null”>
OR title like #{author.name}
</if>
</trim>
</select>
<select id=”dynamicWithForeachList” resultType=”Blog”>
SELECT * FROM POST P WHERE ID in
<foreach collection=”list” index=”index” item=”item” open=”(“ separator=”,” close=”)”>
#{item}
</foreach>
</select>
上述collection的值为list,对应的Mapper是这样的:
public List<Blog> dynamicWithForeachList(List<Integer> ids);
测试代码:
@Test
public void dynamicWithForeachList() {
SqlSession session = MybatisUtil.openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
List<Blog> blogs = blogMapper.dynamicWithForeachList(ids);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
2.单参数array数组的类型:
<select id=”dynamicWithForeachArray” resultType=”Blog”>
SELECT * FROM POST P WHERE ID in
<foreach collection=”array” index=”index” item=”item” open=”(“ separator=”,” close=”)”>
#{item}
</foreach>
</select>
上述collection为array,对应的Mapper代码:
public List<Blog> dynamicWithForeachArray(int[] ids);
测试代码:
@Test
public void dynamicWithForeachArray() {
SqlSession session = MybatisUtil.openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
int[] ids = new int[] {1,2,3};
List<Blog> blogs = blogMapper.dynamicWithForeachArray(ids);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
3.把参数封装成Map的类型
<select id=”dynamicWithForeachMap” resultType=”Blog”>
SELECT * FROM BLOG WHERE title like ”%”#{title}”%” and id in
<foreach collection=”ids” index=”index” item=”item” open=”(“ separator=”,” close=”)”>
#{item}
</foreach>
</select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List<Blog> dynamicWithForeachMap(Map<String, Object> params);
测试代码:
@Test
public void dynamicWithForeachMap() {
SqlSession session = MybatisUtil.openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
final List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
Map<String, Object> params = new HashMap<String, Object>();
params.put(“ids”, ids);
params.put(“title”, ”Mybatis实战”);
List<Blog> blogs = blogMapper.dynamicWithForeachMap(params);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}