Mybatis学习---动态SQL

本文深入探讨MyBatis框架中的动态SQL技术,包括IF、choose、trim、foreach等标签的使用技巧,以及如何通过SQL片段提高代码复用性。适合希望提升MyBatis应用水平的开发者阅读。

动态SQL语句标签

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

IF标签

使用IF标签可以追加条件,达到用户如果输入了title就追加一个title搜索条件,输入了author就追加一个author搜索条件,都没有输入,就查询所有的形式。

<select id="getBlogIf" 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>

choose when otherwise 标签

不使用所有的条件,只使用其中的一个,例:如果传入了title就按title查询,传入了author就按author查询,如果都没有传入就返回标记为featured的对象.相当于Java中的switch语句,在多个when条件中,只会执行一条,执行完毕之后结束。

<select id="getBlogChoose" resultType="blog" parameterType="map">
    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)

where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。

trim可以自定义前缀与后缀去除

<trim prefix="WHERE" prefixOverrides="AND |OR "> <!-- 前缀为where元素,自动去除多余的and和or -->
  ...
</trim>

<trim prefix="SET" suffixOverrides=","><!-- 前缀为set元素,自动去除多余, -->
  ...
</trim>

SQL片段

sql标签的作用是,抽取重复的sql代码,拿出来进行复用。使用标签与配合完成此功能

<!--抽取SQL片段并设置一个ID -->
<sql id="If-title-author">
    <if test="title != null">
        and  title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</sql>
<!-- 在代码中使用<inculde标签>实现SQL代码复用 -->
<select id="getBlogIf" parameterType="map"  resultType="blog" >
    select * from mybatis.blog
    <where>
        <include refid="If-title-author"></include>
    </where>
</select>

注意:

  • 抽取的SQL代码片段,最好是简单的SQL语句,便于复用,最好基于单表,无复杂语句
  • 最好不要含有<where>标签

foreach标签

<foreach>标签通常用来遍历集合使用。collection为集合名称,open为拼接的字符串前缀,close为后缀,separator为分隔符,item为元素

 @Test
    public void myTest04(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        
        HashMap map = new HashMap();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        //传入集合,集合键值:ids
        map.put("ids",ids);
        List<Blog> blogIf = mapper.getBlogByForeach(map);
        for (Blog blog : blogIf) {
            System.out.println(blog);
        }
        sqlSession.close();
    }
<!--select * from mybatis.blog where 1=1 and(id = 1 or id = 2 or id =3)-->
<select id="getBlogByForeach" resultType="blog" parameterType="map">
    select * from mybatis.blog;
    <where>
        <foreach collection="ids" item="id" open="and(" separator="or" close=")">
            id = #{id}
        </foreach>
    </where>
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值