一、接口
沿用上一篇博客“动态SQL环境搭建”项目
//查询博客
List<Blog> queryBlogIF(Map map);
二、Mapper
<select id="queryBlogIF" parameterType="map" resultType="Blog">
select * from blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
三、测试类
@Test
public void queryBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("title" , "Java如此简单");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs){
System.out.println(blog);
}
sqlSession.close();
}
效果: