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 ...
随机推荐
- Spring context:property-placeholder 一些坑
今天在配置多配置文件的时候偶然发现如果我使用 <context:property-placeholder location="classpath:filePath.properti ...
- java线程优先级
java的线程优先级分为1-10 这10个等级 1为最强,最优先 10为最弱 如果大于10或者小于1则会抛异常 源代码为: public final void setPriority(int newP ...
- ios设备触发虚拟键盘输入后position:fixed 无效的一些简单另类的解决方法。
首先看一下我要解决的问题,第一张图是正常的情况下,第二张图是点击了输入框之后的情况,就是要解决此问题~! 百度了一下解决方法,好像有以下的一些方法: 1. iscroll 2. Jquery Mobi ...
- IOS 使用cocoapods后无法导入头文件问题
IOS 使用cocoapods后无法导入头文件问题 这时候如果你发现import的时候没有提示AFN e t wo r k i n g.h的文件,可以在target-Build Settings下修改 ...
- 剑指offer 第一个只出现一次的字符 hash
思路:i表示字符的ASCII码值,cntp[i]表示字符出现的次数. AC代码 class Solution { public: int FirstNotRepeatingChar(string st ...
- UVA 1626 Brackets sequence 区间DP
题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...
- HDU - 2181 dfs [kuangbin带你飞]专题二
保存每个节点的下一个节点一直往下面走就行了,不能重复经过某个点,当经过的点达到20个而且当前节点的下一个节点是起点就打印答案. AC代码 #include<cstdio> #include ...
- Dropout
参数正则化方法 - Dropout 受人类繁衍后代时男女各一半基因进行组合产生下一代的启发,论文(paper.pdf)提出了Dropout. Dropout是一种在深度学习环境中应用的正规化手段.它是 ...
- 深度学习菜鸟的信仰地︱Supervessel超能云服务器、深度学习环境全配置
并非广告~实在是太良心了,所以费时间给他们点赞一下~ SuperVessel云平台是IBM中国研究院和中国系统与技术中心基于POWER架构和OpenStack技术共同构建的, 支持开发者远程开发的免费 ...
- Java兔子问题
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? /** * @Title:Rabbit.java ...