IF
配置:
public interface BlogMapper {// 查询博客List<Blog> queryBlogIF(Map map);}
<select id="queryBlogIF" parameterType="map" resultType="blog">select * from mybatis.blog where 1=1<if test="title != null">and title = #{title}</if><if test="author != null">and author = #{author}</if></select>
测试:
@Testpublic void queryBlogIF(){SqlSession sqlSession = MybatisUtils.getSqlSession();BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);HashMap map = new HashMap();map.put("title","学好Mybatis");List<Blog> blogs = mapper.queryBlogIF(map);for (Blog blog : blogs) {System.out.println(blog);}sqlSession.close();}
choose、when、otherwise
<select id="queryBlogChoose" parameterType="map" resultType="blog">select * from mybatis.blog<where><choose><when test="title != null">title = #{title}</when><when test="author != null">and author = #{author}</when><otherwise>and views = #{views}</otherwise></choose></where></select>
trim、where、set
<select id="queryBlogIF" parameterType="map" resultType="blog">select * from mybatis.blog<where><if test="title != null">title = #{title}</if><if test="author != null">and author = #{author}</if></where></select>
<update id="updateBlog" parameterType="map">update mybatis.blog<set><if test="title != null">title = #{title},</if><if test="author != null">author = #{author]</if></set>where id = #{id}</update>
所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,去执行一个逻辑代码