动态SQL--IF语句
1、BlogMapper
public interface BlogMapper {
//查询博客
List<Blog> queryBlogIF(Map map);
}
2、BlogMapper.xml
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</where>
</select>
3、测试
@Test
public void queryBlogIFTest(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("title","算法");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog:blogs) {
System.out.println(blog);
}
sqlSession.close();
}
详情参考:https://mybatis.org/mybatis-3/zh/dynamic-sql.html