Spring Mybatis 动态语句 总结

news/2024/9/22 16:31:01 标签: spring, mybatis, sql

1.简介

Mybatis 提供动态语句的功能来增强多条件变动的查询语句。

2.代码

if和where搭配使用:

<select id="query" resultType="a">
    select * from t_a
    <where><!--  where内没有条件满足,不转成where,有条件满足转成where。自动去掉多余的and和or -->
	    <if test="name != null">
	    a_name = #{name}
	    </if>
	    <if test="price != null and price &gt; 100">
	    <!--  &gt;是 >  &lt;是 < -->
	    and a_prirce = #{price}
	    </if>
    </where>
</select>

set标签用于去掉多余逗号,和自动添加set关键字:

<!--    set标签用于去掉多余逗号,和自动添加set关键字-->
<update id="update">
    update t_a
    <set>
        <if test="aName != null">
             a_name = #{aName},
        </if>
        <if test="aPrice != null">
             a_price = #{aPrice}
        </if>
        where a_id = #{aId}
    </set>
</update>

trim可以自定义添加关键字,去掉指定字符:

<select id="queryTrim" resultType="a">
    select * from t_a
    <!--  自动添加where,去掉多余前缀的and和or -->
    <trim prefix="where" prefixOverrides="and|or" sufixOverrides="and|or">
	    <if test="name != null">
	    a_name = #{name}
	    </if>
	    <if test="price != null and price &gt; 100">
	    <!--  &gt;是 >  &lt;是 < -->
	    and a_prirce = #{price}
	    </if>
    </trim>
</select>

<select id="queryTrim" resultType="a">
    select * from t_a
    <!--  自动添加where,去掉多余后缀的and和or -->
    <trim prefix="where" sufixOverrides="and|or">
	    <if test="name != null">
	    a_name = #{name} and
	    </if>
	    <if test="price != null and price &gt; 100">
	    <!--  &gt;是 >  &lt;是 < -->
	    a_prirce = #{price}
	    </if>
    </trim>
</select>

choose搭配when使用实现类似switch的功能:

<select id="queryChoose" resultType="a">
    select * from t_emp
   where
    <!-- 如果name !=null 则用name查询,如果name == null 则用price查询,如果都不满足,则走otherwise分支 -->
    <choose>
        <when test="name != null">
            a_name = #{name} and
        </when>
        <when test="salary != null">
            a_price = #{salary}
        </when>
        <otherwise>1=1</otherwise>
    </choose>
</select>

foreach实现批量查询:

<select id="queryBatch" resultType="a">
    select * from t_a
     where a_id in
     <!-- 实现(id1,id2,id3...idn)--> 
     <foreach collection="ids" open="(" separator="," close=")" item="id">
         #{id}
     </foreach>     
</select>

sql标签组成片段,被其他地方引用:
<sql id="mySql">
    select * from t_a
</sql>
<select id="query" resultType="a">
    <include refid="mySql"/>
</select>

http://www.niftyadmin.cn/n/5670576.html

相关文章

【LLM学习之路】9月16日 第六天

【LLM学习之路】9月16日 第六天 损失函数 L1Loss 可以取平均也可以求和 参数解析 input &#xff08;N&#xff0c;*&#xff09; N是batchsize&#xff0c;星号代表可以是任意维度 不是输入的参数&#xff0c;只是描述数据 target 形状要同上 MSELoss平方差 CrossEntr…

C#测试调用PdfiumViewer浏览PDF文件的基本用法

印章管理项目后续准备实现打开浏览PDF文件并进行盖章的功能&#xff0c;需要在Winform中使用控件在线浏览PDF文件&#xff0c;在网上找了几个开源的PDF浏览控件进行测试&#xff0c;以便应用于印章管理项目。本文测试调用PdfiumViewer模块打开及浏览PDF文件。   PdfiumViewer…

Redis 缓存雪崩、缓存穿透、缓存击穿详解

缓存雪崩 缓存雪崩指的是大量缓存数据在同一时间失效&#xff0c;导致所有请求直接打到数据库或下游系统&#xff0c;造成数据库瞬时压力剧增&#xff0c;甚至可能引发系统崩溃。 形成原因&#xff1a; 缓存数据同时过期&#xff1a;由于缓存过期时间设置不合理&#xff0c;…

初识ZYNQ——FPGA学习笔记15

一、ZYNQ简介 ZYNQ&#xff1a;Zynq-7000 All Programmable SoC&#xff08;APSoC&#xff09;&#xff0c;赛灵思公司&#xff08;AMD Xilinx&#xff09;推出的新一代全可编程片上系统 PS&#xff1a;Processing System&#xff0c;处理系统 PL&#xff1a;Program Logic&…

react:组件通信

组件通信 父组件向子组件通信 function App() {return (<div><div>这是父组件</div><Child name"这是子组件" /></div>); }// 子组件 function Child(props) {return <div>{props.name}</div>; }props说明 props可以传…

代码随想录算法训练营第三十五天 | 01背包问题 二维,01背包问题 一维,416. 分割等和子集

三十五天打卡&#xff0c;背包问题入门&#xff0c;之前做过还是比较容易的 46.卡码网【携带研究材料】 题目链接 解题过程 采用二维bp&#xff0c;bp[i][j]的含义是&#xff0c;背包容量为j&#xff0c;只装序号为0~i的物品时能装的最大价值若背包容量大于等于当前物品的重…

python脚本转mac app+app签名公正

python脚本转mac appapp签名公正 1.python打包&#xff0c;有id的可以加上--codesign-identity&#xff0c;然后提交公正&#xff0c;可以避免给别人用的时候需要去设置中信任的问题。没有的话可以忽略&#xff0c;打完包也不用看2和3了创建完证书&#xff0c;本地双击打开一下…

本地快速部署一个简洁美观的个人Halo博客网站并发布公网远程访问

文章目录 前言1. Docker部署Halo1.1 检查Docker版本如果未安装Docker可参考已安装Docker步骤&#xff1a;1.2 在Docker中部署Halo 2. Linux安装Cpolar2.1 打开服务器防火墙2.2 安装cpolar内网穿透 3. 配置Halo个人博客公网地址4. 固定Halo公网地址 前言 本文主要介绍如何在Cen…