Mybatis动态Sql

动态sql官网描述:

MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

 简而言之,学这个就是为了去简化拼接sql语句操作的,通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。

例如下面这个博客案例

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qi.dao.BlogMapper">
    <insert id="addBlog" parameterType="Blog">
        insert into mybatis.blog (id,title,author,create_time,views)
        values(#{id},#{title},#{author},#{createTime},#{views});
    </insert>
    <!--sql片段-->
    <sql id="sql1">
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            author = #{author}
        </if>
    </sql>
    <select id="queryBlogIF" parameterType="map" resultType="blog">
        select * from mybatis.blog
        <where>
<!--            <if test="title != null">-->
<!--                title = #{title}-->
<!--            </if>-->
<!--            <if test="author != null">-->
<!--                author = #{author}-->
<!--            </if>-->
            <include refid="sql1"/>
        </where>
    </select>
    <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>
    <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>
    <select id="queryBlogForeach" parameterType="map" resultType="blog">
        select * from mybatis.blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id = #{id}
            </foreach>
        </where>
    </select>
</mapper>

里面补充说明一个Sql片段,合理利用可以少些重复的代码

代码解释

以下是对每个 SQL 语句和 XML 元素的功能和作用的解释:

  1. <mapper namespace="com.qi.dao.BlogMapper">: 指定了这个 XML 文件对应的 MyBatis Mapper 接口的命名空间。

  2. <insert id="addBlog" parameterType="Blog">: 插入操作,将一个 Blog 对象插入到数据库中。

    • id="addBlog": 操作的标识符。
    • parameterType="Blog": 指定了输入参数的类型。
  3. <sql id="sql1">: 定义了一个 SQL 片段,被后续的 <include> 元素引用。

    • <if> 元素:根据条件动态生成 SQL 片段。
  4. <select id="queryBlogIF" parameterType="map" resultType="blog">: 根据条件查询 Blog 记录。

    • <where> 元素:用于包装生成的 WHERE 子句,只在有条件的情况下添加 WHERE 关键字。
    • <include refid="sql1"/>: 引用之前定义的 SQL 片段。
  5. <select id="queryBlogChoose" parameterType="map" resultType="blog">: 使用 <choose> 元素实现条件选择查询。

    • <choose> 元素:类似于 Java 中的 switch 语句,根据条件选择其中一个分支执行。
  6. <update id="updateBlog" parameterType="map">: 更新 Blog 记录。

    • <set> 元素:用于包装 SET 子句,根据条件动态生成 SET 子句。
  7. <select id="queryBlogForeach" parameterType="map" resultType="blog">: 使用 <foreach> 元素实现对指定 id 集合的查询。

    • <foreach> 元素:用于循环遍历集合,生成对应的 SQL 片段。

这部分知识还是要细心去理解的,讲解是ChatGPT生成的不是很到位 

@SuppressWarnings("all")

另外再补充一个抑制警告的注解 @SuppressWarnings("all") //抑制警告,去除不标准命名下的绿线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值