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必不 ...
随机推荐
- Codeforces Round 961 (Div. 2)
题目链接:Codeforces Round 961 (Div. 2) 总结:B1wa两发可惜,C出得有点小慢. A. Diagonals fag:贪心 Description:给定一个\(n * n\ ...
- 使用Vant做移动端对图片预览ImagePreview和List的理解
使用Vant3做移动端的感受 最近在使用Vant3做移动端. 感觉还可以,使用起来也简单,但是也遇见一些坑. 图片预览ImagePreview的使用 在使用图片预览的时候, 我们在main.js中进行 ...
- 【忍者算法】从十字路口相遇到链表交点:探索相交链表问题|LeetCode第160题 相交链表
从十字路口相遇到链表交点:探索相交链表问题 生活中的相遇问题 想象两个人从不同的地方出发,最后在一个十字路口相遇.他们可能走过不同长度的路程,但最终会在同一个点汇合.这就很像我们今天要讨论的相交链表问 ...
- 微信分享前端开发全程详解含iOS、安卓、H5、ReactNative以及微信开放标签的适配和使用
2024年9月,本人在做微信分享前端部分的iOS.安卓和H5的页面和功能时踩了不少坑,于是写了这篇文章,内容包括微信分享在上面三个端的技术点和坑点.解决办法,微信开放标签的相关适配,以及ReactNa ...
- SAM 学习笔记
发现自己根本没有 SAM 基础,所以想补一篇学习笔记. SAM SAM 是一个可以接受字符串 \(s\) 的所有后缀的最小 \(DFA\)(确定性有限状态自动机).不过他最大的用处和后缀数组一样,都是 ...
- 松下机器人示教器AUR01062触摸无反维修
Panasonic松下机器人示教器AUR01062触摸无反应解决方案 松下机器人示教器现象:触摸屏幕时鼠标箭头无任何动作,没有发生位置改变. 原因:造成此现象产生的原因很多,下面就松下机器人维修中示教 ...
- 工业机器人维修保养|ABB机器人IRB 6700维修保养技巧
通过机器人维修保养服务定制合理的维修保养工作,可以确保ABB机器人IRB 6700的持续稳定运行,延长其使用寿命,为企业的生产提供有力保障. 一.ABB机器人IRB 6700日常检查与维护 外观检查: ...
- QT5笔记: 30. 二进制文件读写
Qt 预定义类型文件 *.stm 标准二进制文件 *.dat 例子: MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include & ...
- Go红队开发—并发编程
目录 并发编程 go协程 chan通道 无缓冲通道 有缓冲通道 创建⽆缓冲和缓冲通道 等协程 sync.WaitGroup同步 Runtime包 Gosched() Goexit() 区别 同步变量 ...
- Scala高阶函数 1
package com.wyh.day01 /** * * 高阶函数 */ object ScalaFun3 { def main(args: Array[String]): Unit = { //定 ...