接口:
//利用if 查询数据 动态sql
List<Blog> getBlogIF(Map map);
接口配置文件:
<sql id="blog">
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</sql>
<select id="getBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<include refid="blog"></include>
</where>
测试类:
@Test
public void getBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession ();
BlogMapper mapper = sqlSession.getMapper (BlogMapper.class);
HashMap map = new HashMap ();
map.put ("title","狂神说JAVA2");
map.put ("id","9f56ae230e5c4c8a971cebbbe64b895c");
int i = mapper.updateBlog (map);
sqlSession.close ();
}