• 动态SQL


    什么是动态SQL

    • 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。

    标签:

    当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。

        <sql id="allColumns">
            id,username,birthday,sex,address
        sql>
    
    • 1
    • 2
    • 3

    标签:

    用于引用定义的常量。

       <select id="getAll" resultType="users" >
            select <include refid="allColumns">include>
            from users
        select>
    
    • 1
    • 2
    • 3
    • 4

    标签:

    进行条件判断,配合其他标签使用。

    <if test="userName != null and userName != '' ">
        and username like concat('%',#{userName},'%')
    if>
    
    • 1
    • 2
    • 3

    标签:

    where标签的作用:让where子句更加动态智能。
    有多个if查询条件时,进行多条件拼接。

    
    
    <select id="getByCondition" resultType="users" parameterType="users">
        select <include refid="allColumns">include>
        from users
        <where>
            <if test="userName != null and userName != '' ">
                and username like concat('%',#{userName},'%')
            if>
            <if test="birthday != null ">
                and birthday = #{birthday}
            if>
            <if test="sex != null and sex !=''">
                and sex = #{sex}
            if>
            <if test="address != null and address !=''">
                and address like concat('%',#{address},'%')
            if>
        where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    标签:

    使用 if+set 标签,在进行表单更新的操作中,哪个字段中有值才去更新,如果某项为 null 则不进行更新,而是保持数据库原值。
    至少更新一列,不更新别调这个更新方法,会报错。

    
    
    <update id="updateBySet" parameterType="users">
        update users
        <set>
            <if test="userName != null and userName != ''">
                username = #{userName},
            if>
            <if test="birthday != null">
                birthday = #{birthday},
            if>
            <if test="sex != null and sex != ''">
                sex =#{sex},
            if>
            <if test="address != null and address !=''">
                address = #{address}
            if>
        set>
        where id = #{id}
    update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    标签:

    <choose>
      <when>when>
      <when>when>
      <when>when>
      <otherwise>otherwise>
    choose>
    等同于:
    if(){
        
    }else if(){
        
    }else if(){
        
    }else if(){
        
    }else{
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    标签:

    主要用来进行集合或数组的遍历,动态生成SQL,主要有以下参数:

    • collection:collection 属性的值有三个分别是 list、array、map 三种,分别对应的参数类型为:List、数组、map 集合。

    • item :循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details,在list和数组中是其中的对象,在map中是value。

    • index :在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

    • open :表示该语句以什么开始。

    • close :表示该语句以什么结束。

    • separator :表示元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。

    • 批量查询:

    
        <select id="getByIds" resultType="users">
            select <include refid="allColumns">include>
            from users
            where id in
               <foreach collection="array" item="id" separator="," open="(" close=")">
                   #{id}
               foreach>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 批量删除:
    
    <delete id="deleteBatch" >
        delete from users where id in
        <foreach collection="array" item="id" close=")" open="(" separator=",">
            #{id}
        foreach>
    delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 批量增加:
    
        <insert id="insertBatch" >
            insert into users(username,birthday,sex,address) values
            <foreach collection="list" separator="," item="u">
                (#{u.userName},#{u.birthday},#{u.sex},#{u.address})
            foreach>
        insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 批量更新:难点
    
        <update id="updateSet"  >
           <foreach collection="list" item="u" separator=";">
            update users
            <set>
                <if test="u.userName != null  and u.userName != ''">
                    username=#{u.userName},
                if>
                <if test="u.birthday != null">
                    birthday = #{u.birthday},
                if>
                <if test="u.sex != null  and u.sex != ''">
                    sex = #{u.sex},
                if>
                <if test="u.address != null  and u.address != ''">
                    address = #{u.address}
                if>
            set>
            where id = #{u.id}
           foreach>
        update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注意:要使用批量更新,必须在jdbc.properties属性文件中的url中添加&allowMultiQueries=true,允许多行操作。

  • 相关阅读:
    WEEX上线高防系统,防御效果拔群,开启20万U盛大回馈庆典
    【CAN总线】从数字设计的角度分析CAN协议1—CAN概述
    MAC执行graalvm并编译
    java计算机毕业设计九宫格日志网站源码+数据库+系统+lw文档+mybatis+运行部署
    Java设计模式之原型模式
    Splunk Enterprise 9.0.0 (macOS, Linux, Windows) -- 机器数据管理和分析
    第三篇文章:springboot里面的配置文件 application.properties
    数组去重有哪些方法
    PTA 甲级 1016 Phone Bills
    传统 API 管理与测试过程正面临严峻的挑战
  • 原文地址:https://blog.csdn.net/m0_53881899/article/details/126871090