mybatis06--动态sql
1.if标签

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

xml文件中的内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.StudentDao"> <!-- 需要注意的事项:
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 & name!=''">
and name like '%' #{name} '%'
</if>
<if test="age>0">
and age > #{age}
</if>
</select>
</mapper>

测试类

public class StudentTest {
StudentDao dao;
SqlSession session;
@Before
public void before() {
// 因为需要关闭session 需要把session提取出去
session = SessionUtil.getSession();
dao = session.getMapper(StudentDao.class);
}
@After
public void after() {
if (session != null) {
session.close();
}
}
// 01.动态查询
@Test
public void test1() {
Student stu=new Student();
//01.属性都不赋值 会查询所有
//02.只给年龄赋值stu.setAge(10);
//03.只给姓名赋值stu.setName("小");
//04.同时给两个属性都赋值
stu.setAge(10);
stu.setName("小");
List<Student> list = dao.selectStudentsByIf(stu);
for (Student student : list) {
System.out.println(student);
}
}
}

2.where标签
上面的代码有点问题,就是在xml文件中的sql语句有where 1=1,如果查询条件多的话,性能是很低的,因为每次查询都需要判断一次!这时候 我们就需要使用 where 标签来代替!
public interface StudentDao {
List<Student> selectStudentsByWhere(Student student);
}
xml文件的配置 省略了 where 1=1

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.StudentDao"> <select id="selectStudentsByWhere" resultType="Student">
select id,name,age from student
<where>
<!-- and 必须要加上mybatis只会减 不会加 -->
<if test="name!=null & name!=''">
and name like '%' #{name} '%'
</if>
<if test="age>0">
and age > #{age}
</if>
</where>
</select>
</mapper>

测试类中新增

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

运行即可得到相同的结果!
2.choose标签
比如说当姓名不为空的时候,按照姓名来查询,年龄不为空的时候按照年龄来查询!如果都为空则返回空!

public interface StudentDao {
/**
*动态sql的查询 参数是Student对象
*/
List<Student> selectStudentsByChoose(Student student);
}

xml文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.StudentDao">
<!-- 姓名不空 按照姓名查询 年龄不为空 按照年龄查询
只要满足一个when 则其他的when则不会执行!
如果都不满足,则会执行otherwise 也就是没有查询结果
-->
<select id="selectStudentsByChoose" resultType="Student">
select id,name,age from student
<where>
<choose>
<when test="name!=null and name!=''">
and name like '%' #{name} '%'
</when>
<when test="age>0">
and age > #{age}
</when>
<otherwise>
1!=1
</otherwise>
</choose>
</where>
</select>
</mapper>

测试类代码

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

4.choose标签 遍历数组
public interface StudentDao {
List<Student> selectStudentsByForeach(int [] ids);
}
xml文件中的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.StudentDao"> <select id="selectStudentsByForeach" 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"><!-- 看传递来的数组长度是否大于0,如果数组长度为0 则是查询所有信息-->
where id in
<foreach collection="array" item="myId" open="(" separator="," close=")">
#{myId}
</foreach>
</if>
</select>
</mapper>

测试代码

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

4.choose标签 遍历list集合
public interface StudentDao {
List<Student> selectStudentsByForeachArray(List<Integer> ids);
}
xml文件中的配置

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

测试代码

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

4.choose标签 遍历自定义类型集合
public interface StudentDao {
List<Student> selectStudentsByForeachStudent(List<Student> stus);
}
xml文件中的配置

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

测试代码

// 06.动态查询 foreach 遍历自定义集合
@Test
public void test6() {
Student stu1 = new Student();
stu1.setId(1);
Student stu2 = new Student();
stu2.setId(13);
Student stu3 = new Student();
stu3.setId(15);
List<Student> stus=new ArrayList<Student>();
stus.add(stu1);
stus.add(stu2);
stus.add(stu3);
List<Student> list = dao.selectStudentsByForeachStudent(stus);
for (Student student : list) {
System.out.println(student);
}
}

