Mybatis-动态SQL

12、动态SQL

什么是动态SQL:动态SQL就是根据不同条件生成不同的SQL语句

这一特性可以彻底摆脱这种痛苦

动态SQL元素和JSTL或基于类似的XML文本处理器相似。在Mybatis之前的版本中,有很多元素需要花时间了解。Mybatis 3大大精简了元素种类,现在只需要学习原来一半的元素便可。MyBatis采用功能强大的基于OGNL的表达式来淘汰其他大部分元素。

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

动态SQL的元素

元素作用备注
if判断语句单条件分支判断
choose(when、otherwise)相当于Java中的switch和case语句多条件分支判断
trim(where、set)辅助元素,用于处理待定的SQL拼装问题,比如去掉多余的and、or元素用于处理SQL拼装的问题
foreach循环语句在in语句等列举条件常用

IF

多个符合条件时都会加入SQL

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

Choose(When,Otherwise)

只会选择第一个符合条件的sql进行查询

<select id="queryBlogChoose" parameterType="map" resultType="blog">
    select * from mybatis.blog
    <where>
        <choose>
            <when test="title!=null">
                and title = #{title}
            </when>
            <when test="author!=null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

Trim(Where,Set)

多个条件拼接sql查询

<select id="queryBlogIF" parameterType="map" resultType="blog">
    select *from mybatis.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,本质还是SQL语句,只是我们可以再SQL层面,去执行一个逻辑代码

SQL片段

有的时候,可以将一部分功能抽取出来,方便复用

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

    <sql id="if-title-author">
        <if test="title != null">
            and 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"></include>
    </where>
</select>

注意事项

  • 最好基于单表来定义SQL片段
  • 不要存在where标签
  • 一般复用if即可

Foreach

<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>

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

collection对应的就是传递进来的参数名字,可以是数组、List、Set等集合

建议:

 id = #{id}
    </foreach>
</where>
```

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

collection对应的就是传递进来的参数名字,可以是数组、List、Set等集合

建议:

  • 先在Mysql中写完整的SQL,再对应的修改成需要的动态SQL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shenyang1026

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值