首页 > 数据库 >MyBatis 动态SQL标签汇总

MyBatis 动态SQL标签汇总

时间:2023-03-07 12:31:58浏览次数:29  
标签:name 标签 age emp SQL MyBatis id select

if 标签

<select id="getEmpByCondition" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    select * from emp
    where 1 = 1
    <if test="name != '' and name != null">
        and name = #{name}
    </if>
    <if test="age != '' and age != null">
        and age = #{age}
    </if>
</select>

where 标签

<select id="getEmpByCondition" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    select * from emp
    <where>
        <if test="name != '' and name != null">
            name = #{name}
        </if>
        <if test="age != '' and age != null">
            and age = #{age}
        </if>
    </where>
</select>

trim 标签

<select id="getEmpByCondition" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    select * from emp
    <trim prefix="where" suffixOverrides="and" prefixOverrides="and" suffix="order by id desc">
        <if test="name != '' and name != null">
            name = #{name}
        </if>
        <if test="age != '' and age != null">
            and age = #{age}
        </if>
    </trim>
</select>

choose、when、otherwise 标签

<select id="getEmpByChoose" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    select * from emp
    <where>
        <choose>
            <when test="name != '' and name != null and age != '' and age != null">
                name = #{name} and age = #{age}
            </when>
            <when test="age != '' and age != null">
                age = #{age}
            </when>
            <when test="name != '' and name != null">
                name = #{name}
            </when>
            <otherwise>
                1 = 1
            </otherwise>
        </choose>
    </where>
</select>

foreach 标签

<select id="getEmpByIds" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    select * from emp
    <foreach collection="ids" item="id" open="where id in (" close=")" separator=",">
        #{id}
    </foreach>
</select>

sql、include 标签

<sql id="base">
    select id, name, age
    from emp
</sql>

<select id="getEmpByIds" resultType="com.xy.mybatis.dynamic.pojo.Emp">
    <include refid="base"/>
    <foreach collection="ids" item="id" open="where id in (" close=")" separator=",">
        #{id}
    </foreach>
</select>

标签:name,标签,age,emp,SQL,MyBatis,id,select
From: https://blog.51cto.com/learningfish/6105771

相关文章

  • sql 时间函数
    计算时间间隔daydatediff(大日期,小日期)SELECTdatediff('2009-07-31','2009-07-30')month,year,secondtimestampdiff(month,小日期,大日期)SELECTtimestam......
  • mybatis-plus(mp)常用概念以及demo实操
    概念:作为国内流行的持久层框架,mp是mybatis的拓展,并不改变mybaits的底层,因此使用mybatis的项目可以无缝使用mp进行迭代本博客旨在温习mp常用的使用场景、使用方式(一)demo框......
  • 老杜带你从零入门MyBatis,学MyBatis看这篇就够了!
    MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。iBATIS一词来源于......
  • Mybatis框架的mapper接口中的方法名可以重载吗
    关于mybatis框架的mapper接口中的方法名是否可以重载答案是不可以重载为什么是不可以重载?这个就要从Mybatis框架中mapper接口的工作原理说起Mybatis中mapper接口的工作......
  • MybatisPlus多表连接查询
    一、(一)背景内容软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题。时至......
  • 使用反射简化批量保存sql语句
    写批量保存的时候遇到实体类字段比较多的时候写起来非常的头疼,所以我想能不能通过程序自动获取拼接思路:1.通过反射获取实体类的所有字段2.把字段拼接为id,user_name,us......
  • SQL标签库详解例子
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@tagliburi="http://java.sun.com/jsp/jstl/sql"prefix="sql"%><%@tagli......
  • Mysql 主从复制和 GTID 复制
    1、安装主Mysql优化命令创建数据和日志存储目录1)安装Mysql​root@centos05~]#tarzxfmysql-8.0.32-el7-x86_64.tar.gz-C/usr/src/[root@centos05~]#mv/usr/src/m......
  • Sqlmap
    1.Sqlmap简介SQLMap是一个自动化的SQL注入工具,其主要功能是扫描、发现并利用给定URL的SQL注入漏洞,内置了很多绕过插件,支持的数据库是MySQL、Oracle、PostgreSQL、Micr......
  • MySQL 安装过程中踩过的坑
    1、用 grep'temporarypassword'/var/log/mysqld.log生成的初始密码老提示密码错误,只能直接发大招:       A、vi/etc/my.cnf在文件的[mysqld]内增加一行 ......