1.动态sql

01.if:单独使用if,后面必须有where 1=1

代码:
<!-- 需要注意的事项:
01. 在xml文件中 特殊字符的使用
&&必须换成 and或者 &amp;
< &lt;
> &gt;
<= &lt;=
>= &gt;=
' &apos;
" &quot;
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的更多相关文章

  1. 《30天自制操作系统》笔记(03)——使用Vmware

    <30天自制操作系统>笔记(03)——使用Vmware 进度回顾 在上一篇,实现了用IPL加载OS程序到内存,然后JMP到OS程序这一功能:并且总结出下一步的OS开发结构.但是遇到了真机测 ...

  2. Mybatis笔记二:接口式编程

    目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...

  3. JS自学笔记03

    JS自学笔记03 1.函数练习: 如果函数所需参数为数组,在声明和定义时按照普通变量名书写参数列表,在编写函数体内容时体现其为一个数组即可,再传参时可以直接将具体的数组传进去 即 var max=ge ...

  4. 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试

    机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...

  5. CS229 笔记03

    CS229 笔记03 局部加权线性回归 Non-Parametric Learning Algorithm (非参数学习方法) Number of parameters grows with the ...

  6. OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓

    本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...

  7. OpenCV 学习笔记03 findContours函数

    opencv-python   4.0.1 1 函数释义 词义:发现轮廓! 从二进制图像中查找轮廓(Finds contours in a binary image):轮廓是形状分析和物体检测和识别的 ...

  8. C++ GUI Qt4学习笔记03

    C++ GUI Qt4学习笔记03   qtc++spreadsheet文档工具resources 本章介绍创建Spreadsheet应用程序的主窗口 1.子类化QMainWindow 通过子类化QM ...

  9. MyBatis笔记二:配置

    MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...

随机推荐

  1. ASP.NET根据当前时间获取,本周,本月,本季度等时间段

    DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek. ...

  2. Ubuntu忘记root密码怎么办?

    http://www.linuxidc.com/Linux/2016-05/131256.htm

  3. 关于在windows10中的vmware9.0里面安装的ubuntukylin15.04和windows共享目录的一些反思

    关于在windows10中的vmware9.0里面安装的ubuntukylin15.04和windows共享目录的一些反思 一.遇到的问题      如题目所说,在windows的虚拟机中和windo ...

  4. Google 浏览器被劫持怎么办?

    chrome://version/ 输入以上语句,在命令行中可看到恶意网址,复制该命令行,修改后面的网址即可

  5. HashMap并发导致死循环 CurrentHashMap

    为何出现死循环简要说明 HashMap闭环的详细原因 cocurrentHashMap的底层机制 为何出现死循环简要说明 HashMap是非线程安全的,在并发场景中如果不保持足够的同步,就有可能在执行 ...

  6. java异常处理、多态

    第一:GC是什么? 为什么要有GC?  第二:垃圾回收的优点和原理.并考虑2种回收机制.  第三:垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?  第 ...

  7. 转 Caffe学习系列(4):激活层(Activiation Layers)及参数

    在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...

  8. 1_类的定义(Defining Class)

    C++ 提供了一种类class机制,让程序员可以定义真正意义上的数据类型.即不但可以定义数据的复合,还可以定义该复合数据的操作,以便让本应由使用该数据类型的程序员做得工作分出来,让定义类型的程序员去做 ...

  9. 【前端】Angular2 Ionic2 学习记录

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/angular2_ionic2.html 一.建立项目 ionic start ProductName super ...

  10. VS2010 C++学习(5):基于DirectShow的视频预览录像程序

    VS2010 C++学习(5):基于DirectShow的视频 预览录像程序 学习VC++编制的基于DirectShow视频捕获程序,主要练习基于DirectShow程序的应用. 一.         ...