1.if标签

  1. public interface StudentDao {
  2. /**
  3. *动态sql的查询 参数是Student对象 不确定 用户输入几个属性值
  4. */
  5. List<Student> selectStudentsByIf(Student student);
  6. }

xml文件中的内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <!-- 需要注意的事项:
  8. 01. 在xml文件中 特殊字符的使用
  9. &&必须换成 and或者 &amp;
  10. < &lt;
  11. > &gt;
  12. <= &lt;=
  13. >= &gt;=
  14. ' &apos;
  15. " &quot;
  16.  
  17. 02.因为不确定用户输入的到底是哪个参数
  18. 所以 where 之后必须加上 1=1 而且 每个条件之前加上 and
  19.  
  20. -->
  21. <select id="selectStudentsByIf" resultType="Student">
  22. select id,name,age from student
  23. where 1=1
  24. <if test="name!=null &amp; name!=''">
  25. and name like '%' #{name} '%'
  26. </if>
  27. <if test="age>0">
  28. and age > #{age}
  29. </if>
  30. </select>
  31. </mapper>

测试类

  1. public class StudentTest {
  2. StudentDao dao;
  3. SqlSession session;
  4.  
  5. @Before
  6. public void before() {
  7. // 因为需要关闭session 需要把session提取出去
  8. session = SessionUtil.getSession();
  9. dao = session.getMapper(StudentDao.class);
  10. }
  11.  
  12. @After
  13. public void after() {
  14. if (session != null) {
  15. session.close();
  16. }
  17. }
  18.  
  19. // 01.动态查询
  20. @Test
  21. public void test1() {
  22.  
  23. Student stu=new Student();
  24. //01.属性都不赋值 会查询所有
  25. //02.只给年龄赋值stu.setAge(10);
  26. //03.只给姓名赋值stu.setName("小");
  27. //04.同时给两个属性都赋值
  28. stu.setAge(10);
  29. stu.setName("小");
  30. List<Student> list = dao.selectStudentsByIf(stu);
  31. for (Student student : list) {
  32. System.out.println(student);
  33. }
  34. }
  35. }

2.where标签

上面的代码有点问题,就是在xml文件中的sql语句有where  1=1,如果查询条件多的话,性能是很低的,因为每次查询都需要判断一次!这时候 我们就需要使用 where 标签来代替!

  1. public interface StudentDao {
  2.  
  3. List<Student> selectStudentsByWhere(Student student);
  4. }

xml文件的配置  省略了  where  1=1

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <select id="selectStudentsByWhere" resultType="Student">
  8. select id,name,age from student
  9. <where>
  10. <!-- and 必须要加上mybatis只会减 不会加 -->
  11. <if test="name!=null &amp; name!=''">
  12. and name like '%' #{name} '%'
  13. </if>
  14. <if test="age>0">
  15. and age > #{age}
  16. </if>
  17. </where>
  18. </select>
  19. </mapper>

测试类中新增

  1. // 02.动态查询 where
  2. @Test
  3. public void test2() {
  4. Student stu=new Student();
  5. //01.属性都不赋值 会查询所有
  6. //02.只给年龄赋值stu.setAge(10);
  7. //03.只给姓名赋值stu.setName("小");
  8. //04.同时给两个属性都赋值
  9. stu.setAge(10);
  10. stu.setName("小");
  11. List<Student> list = dao.selectStudentsByWhere(stu);
  12. for (Student student : list) {
  13. System.out.println(student);
  14. }
  15. }

运行即可得到相同的结果!

2.choose标签

比如说当姓名不为空的时候,按照姓名来查询,年龄不为空的时候按照年龄来查询!如果都为空则返回空!

  1. public interface StudentDao {
  2. /**
  3. *动态sql的查询 参数是Student对象
  4. */
  5.  
  6. List<Student> selectStudentsByChoose(Student student);
  7. }

xml文件中配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6. <!-- 姓名不空 按照姓名查询 年龄不为空 按照年龄查询
  7. 只要满足一个when 则其他的when则不会执行!
  8. 如果都不满足,则会执行otherwise 也就是没有查询结果
  9. -->
  10. <select id="selectStudentsByChoose" resultType="Student">
  11. select id,name,age from student
  12. <where>
  13. <choose>
  14. <when test="name!=null and name!=''">
  15. and name like '%' #{name} '%'
  16. </when>
  17. <when test="age>0">
  18. and age > #{age}
  19. </when>
  20. <otherwise>
  21. 1!=1
  22. </otherwise>
  23. </choose>
  24. </where>
  25. </select>
  26. </mapper>

