Mybatis动态SQL,以及常用标签

什么是动态SQL:
动态SQL就是指根据不同的条件生成不同的SQL语句
环境搭建

CREATE TABLE `blog`(
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客标题',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`views` INT(30) NOT NULL COMMENT '浏览量'
)ENGINE=INNODB DEFAULT CHARSET=utf8

准备工作 :创建一个基础工程
1,导包
2,编写配置文件
3,编写实体类
4,编写mapper接口以及xml文件

一,
if
接口方法
在这里插入图片描述
mapper.xml

select * from blog where 1=1

and title = #{title}


and author =#{author}

</select>

if 即为判断条件
在if标签内可添加追加条件

测试方法

@Test
    public void selectBlogIfTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);


        HashMap map = new HashMap();
        map.put("title","Java如此简单");
        List<Blog> blogs = mapper.selectBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }

查询结果
在这里插入图片描述
二,
choose(when,otherwise)


    <select id="selectBlogChoose" parameterType="map" resultType="com.briup.pojo.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>

choose标签类似于java中的swich case语句
当满足第一个条件时,后面的都不执行

三,
trim(where,set)

   select * from blog 
        <where>
            <if test="title != null" >
                 title = #{title}
            </if>
            <if test="author != null">
                and author =#{author}
            </if>
        </where>
<update id="UpdateBlog" parameterType="map" >
        update blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author}
            </if>

        </set>
        where id = #{id}
    </update>

Foreach遍历

普通SQL语句
select * from user where 1 =1
and (id = 1 or id = 2 or id = 3)

使用Foreach标签

<foreach collection="ids" item="id"
         open="(" separator="or" close=")">
            #{id}
        </foreach>

举例:
查询1,2,3号博客

<select id="selectBlogForeach" parameterType="map"  resultType="com.briup.pojo.Blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id"
                     open="and (" close=")" separator="or"

            >
                id =#{id}

            </foreach>

        </where>

    </select>

传入一个map,在map中传入一个list集合,这个list集合叫ids,在这个list集合中传入你想要查询的id(1,2,3)

小结:
所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,执行一个逻辑代码if,where,set,choose,when

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值