例子、

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.dao.TeacherDao">
<resultMap type="com.atguigu.bean.Teacher" id="teacherMap">
<id property="id" column="id" />
<result property="address" column="address" />
<result property="birth" column="birth_date" />
<result property="course" column="class_name" />
<result property="name" column="teacherName" />
</resultMap> <!--public Teacher getTeacherById(Integer id); -->
<select id="getTeacherById" resultMap="teacherMap">
select * from t_teacher
where id=#{id}
</select> <!-- if:判断 -->
<!--public List<Teacher> getTeacherByCondition(Teacher teacher); -->
<select id="getTeacherByCondition" resultMap="teacherMap">
select * from t_teacher
<!-- test="":编写判断条件 id!=null:取出传入的javaBean属性中的id的值,判断其是否为空 -->
<!-- where可以帮我们去除掉前面的and; --> <!-- trim:截取字符串
prefix="":前缀;为我们下面的sql整体添加一个前缀
prefixOverrides="": 取出整体字符串前面多余的字符
suffix="":为整体添加一个后缀
suffixOverrides="":后面哪个多了可以去掉; -->
<!-- 我们的查询条件就放在where标签中;每个and写在前面,
where帮我们自动取出前面多余的and -->
<trim prefix="where" prefixOverrides="and" suffixOverrides="and">
<if test="id!=null">
id > #{id} and
</if>
<!-- 空串 "" and; && or: ||; if():传入非常强大的判断条件;
OGNL表达式;对象导航图
Person
===lastName
===email
===Address
===city
===province
===Street
===adminName
===info
===perCount
方法、静态方法、构造器。xxx
在mybatis中,传入的参数可以用来做判断;
额外还有两个东西;
_parameter:代表传入来的参数;
1)、传入了单个参数:_parameter就代表这个参数
2)、传入了多个参数:_parameter就代表多个参数集合起来的map
_databaseId:代表当前环境
如果配置了databaseIdProvider:_databaseId就有值 -->
<if test="name!=null &amp;&amp; !name.equals(&quot;&quot;)">
teacherName like #{name} and
</if>
<if test="birth!=null">
birth_date &lt; #{birth} and
</if>
</trim>
</select> <!-- public List<Teacher> getTeacherByIdIn(List<Integer> ids); -->
<select id="getTeacherByIdIn" resultMap="teacherMap"> SELECT * FROM t_teacher WHERE id IN
<!-- 帮我们遍历集合的; collection="":指定要遍历的集合的key
close="":以什么结束
index="i":索引;
如果遍历的是一个list;
index:指定的变量保存了当前索引
item:保存当前遍历的元素的值
如果遍历的是一个map:
index:指定的变量就是保存了当前遍历的元素的key
item:就是保存当前遍历的元素的值
item="变量名":每次遍历出的元素起一个变量名方便引用
open="":以什么开始
separator="":每次遍历的元素的分隔符
(#{id_item},#{id_item},#{id_item} -->
<if test="ids.size >0">
<foreach collection="ids" item="id_item" separator="," open="("
close=")">
#{id_item}
</foreach>
</if>
</select> <!--public List<Teacher> getTeacherByConditionChoose(Teacher teacher); -->
<select id="getTeacherByConditionChoose" resultMap="teacherMap">
select * from t_teacher
<where>
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="name!=null and !name.equals(&quot;&quot;)">
teacherName=#{name}
</when>
<when test="birth!=null">
birth_date = #{birth}
</when>
<otherwise>
1=1
</otherwise>
</choose> 实例、 <mapper namespace="com.apcstudy.mybatis.dao.TeacherDao"> <resultMap id="teacherMap" type="com.apcstudy.mybatis.bean.Teacher">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="course" column="course" />
<result property="address" column="address" />
<result property="birth" column="birth" />
</resultMap> <select id="getTeacherById" parameterType="integer" resultMap="teacherMap">
select * from t_teacher
where id=#{id}
</select> <select id="getTeacherByCondition01" resultMap="teacherMap">
select * from t_teacher
where 1=1
<if test="name!=null and name!='' ">
and name=#{name}
</if>
<if test="course!=null and course!='' ">
and course=#{course}
</if>
</select> <select id="getTeacherByCondition02" resultMap="teacherMap">
select * from t_teacher
<where>
<if test="name!=null and name!='' ">
name=#{name}
</if>
<if test="course!=null and course!='' ">
and course=#{course}
</if>
</where>
</select> <select id="getTeacherByCondition03" resultMap="teacherMap">
select * from t_teacher
<trim prefix="where" prefixOverrides="and" suffixOverrides="and">
<if test="name!=null and name!='' ">
name=#{name} and
</if>
<if test="course!=null and course!='' ">
course=#{course} and
</if>
</trim>
</select> <update id="updateTeacher">
update t_teacher
<set>
<if test="name!=null and name!='' ">
name=#{name},
</if>
<if test="course!=null and course!='' ">
course=#{course},
</if>
<if test="address!=null and address!='' ">
address=#{address},
</if>
<if test="birth!=null and birth!='' ">
birth=#{birth}
</if>
</set>
<where>
id=#{id}
</where>
</update> <select id="getTeacherByIdIn" resultMap="teacherMap">
select * from t_teacher where id in
<if test="ids!=null">
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</select> <select id="getTeacherByConditionChoose" resultMap="teacherMap">
select * from t_teacher
<where>
<choose>
<when test="name!=null and name!='' ">
name=#{name}
</when>
<when test="course!=null and course!=''">
course=#{course}
</when>
<when test="address!=null and address!=''">
address=#{address}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>

Mybatis的动态SQL的语句的更多相关文章

  1. mybatis 的动态sql语句是基于OGNL表达式的。

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  2. MyBatis中动态SQL语句完成多条件查询

    一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...

  3. Mybatis中动态SQL语句中的parameterType不同数据类型的用法

    Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型,    此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...

  4. MyBatis 构造动态 SQL 语句

    以前看过一个本书叫<深入浅出 MFC >,台湾 C++ 大师写的一本书.在该书中写到这样一句话,“勿在浮沙筑高台”,这句话写的的确对啊.编程很多语言虽然相通,但是真正做还是需要认真的学习, ...

  5. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  6. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  7. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  8. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  9. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  10. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

随机推荐

  1. matlib:图像旋转-缩放

    需求 使用MATLAB尝试完成一个自定义的图像攻击软件,功能描述: 1)根据输入参数,完成旋转功能 2)根据输入参数,完成缩放功能 开始 旋转 参数:参数为正,顺时针旋转:参数为负,逆时针旋转 主要代 ...

  2. Sorcerer pg walkthrough Intermediate

    nmap ┌──(root㉿kali)-[~/lab] └─# nmap -p- -A 192.168.192.100 Starting Nmap 7.94SVN ( https://nmap.org ...

  3. 记录在本地电脑部署自己的DeepSeek 大模型AI

    大家新年好呀,年初二给各位拜年啦,祝各位新年身体健康,工作顺利,万事如意,开心快乐每一天! 前言: 这几天看到很多DeepSeek的热点新闻(火的不行呀),去了解下这个AI,然后自己试了下本地部署,发 ...

  4. Ubuntu Linux部署DeepSeek

    技术背景 DeepSeek是这段时间最热门的话题之一,其蒸馏模型可以实现低成本而高质量的推理,使得我们现在可以在本地小型化的硬件上也用上大模型这一AI利器.本文主要介绍通过Ollama来部署DeepS ...

  5. Presto-JDBC使用

    一.简介 PrestoConnection并不能提供一个持久的Socket连接,而是创建一个OkHttpClient与Presto按照HTTP1.1协议进行通信,并且PrestoConnection仅 ...

  6. Ubuntu 部署饥荒联机版服务器 Linux DST_Dedicate_Server

    0. 文件夹 - ~ |- ~/steamcmd # 装的是steamcmd_linux.tar.gz以及其解压出来的东西 |- ~/DST # 装的是DST服务器可执行文件.世界存档.世界模板 |- ...

  7. axios 发送 form-data 请求和 x-www-form-urlencoded请求以及相关问题

    问题 not supported { "msg": "Content type 'multipart/form-data;boundary=--------------- ...

  8. MySQL - [04] 分布式部署&主从复制&读写分离

    一.前言 Q1:为什么需要主从复制? 1.在业务复杂的系统中,有一条SQL语句需要锁表,导致暂时不能使用读的服务,那么就很影响运行中的业务.使用主从复制,让主库负责写,从库负责读,这样即使主库出现了锁 ...

  9. vue+elementUI 表格操作按钮添加loading

    前言 表格操作栏,某个操作需要异步请求才能做跳转等 方案 整个列表每行都加一个loading字段,不够优雅 利用$set方法 改变当前行当前按钮loading,可行(代码如下) //按钮 row.lo ...

  10. go、thinkphp8、webman数据读取并发测试、性能测试

    前期准备:本地搭建程序运行所需环境,分别编写go,thinkphp8和webman程序,确保程序运行正常,新建mysql的student表,模拟存储学生信息,共计3646条数据,分别使用go语言.th ...