测试类代码

  1. // 03.动态查询 choose
  2. @Test
  3. public void test3() {
  4. Student stu=new Student();
  5. stu.setName("小"); //name 不会空 则会按照name来查询 其他的条件无效
  6. stu.setAge(10);
  7. //如果都没有赋值 则没有返回结果
  8. List<Student> list = dao.selectStudentsByChoose(stu);
  9. for (Student student : list) {
  10. System.out.println(student);
  11. }
  12. }

 4.choose标签 遍历数组

  1. public interface StudentDao {
  2.  
  3. List<Student> selectStudentsByForeach(int [] ids);
  4. }

xml文件中的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <select id="selectStudentsByForeach" resultType="Student">
  8. <!-- 这就不是动态查询了 而是把参数写成固定的了
  9. select id,name,age from student where id in(1,13,15)
  10. -->
  11. select id,name,age from student
  12. <if test="array.length>0"><!-- 看传递来的数组长度是否大于0,如果数组长度为0 则是查询所有信息-->
  13. where id in
  14. <foreach collection="array" item="myId" open="(" separator="," close=")">
  15. #{myId}
  16. </foreach>
  17. </if>
  18. </select>
  19. </mapper>

测试代码

  1. // 04.动态查询 foreach 遍历数组
  2. @Test
  3. public void test4() {
  4. int [] ids={1,13,15};
  5. List<Student> list = dao.selectStudentsByForeach(ids);
  6. for (Student student : list) {
  7. System.out.println(student);
  8. }
  9. }

 4.choose标签 遍历list集合

  1. public interface StudentDao {
  2.  
  3. List<Student> selectStudentsByForeachArray(List<Integer> ids);
  4. }

xml文件中的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <select id="selectStudentsByForeachArray" resultType="Student">
  8. select id,name,age from student
  9. <if test="list.size>0"><!-- 看传递来的数组长度是否大于0,如果数组长度为0 则是查询所有信息-->
  10. where id in
  11. <foreach collection="list" item="myId" open="(" separator="," close=")">
  12. #{myId}
  13. </foreach>
  14. </if>
  15. </select>
  16. </mapper>

测试代码

  1. // 05.动态查询 foreach 遍历list集合
  2. @Test
  3. public void test5() {
  4. List<Integer> ids=new ArrayList<Integer>();
  5. ids.add(1);
  6. ids.add(13);
  7. ids.add(14);
  8. List<Student> list = dao.selectStudentsByForeachArray(ids);
  9. for (Student student : list) {
  10. System.out.println(student);
  11. }
  12. }

4.choose标签 遍历自定义类型集合

  1. public interface StudentDao {
  2.  
  3. List<Student> selectStudentsByForeachStudent(List<Student> stus);
  4. }

xml文件中的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <!-- 遍历自定义类型的集合 -->
  8. <select id="selectStudentsByForeachStudent" resultType="Student">
  9. select id,name,age from student
  10. <if test="list.size>0"><!-- 看传递来的数组长度是否大于0,如果数组长度为0 则是查询所有信息-->
  11. where id in
  12. <foreach collection="list" item="stu" open="(" separator="," close=")">
  13. #{stu.id}
  14. </foreach>
  15. </if>
  16. </select>
  17. </mapper>

测试代码

  1. // 06.动态查询 foreach 遍历自定义集合
  2. @Test
  3. public void test6() {
  4. Student stu1 = new Student();
  5. stu1.setId(1);
  6. Student stu2 = new Student();
  7. stu2.setId(13);
  8. Student stu3 = new Student();
  9. stu3.setId(15);
  10. List<Student> stus=new ArrayList<Student>();
  11. stus.add(stu1);
  12. stus.add(stu2);
  13. stus.add(stu3);
  14. List<Student> list = dao.selectStudentsByForeachStudent(stus);
  15. for (Student student : list) {
  16. System.out.println(student);
  17. }
  18. }

5.sql片段

如果一个xml文件中的sql语句有很多相同的地方,则可以使用sql片段来替换!如:

  1. public interface StudentDao {
  2.  
  3. List<Student> selectStudentsBySql(List<Student> stus);
  4. }

