在Mybatis中实现动态SQL

一、环境搭建

二、编写pojo mapper、util

三、测试

IF和where

Mapper接口和Mapper.xml

    <!--查询blog-->
    ArrayList<Blog> quaryBlog(Map map);   
        
<!--使用Jstl的if标签 查询blog-->
    <select id="quaryBlog" parameterType="map" resultType="com.llf.Pojo.Blog">
            select * from blog where 1=1
         <!--动态sql条件判断   test为判断条件 -->    
        <if test="title != null">
                and title=#{title}
            </if>
            <if test="author!= null">
                and author=#{author}
            </if>
    </select>

测试

    //查询blog
    @Test
    public void quaryBlog(){
      <!--使用Jstl的if标签 查询blog-->
    <select id="quaryBlog" parameterType="map" resultType="com.llf.Pojo.Blog">
            select * from blog
            <where><if test="title != null">
                 title=#{title}
            </if>
                <if test="author!= null">
                and author=#{author}
                </if></where>
​
    </select>
​

IF标签用来判断条件是否成立

where标签用来拼接条件 与常规sql语句中的where相比, where标签在当我们设置的IF条件一个也不满足时 会自动删除 where和and,当我们的条件至少满足一个时,会自动帮我们加上where,去掉and

换句话说 where元素只会在子元素返回任何内容的情况下才插入where子句 而且 若子句的开头为and或or where元素也会将他们删除

如下图所示 我们的第二个条件满足时 它会自动删除我们的and

choose(when、otherwise)

    <!--    //根据title、author中的任意一个条件查询blog-->
    <select id="chooseBlog" parameterType="map" resultType="blog">
        select * from 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>
​

我们把前两个条件注释掉 它自动选择了otherwise的语句

我们把所有条件添加 我们可以看出 它默认执行了下面这个条件

   <when test="title != null">
                title=#{title}
            </when>

由此我们可以得到,choose的功能类似于switch。 里面的条件只要有一个满足 就会跳出结构 如果有多个条件满足,则会按照满足条件的先后顺序,选择第一个满足的条件执行。choose里面嵌套when标签 这里的when标签的使用和IF标签相同 otherwise 当都不满足时 选择该语句执行 适应于查询语句

set

(Set元素会动态地在行首插入set关键字,并删除额外的逗号)

<!--使用set标签更新-->
    <update id="updateblog" parameterType="map">
        update blog
       <set>
         <if test="title != null">
              title=#{title},
         </if>
         <if test="author != null ">
              author=#{author},
         </if>
         <if test="create_time != null">
              create_time=#{createTime},
         </if>
         <if test="views != null">
              views=#{views}
         </if>
        </set>
       where id=#{id}
    </update>

    @Test
    public void updateblog(){
        SqlSession session = MybatisUtils.getsession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("id","b094213b763f49e5a847944f59654889");
        map.put("title","Spring难啊");
        map.put("author","llf");
        mapper.updateblog(map);
        session.commit();
        MybatisUtils.getsession();
    }
​

当我们的条件满足时 它会自动给我们补充set关键字

当我们的条件部分满足时 它会帮我们删除分号

在我们的配置文件中 author后面是有分号的 现在给自动删除了

ForEach

  
  <!--ForEach遍历ID查询blog-->
    <select id="quaryBlogForEach" parameterType="map" resultType="blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="(" close=")" separator="or">
                id = #{id}
            </foreach>
        </where>
    </select>

参数解释 collection 集合名字 itms 每一个元素的键 open 开始的符号 close 结束的符号 separator 分割的符号

思路:先将sql完整写出

  select * from blog where
    (id= 'c82b1bdeaabd4c0db186cec5d342aab1'
     or id='d657700de62f4f6c8a070476ac395bd0'
     or id='659cdc9aece540c585d8bd6c1615a9bc'
     or id='50b0d5cf15154b3c9d21ad81a6c64176'
     )

因为中间用or 连接每一个id 所以separator 的值为or

因为collection 需要一个id的集合 所以我们从map中传递一个id的list集合 键为ids 值为id

item的键要与#{}取值中的键 相同

    @Test
    public void quaryBlogForEach(){
        SqlSession session = MybatisUtils.getsession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> ids = new ArrayList<>();
        ids.add("c82b1bdeaabd4c0db186cec5d342aab1");
        ids.add("d657700de62f4f6c8a070476ac395bd0");
        ids.add("659cdc9aece540c585d8bd6c1615a9bc");
        map.put("ids",ids);
        ArrayList<Blog> blogs = mapper.quaryBlogForEach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        session.close();
    }

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,看它多智能!

提示 你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值

四、总结

所谓动态SQL 就是指根据不同的条件生成不同的SQL语句

动态SQLiu是在拼接SQL语句 我们只要保证SQL的正确性 按照SQL的格式 去排列组合就可以了

现在Mysql中写出完整的SQL 再对应的修改成为我们的动态SQL实现通用即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值