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必不 ...
随机推荐
- MySQL---锁、变量、存储过程、游标、自定义函数
一概述 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制. MySQL各存储引擎使用了三种类型 ...
- idea+maven打包.jar发布项目
开发完项目后,idea+maven环境打包成.jar包,才能发布项目.下面记录常用的几种打包方式. 一,通过mvn命令打包 比较专业的用法是通过mvn命令打包: mvn clean package - ...
- 2025高级java面试精华及复习方向总结
1. Java基础 顶顶顶顶的点点滴滴 1.1 java集合关系结构图 1.2 如何保证ArrayList的线程安全 方法一: 使用 Collections 工具类中的 synchronizedLis ...
- C:条件编译
问题 #ifdef HELIB_DEBUG long pa, pb; std::vector<long> slots; decryptBinaryNums(slots, a, *dbgKe ...
- Kotlin:【继承】:继承、函数重载、类型检测、as类型转换、智能类型转换、Kotlin层次
- MySQL分页性能思考
MySQL分页性能思考 关键词:深度分页 背景 最近有一个需求:在后台管理页面中,需要展示产品信息的列表. 之前版本开发中产品信息是用户填写完所有字段之后能进行保存.在之前的基础上需要支持用户不完全填 ...
- maven项目中解决 .git文件太大问题
一.git项目上传后项目文件太大,git filter-branch手动清理 最近发现一个项目git目录已经达到200MB,严重影响了clone代码.操作之前请全量备份代码,操作失误可恢复 二.测试项 ...
- [ZJOI2019] 语言 题解
不愧是 \(ZJOI\),<最可做的一道题>都让人一头雾水-- 首先将问题转化到链上. 可以将总共的组数转化为每个点可以到达的城市. 明显给每个点建一棵动态开点线段树,维护可以和他通商的点 ...
- 基于stm32+esp8266通过阿里云物联网平台和MQTT实现智慧粮仓环境监测管理系统
基于STM32+ESP8266通过阿里云物联网平台和MQTT实现智慧粮仓环境监测管理系统 技术要点:STM32f407.ESP8266.阿里云物联网平台IOT.MQTT.JSON数据解析. 1.功能与 ...
- AI图像翻译
在当今互联互通的世界中,快速准确地翻译图像中的文本的能力非常宝贵.无论您是商务人士.教育工作者.设计师还是内容创作者,Visual Paradigm Online 的 AI 图像翻译器都能提供强大的解 ...