您的位置:首页 > 汽车 > 时评 > Mybatis——动态SQL常用标签

Mybatis——动态SQL常用标签

2024/11/18 3:17:56 来源:https://blog.csdn.net/chenyang_88/article/details/140589469  浏览:    关键词:Mybatis——动态SQL常用标签

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层面,去执行一个逻辑代码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com