MyBatis笔记03
1.动态sql
01.if:单独使用if,后面必须有where 1=1
代码:
<!-- 需要注意的事项:
01. 在xml文件中 特殊字符的使用
&&必须换成 and或者 &
< <
> >
<= <=
>= >=
' '
" "
02.因为不确定用户输入的到底是哪个参数
所以 where 之后必须加上 1=1 而且 每个条件之前加上 and
--> <select id="selectStudentsByIf" resultType="Student">
select id,name,age from student where 1=1
<if test="name!=null and name!='' ">
and name like concat('%',#{name},'%')
</if> <if test="age>0">
and age>#{age}
</if>
</select>
02.where:上面的代码有点问题,就是在xml文件中的sql语句有where 1=1,如果查询条件多的话,性能是很低的,因为每次查询都需要判断一次!这时候 我们就需要使用 where 标签来代替!
代码:
<!--动态sql where -->
<select id="selectStudentsByWhere" resultType="Student">
select id,name,age from student
<where>
<!-- and 必须要加上mybatis只会减 不会加 -->
<if test="name!=null and name!='' ">
and name like concat('%',#{name},'%')
</if> <if test="age>0">
and age>#{age}
</if>
</where>
</select>
03.choose:比如说当姓名不为空的时候,按照姓名来查询,年龄不为空的时候按照年龄来查询!如果都为空则返回空!
代码:
<!--动态sql choose
姓名不空 按照姓名查询 年龄不为空 按照年龄查询
只要满足一个when 则其他的when则不会执行!
如果都不满足,则会执行otherwise 也就是没有查询结果
-->
<select id="selectStudentsByChoose" resultType="Student">
select id,name,age from student
<where>
<choose> <when test="age>0 ">
and age>#{age}
</when> <when test="name!=null and name!='' ">
and name like concat('%',#{name},'%')
</when> <otherwise> </otherwise>
</choose>
</where>
</select>
04:foreach标签 遍历数组
代码:
<select id="selectStudentsByForeachArray" resultType="Student">
<!-- 这就不是动态查询了 而是把参数写成固定的了
select id,name,age from student where id in(1,13,15)
-->
select id,name,age from student
<if test="array.length>0">
where id in
<foreach collection="array" item="myId" open="(" separator="," close=")">
#{myId}
</foreach>
</if>
</select>
05:foreach标签 遍历list集合
代码:
<select id="selectStudentsByForeachList" resultType="Student">
select id,name,age from student
<if test="list.size()>0 ">
where id IN
<foreach collection="list" item="myId" open="(" separator="," close=")">
#{myId}
</foreach>
</if>
</select>
06:foreach标签 遍历自定义类型集合
代码:
<select id="selectStudentsByForeachStudent" resultType="Student">
select id,name,age from student
<if test="list.size()>0 ">
where id IN
<foreach collection="list" item="stu" open="(" separator="," close=")">
#{stu.id}
</foreach>
</if>
</select>
07:sql片段 如果一个xml文件中的sql语句有很多相同的地方,则可以使用sql片段来替换!
代码:
<!-- sql片段的使用 -->
<select id="selectStudentsBySql" resultType="Student">
<!-- 引入sql片段-->
<include refid="selectStudent"/>
<if test="list.size()>0" >
where id IN
<foreach collection="list" item="stu" open="(" separator="," close=")">
#{stu.id}
</foreach>
</if> </select>
<!-- 如果有需求不查询age了,之前需要在所有的查询中删除age字段,
现在只需要在sql片段中删除即可! -->
<sql id="selectStudent">
select id,name,age from student
</sql>
MyBatis笔记03的更多相关文章
- 《30天自制操作系统》笔记(03)——使用Vmware
<30天自制操作系统>笔记(03)——使用Vmware 进度回顾 在上一篇,实现了用IPL加载OS程序到内存,然后JMP到OS程序这一功能:并且总结出下一步的OS开发结构.但是遇到了真机测 ...
- Mybatis笔记二:接口式编程
目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...
- JS自学笔记03
JS自学笔记03 1.函数练习: 如果函数所需参数为数组,在声明和定义时按照普通变量名书写参数列表,在编写函数体内容时体现其为一个数组即可,再传参时可以直接将具体的数组传进去 即 var max=ge ...
- 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试
机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...
- CS229 笔记03
CS229 笔记03 局部加权线性回归 Non-Parametric Learning Algorithm (非参数学习方法) Number of parameters grows with the ...
- OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓
本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...
- OpenCV 学习笔记03 findContours函数
opencv-python 4.0.1 1 函数释义 词义:发现轮廓! 从二进制图像中查找轮廓(Finds contours in a binary image):轮廓是形状分析和物体检测和识别的 ...
- C++ GUI Qt4学习笔记03
C++ GUI Qt4学习笔记03 qtc++spreadsheet文档工具resources 本章介绍创建Spreadsheet应用程序的主窗口 1.子类化QMainWindow 通过子类化QM ...
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
随机推荐
- JavaScript那些事
1.定义静态常量: const C=1; 该常量不能变化的. 2.在if判断中如果是两个变量比较js会将一个字符和一个数字比较的话,会将字符转换成数字然后在对这两个进行对比: var num= ...
- Mysql数据库建立索引的优缺点有哪些?
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息. 什么是索引 数据库索引好比是一本书前面的目录,能加快数据库的查询速度. 例如这样一个查询:select * ...
- python_crawler,批量下载文件
这个第一个python3网络爬虫,参考书籍是<python网络数据采集>.该爬虫的主要功能是爬取某个网站,并将.rar,.doc,.docx,.zip文件批量下载. 后期将要改进的是,用后 ...
- Springdata mongodb 版本兼容 引起 Error [The 'cursor' option is required, except for aggregate with the explain argument
在Spring data mongodb 中使用聚合抛出异常 mongodb版本 为 3.6 org.springframework.dao.InvalidDataAccessApiUsageExce ...
- JVM笔记5-对象的访问定位。
java虚拟机中指定一个栈内存的引用指向了堆内存中的对象.这样说只是笼统的说法.而指向堆内存中的对象就一定是栈引用所需要的那个对象吗?其实并不定. 这就需要知道对象的访问定位方式有两种: 1.使用句柄 ...
- 用Node.JS+MongoDB搭建个人博客(页面模板)(五)(结束)
<差不多先生> 我是差不多先生,我的差不多是天生.也代表我很天真,也代表我是个闲人.这差不多的人生,总是见缝插针. 求学的道路上总是孤独的,即使别人不理解我,认为我是奇葩!但没关系,我会坚 ...
- 如何更改Ubuntu的root密码
安装Ubuntu系统时,只提示了设定用户密码,该密码可用于普通用户暂时获取root的权限,执行一些需要root权限的操作,而没有要求我们设置root密码,在需要用到root密码时,却想不起来,很尴尬啊 ...
- raw文件系统
简介 RAW文件系统是一个没有被NT文件系统(FAT或NTFS)格式化的磁盘分区(win8默认用的是RAW文件系统). 2定义 分区显示RAW文件系统的直接原因,是由于文件系统不被操作系统识别.此时在 ...
- DML 触发器2
2.行级触发器的关联标识符 :new,:old >>1. 一般通过:new.filed 引用(filed是trigger_table的字段名) :new :old中filed字段的意义 触 ...
- Linux显示用户注册名
Linux显示用户注册名 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ finger -s Login Name Tty Idle Login Time Of ...