Mybatis 动态sql环境搭建(学习笔记16)

本文介绍了MyBatis3如何利用OGNL表达式简化动态SQL的编写,包括ifchoose、trim、where优化、foreach遍历以及SQL片段的使用,如通过UUID生成随机ID和处理数据库字段映射问题。
摘要由CSDN通过智能技术生成

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

借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

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

首先还是搭建基础框架   忘了就去看前面笔记

解决数据库名和字段名不一致

在官网有一个这样的配置文件,开启后将数据库列名从abc_name  变成abcName这样的驼峰命名

通过UUID 生成随机id

package com.li.utils;

import org.junit.Test;

import java.util.UUID;

public class IDUtils {
    public static String getId(){
        return UUID.randomUUID().toString().replace("-","");
    }

    @Test
 public void test1(){
        System.out.println(IDUtils.getId());
    }

}

添加完数据准备开始测试

if标签:表示判断

<select id="queryBlogIF" parameterType="map" resultType="blog">
    select * from mybatis1.blog where 1 = 1
-- if标签中的test属性后面判断条件
                                  
    <if test="title != null">
        and title = #{title}
    </if>

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

</select>

where标签优化sql语句的后续拼接,会自动帮我们判断是否需要加上活去掉and / or

<select id="queryBlogIF" parameterType="map" resultType="blog">
    select * from mybatis1.blog
-- if标签中的test属性后面判断条件
--这里加上了where标签包含if标签
<where >
    <if test="title != null">
        and title = #{title}
    </if>

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

shoose,when,otherwise标签和Java的switch差不多

    <select id="queryBlogChoose" parameterType="map" resultType="blog">
        select *
        from mybatis1.blog
        <where><!--相当于switch-->
            <choose><!--相当于case-->
                <when test="author != null">
                    and author = #{author}
                </when>
                <when test="title != null">
                    and title = #{title}
                </when>
                <otherwise><!--相当于default-->
                    and views = #{views}
                </otherwise>
            </choose>
        </where>
    </select>

用update跟新数据是会遇到衔接where时会出现问题这时我们就要用到set标签

trim标签可以定制where

所谓的动态sql,本质还是sql语句,只是我们可以在sql前面,去执行一些逻辑代码

sql标签,又叫sql片段和include连用有点像java中的方法把错付的代码复用

<sql id="title_"><!--提取重复代码 id接名字-->
    -- if标签中的test属性后面判断条件
    <where >
        <if test="title != null">
            and title = #{title}
        </if>

        <if test="author != null" >
            and author = #{author}
        </if>
    </where>
</sql>
<select id="queryBlogIF" parameterType="map" resultType="blog">
    select * from mybatis1.blog
    <!--引用上面提取的代码,refid接上面sql标签id的名字-->
    <include refid="title_"></include>

</select>

foreach标签 思路比较绕多练习就会理解,忘了就去看官方文档

<!--下面的代码相当于sql语句select * from mybatis1.blog where 1=1 and(id=1 or id = 2 or id =3)-->
<!--foreach标签中collection表示需要遍历的集合,item表示集合遍历出的元素,还有一个index表示集合的下标
open 表示从什么地方开始拼接 separator表示拼接的分隔符是什么 close表示从什么时候结束拼接
-->
    <select id="queryBlogForeach" parameterType="map" resultType="blog">
        select *
        from mybatis1.blog
        <where>
            <foreach item="id" collection="ids"
            open="and (" separator="or" close=")">
        id = #{id}
            </foreach>
        </where>
    </select>

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

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一年找不到工作就去厂里大螺丝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值