[Mybatis]11.动态sql

什么动态sql?

指的是:根据不同的条件生成的不同sql语句

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

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

搭建测试环境

创建一张表

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接口和mapper.xml

常用标签

IF

    <select id="queryBlogIF" parameterType="map" resultType="Blog">
        select * from blog where 1=1
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = ${author}
        </if>
    </select>

可以使用万能map来进行参数的传递

    @Test
    public void queryBlogIF(){
            SqlSession sqlSession = MybatisUtil.getSqlSession();

            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

            HashMap<String, Object> 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 blog
        <where>
            <choose>

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

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

                <otherwise>
                    view = #{views}
                </otherwise>

            </choose>
        </where>
    </select>

trim(where,set)

    <select id="queryBlogIF" parameterType="map" resultType="Blog">
        select * from blog
        <where>
            <if test="title != null">
                and 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片段
有的时候,我们可能会将一些功能的部分抽取出来蛮方便复用!

  1. 使用SQL变迁抽取公共的部分
    <sql id="if-title-author">
        <if test="title != null">
            title = #{title},
        </if>
        <if test="author != null">
            author = #{author},
        </if>
    </sql>
  1. 在需要使用的地方使用include标签引用即可
    <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <include refid="if-title-author"></include>
        </set>
        where id = #{id}
    </update>

注意事项:

  • 最好基于单表来定义SQL片段
  • 不要存在where标签
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值