Mybatis的动态SQL的语句
例子、
<?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 && !name.equals("")">
teacherName like #{name} and
</if>
<if test="birth!=null">
birth_date < #{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("")">
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的语句的更多相关文章
- mybatis 的动态sql语句是基于OGNL表达式的。
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- MyBatis中动态SQL语句完成多条件查询
一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型, 此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...
- MyBatis 构造动态 SQL 语句
以前看过一个本书叫<深入浅出 MFC >,台湾 C++ 大师写的一本书.在该书中写到这样一句话,“勿在浮沙筑高台”,这句话写的的确对啊.编程很多语言虽然相通,但是真正做还是需要认真的学习, ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
随机推荐
- IDEA 2020.3.2 安装激活教程
注意 本教程适用于 IntelliJ IDEA 2020.3.2 以下所有版本,请放心食用~ 本教程适用于 JetBrains 全系列产品,包括 Pycharm.IDEA.WebStorm.Phpst ...
- linux mint安装Scala
Scala由java编写,需要前期安装jdk 面向函数式编程 1.下载 Scala 二进制包2.11.8 http://www.scala-lang.org/downloads 解压到/usr/loc ...
- Flink基础Source配置
一.pom文件 https://www.cnblogs.com/robots2/p/16048648.html 二.代码demo FlinkBaseSource.java package net.xd ...
- FLink写入Clickhouse优化
一.背景 ck因为有合并文件操作,适合批量写入.如单条插入则速度太慢 二.Flink写入ck优化 改为分批插入,代码如下 DataStream<Row> stream = ... stre ...
- autMan奥特曼机器人-内置微信如何定时给公众号发消息
autMan版本要求2.1.3以上 一.打开左侧栏的本地开发,然后从实时日志获取公众号的ID或名称 ![2024-10-23T01:45:34.png][1] ![2024-10-23T01:44:5 ...
- ReviOS - 专为游戏优化的 Win11 / Win10 精简版系统
ReviOS介绍 ReviOS 渴望重新创建作为操作系统的 Windows 应该是 - 简单.ReviOS 是一款功能强大.高效的私有操作系统,适用于游戏玩家.高级用户和发烧友.由于资源.占用内存和存 ...
- Flink - [08] 状态一致性
题记部分 一.什么是状态一致性 有状态的流处理,内部每个算子任务都可以有自己的状态.对于流处理器内部来说,所谓的状态一致性,其实就是我们所说的计算结果要保证准确.一条数据也不应该丢失,也不应该重复 ...
- win - [01] 修改网络连接名称(网络1、网络2...网络10)
修改网络连接的名称 1.打开运行窗口:Windows 键 + R 2.在运行窗口输入 regedit,打开注册表编辑器 3.在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsof ...
- Hadoop - [04] 分布式部署
Zookeeper的分布式部署 >> Hadoop的分布式部署 一.集群规划 主机名 node01 node02 node03 JDK ○ ○ ○ Zookeeper ○ ○ ○ Name ...
- C#中固定编译时不确定数量的变量(相关话题fixed固定多个数组)
以交错数组byte[][]为例. fixed无法固定byte[][],只能在编译时固定确定数量的变量. 交错数组byte[][]中的每一个byte[]可以采用GCHandle进行固定. int n = ...