首页 > 数据库 >11_动态SQL

11_动态SQL

时间:2022-12-01 19:46:48浏览次数:43  
标签:11 title author blog SQL 动态 where id

1.动态SQL的定义根据不同的条件生成不同的SQL语句

如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

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

2.搭建环境

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. 编写实体类

    @Data
    public class Blog {
        private int id;
        private String title;
        private String author;
        private Date createTime;
        private int views;
    
    }
    
  4. 编写实体类对应的Mapper接口和Mapper.xml文件

  5. 测试类

对于Mapper文件的格式,可以从mybatis配置文件中获得:

将configuration,config改为mapper

3.驼峰命名

配置文件中设置:

<settings>
    <!--开启驼峰命名规则:
       是否开启驼峰命名自动映射,
       即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。
       -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

对于驼峰命名,是数据库表中的字段名有下划线,比如:create_time,但java实体类中的属性名是createTime,这个时候就要用到驼峰命名规则。

平常对于属性名不一致问题,通常采用结果集映射完成。

4.SQL中的标签

4.1IF

对于不在select语句中添加限制条件,例:

select *from mybatis.blog where title =#{title}

如果不添加限制条件,但也要达到这样的效果则需使用if语句,

 <if test="title!=null">
        and title=#{title}
    </if>
<if test="author!=null">
    and author=#{author}
</if>

4.2choose(when,otherwise)

只要满足第一个when就结束之后的操作

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>

4.3trim(where,set)

映射文件中的where标签可以过滤掉条件语句中的第一个andor关键字。

select * from mybatis.blog
    <where>
        <if test="title!=null">
            title=#{title}
        </if>
    <if test="author!=null">
        and author=#{author}
    </if>
</where>

set标签用于更新语句

<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,本质还是SQL语句,知识我们在SQL层面去执行一个逻辑代码

4.4SQL片段

有的时候,我们可能会将一些功能的部分抽取出来,方便复用!

1.使用SQL标签抽取公共的部分

<!--实现代码的复用-->
<sql id="if_title_author">
    <if test="title!=null">
        title=#{title}
    </if>
    <if test="author!=null">
        and author=#{author}
    </if>
</sql>

2.在需要复用的地方使用include标签

<select id="queryBlogIf" parameterType="map" resultType="blog">
    select * from mybatis.blog
    <where>
        <include refid="if_title_author"/>
</where>
</select>

注意事项:

  • 最后基于单表来定义SQL片段
  • 不要存在where标签(不在sql标签中)

4.5forEach

select * from user where 1=1 and (id=1 or id=2 or id=3)


 <!--select  * from mybatis.blog where 1=1 and (id=1 or id=2 or id =3)
       现在传递一个万能的map,这个map可以存在一个集合

-->
    <!--当where中的条件都不满足时,自动省略-->
    <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>

image-20221127155959970

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

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

至此,我们已经完成了与 XML 配置及映射文件相关的讨论。下一章将详细探讨 Java API,以便你能充分利用已经创建的映射配置。

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

标签:11,title,author,blog,SQL,动态,where,id
From: https://www.cnblogs.com/zzlbk/p/16942464.html

相关文章