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 ...
随机推荐
- Windows下nexus-3.*搭建Maven私服
1.下载 从官网下载https://help.sonatype.com/display/NXRM3/Download 选择Windows archive https://download.sonaty ...
- 字典树trie
字典树经常用于单词搜索,现在网络引擎中也应用了trie树: public class Trie{ private int SIZE = 26; private TrieNode root; Trie( ...
- the c programing language 学习过程3
ControlFlow 控制流 specify 指定 compound statement 复合语句 cryptic有隐含意义的 ambiguity歧义 robust稳健 disintegratio ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- hdu1556 Color the ball 线段树区间染色问题
都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...
- linux命令学习汇总
1. touch 命令 touch -简单来说touch 命令可以用来建立文件 文件不存在的情况下,也可以更改创建时间等等 2. pwd 查看目录---一般看目录用pwd 即可但是pwd -P 会不以 ...
- Spring MVC处理(下周完善)
http://www.cnblogs.com/xiepeixing/p/4244574.html http://blog.csdn.net/kobejayandy/article/details/12 ...
- Java中equal和==区别及String创建过程
Java中equal和==区别 1.起因 在一段Java代码中,使用了两种实现方式. //第一种命令行输入 int main (String[] args) { if(args[0] == " ...
- python 常见错误和异常 函数 正则表达式及多线程编程
生成随机密码#!/usr/bin/env python import stringfrom random import choice def gen_pass(num=9): all_chs = st ...
- freemarker.template.TemplateException:Error parsing including template
1.错误描述 freemarker.template.TemplateException:Error parsing including template ftl/main.ftl:on line 6 ...