if语句
1.编写接口类
List queryBlogIf(Map map);
2.编写mapper.xml
select * from blog where
title = #{title}
and author = #{author}
3.编写test
@Test
public void queryBlogIf(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap<String, String> map = new HashMap<>();
map.put(“title”,“Mybatis如此简单”);
map.put(“author”,“狂神说”);
List blogs = mapper.queryBlogIf(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
注意:单纯的使用if语句的话容易在sql语句连接的过程中,比如and出现问题
Where语句
select * from blog
and title = #{title}
and author = #{author}
注意:使用where可以避免sql拼接过程中出现多写and的问题
Set语