5.sql片段
如果一个xml文件中的sql语句有很多相同的地方,则可以使用sql片段来替换!如:
public interface StudentDao {
List<Student> selectStudentsBySql(List<Student> stus);
}
xml文件中的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
<mapper namespace="cn.bdqn.dao.StudentDao"> <!-- sql片段的使用 -->
<select id="selectStudentsBySql" resultType="Student">
<include refid="selectStudent"/><!-- 引入sql片段 -->
<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>
</mapper>

测试代码

// 07.sql片段
@Test
public void test7() {
Student stu1 = new Student();
stu1.setId(1);
Student stu2 = new Student();
stu2.setId(13);
Student stu3 = new Student();
stu3.setId(15);
List<Student> stus=new ArrayList<Student>();
stus.add(stu1);
stus.add(stu2);
stus.add(stu3);
List<Student> list = dao.selectStudentsBySql(stus);
for (Student student : list) {
System.out.println(student);
}
}
mybatis06--动态sql的更多相关文章
- Mybatis-06 动态Sql
Mybatis-06 动态Sql 多对一处理 多个学生,对应一个老师 对于学生这边而言,关联多个学生,关联一个老师 [多对一] 对于老师而言,集合,一个老师又很多学生 [一对多] 1.创建数据库 2. ...
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- Mysql - 游标/动态sql/事务
游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...
- MyBatis4:动态SQL
什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 自定义函数执行动态sql语句
--函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如: create proc [dbo].[FUN_YSCL ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
- mybatis 动态sql表达式相关应用
一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...
随机推荐
- Error opening terminal: xterm-256color
在使用gdb调试linux内核时,提示如下错误: arm-none-linux-gnueabi-gdb --tui vmlinux Error opening terminal: xterm-256c ...
- 分享12款令人瞠目结舌的WebVR演示和实验效果
不管你信不信, WebVR绝对是浏览器下一个让你激动的技术方向, 也许很快你就可以使用VR头显或者相关设备直接访问web内容和资源啦! 在这篇资源分享帖中,我们将介绍很多基于浏览器的VR演示和游戏,帮 ...
- laravel实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 laravel是我工作10多年来见到的真正能称得上让phper从面条一样杂乱的代 ...
- Java 基础【19】代理
Java 代理(Proxy)模式与现实中的代理含义一致,如旅游代理.明星的经纪人. 在目标对象实现基础上,增加额外的功能操作,由此来扩展目标对象的功能. JavaWeb 中最常见的过滤器.Struts ...
- .NET 同步与异步 之 EventWaitHandle(Event通知) (十三)
本随笔续接:.NET 同步与异步 之 Mutex (十二) 在前一篇我们已经提到过Mutex和本篇的主角们直接或间接继承自 WaitHandle: Mutex类,这个我们在上一篇已经讲过. Event ...
- maven在Idea建立工程,运行出现Server IPC version 9 cannot communicate with client version 4错误
问题的根源在于,工程当中maven dependencies里面的包,有个hadoop-core的包,版本太低,这样,程序里面所有引用到org.apache.hadoop的地方,都是低版本的,你用的是 ...
- MySQL技术内幕读书笔记(七)——锁
锁 锁是数据库系统区分与文件系统的一个关键特性.为了保证数据一致性,必须有锁的介入.数据库系统使用锁是为了支持对共享资源进行并发访问,提供数据的完整性和一致性. lock与latch 使用命令 ...
- Android App 安全的HTTPS 通信
漏洞描述 对于数字证书相关概念.Android 里 https 通信代码就不再复述了,直接讲问题.缺少相应的安全校验很容易导致中间人攻击,而漏洞的形式主要有以下3种: 自定义X509TrustMana ...
- Unity应用架构设计(4)——设计可复用的SubView和SubViewModel(Part 2)
在我们设计和开发应用程序时,经常要用到控件.比如开发一个客户端WinForm应用程序时,微软就为我们提供了若干控件,这些控件为我们提供了可被定制的属性和事件.属性可以更改它的外观,比如背景色,标题等, ...
- CentOS 6.5 x64下网络配置
一.自动获取IP地址 #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,ip地址.MAC地址 [root@CentOS6 ~]# vi /etc/sysconfig/n ...