xml文件中的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
  5. <mapper namespace="cn.bdqn.dao.StudentDao">
  6.  
  7. <!-- sql片段的使用 -->
  8. <select id="selectStudentsBySql" resultType="Student">
  9. <include refid="selectStudent"/><!-- 引入sql片段 -->
  10. <if test="list.size>0">
  11. where id in
  12. <foreach collection="list" item="stu" open="(" separator="," close=")">
  13. #{stu.id}
  14. </foreach>
  15. </if>
  16. </select>
  17.  
  18. <!-- 如果有需求不查询age了,之前需要在所有的查询中删除age字段,现在只需要在sql片段中删除即可! -->
  19. <sql id="selectStudent">
  20. select id,name,age from student
  21. </sql>
  22. </mapper>

测试代码

  1. // 07.sql片段
  2. @Test
  3. public void test7() {
  4. Student stu1 = new Student();
  5. stu1.setId(1);
  6. Student stu2 = new Student();
  7. stu2.setId(13);
  8. Student stu3 = new Student();
  9. stu3.setId(15);
  10. List<Student> stus=new ArrayList<Student>();
  11. stus.add(stu1);
  12. stus.add(stu2);
  13. stus.add(stu3);
  14. List<Student> list = dao.selectStudentsBySql(stus);
  15. for (Student student : list) {
  16. System.out.println(student);
  17. }
  18. }

mybatis06--动态sql的更多相关文章

  1. Mybatis-06 动态Sql

    Mybatis-06 动态Sql 多对一处理 多个学生,对应一个老师 对于学生这边而言,关联多个学生,关联一个老师 [多对一] 对于老师而言,集合,一个老师又很多学生 [一对多] 1.创建数据库 2. ...

  2. 值得注意的ibatis动态sql语法格式

    一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...

  3. Mysql - 游标/动态sql/事务

    游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...

  4. MyBatis4:动态SQL

    什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...

  5. 分享公司DAO层动态SQL的一些封装

    主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...

  6. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  7. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...

  8. 自定义函数执行动态sql语句

    --函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如:   create proc [dbo].[FUN_YSCL ...

  9. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

  10. mybatis 动态sql表达式相关应用

    一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...

随机推荐

  1. .NET轻量级ORM框架Dapper入门精通

    一.课程介绍 本次分享课程包含两个部分<.NET轻量级ORM框架Dapper修炼手册>和<.NET轻量级ORM框架Dapper葵花宝典>,阿笨将带领大家一起领略轻量级ORM框架 ...

  2. win10下搭建storm环境

    原文:https://blog.csdn.net/lu_wei_wei/article/details/80843365 1.下载storm; http://mirror.bit.edu.cn/apa ...

  3. ubuntu redis 自启动配置文件(关机有密码)

    #!/bin/bash # chkconfig : ### BEGIN INIT INFO # Provides: redis-server # Required-Start: $syslog $re ...

  4. AlexNet总结

    https://blog.csdn.net/Rasin_Wu/article/details/80017920 https://blog.csdn.net/chaipp0607/article/det ...

  5. iOS:基于RTMP的视频推流

    iOS基于RTMP的视频推流 一.基本介绍 iOS直播一出世,立马火热的不行,各种直播平台如雨后春笋,正因为如此,也同样带动了直播的技术快速发展,在IT界精通直播技术的猴子可是很值钱的.直播技术涉及的 ...

  6. 【Boost】boost::string_algo详解2——find相关函数

    来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明:   template<typename Range1T, typ ...

  7. [Javascript] Multiply Two Arrays over a Function in JavaScript

    Just like the State ADT an Array is also an Applicative Functor. That means we can do the same trick ...

  8. SSE图像算法优化系列十四:局部均方差及局部平方差算法的优化。

    关于局部均方差有着较为广泛的应用,在我博客的基于局部均方差相关信息的图像去噪及其在实时磨皮美容算法中的应用及使用局部标准差实现图像的局部对比度增强算法中都有谈及,即可以用于去噪也可以用来增强图像,但是 ...

  9. [Linux] - 利用ping给端口加密,限制访问

    Linux中,想对特定的端口加密访问,可以使用iptables的ping方式. 作用 访问被限制的端口,必需先ping发送对应的字节包(字节包大小可自行设置,此为密钥)才能访问成功! 下边是对SSH的 ...

  10. [Python设计模式] 第25章 联合国维护世界和平——中介者模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目背景 联合国在世界上就是中介者的角色,各国之间的关系复杂,类似不同的对象和对 ...