当前位置: 首页 > article >正文

Spring Mybatis 动态语句 总结

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.kler.cn/news/314562.html

相关文章:

  • 简单生活的快乐
  • (k8s)kubernetes集群基于Containerd部署
  • Flask-SQLAlchemy一对多 一对一 多对多关联
  • GDPU Andriod移动应用 Activity
  • 【数据结构与算法】LeetCode:哈希表
  • Alinx MPSoC驱动开发第17章I2C实验修改设备树后petalinux编译报错
  • 分布式Id生成策略-美团Leaf
  • 使用python对图像批量水平变换和垂直变换
  • 深度学习参数管理
  • MySQL-DDL/DML(数据定义/操作语言)
  • GIS开发之如何使用OpenLayers,Leaflet,Mapbox,Cesium,ArcGIS, Turf.js 与 D3.js
  • 【Webpack--00802】配置Babel语法兼容
  • 【图像检索】基于Gabor特征的图像检索,matlab实现
  • Python面试宝典第50题:分割等和子集
  • Vscode、插件历史版本下载
  • [数据结构与算法·C++] 笔记 1.4 算法复杂性分析
  • [附源码]SpringBoot+VUE+Java实现人脸识别系统
  • 实战指南:深度剖析Servlet+JSP+JDBC技术栈下的用户CRUD操作
  • 探秘 Web Bluetooth API:连接蓝牙设备的新利器
  • 828华为云征文|Flexus X实例GitLab部署构建流水线-私人一体化代码仓库~
  • AWS账号可以共用吗?
  • vue 中互相不关联的两个组件怎么进行通信(数据传输)
  • MFC获取网页的html文本
  • 视频V4改进
  • 锐捷 睿易路由器存在RCE漏洞
  • 会声会影2025视频剪辑教学
  • 开源集成开发环境搭建之VSCode安装部署教程
  • MySQL:基本查询操作
  • java计算机毕设课设—土地档案管理系统(附源码、文章、相关截图、部署视频)
  • 基于Java的SSM(Spring、Spring MVC、MyBatis)框架构建的远程诊断系统