MyBatis系列四 之 智能标签进行查询语句的拼接
MyBatis系列四 之 智能标签进行查询语句的拼接
使用Foreach进行多条件查询
1.1 foreach使用数组进行多条件查询
在MyBatis的映射文件中进行如下配置
<!--根据数组进行多条件查询 -->
<select id="findByForeachAraay" resultType="Student">
select * from Student
<if test="array.length>0">
where sid in
<foreach collection="array" open="(" close=")" separator="," item="myid">
#{myid}
</foreach> </if>
</select>
在接口类中定义和映射文件中的查询语句的id值相同的方法名称
//根据数组查询
public List<Student> findByForeachAraay(int[] ids);
在测试类中进行如下代码的书写进行测试
//根据数组进行多条件查询
@Test
public void findByForeachAraay() throws IOException{ int[] ids=new int[2];
ids[0]=48;
ids[1]=50; List<Student> list = dao.findByForeachAraay(ids);
for (Student student : list) {
System.out.println(student.getSname());
}
}
1.2foreach使用list泛型集合进行多条件查询
在MyBatis的映射文件中做如下配置
<!--根据list进行多条件查询 -->
<select id="findByForeachlist" resultType="Student">
select * from Student
<if test="list.size>0">
where sid in
<foreach collection="list" open="(" close=")" separator="," item="myid">
#{myid}
</foreach> </if>
</select>
在接口类中定义一个和银蛇文件中id值相同的方法名称
//根据list集合进行多条件查询
public List<Student> findByForeachlist(List<Integer> ids);
在测试类中书写如下代码进行代码测试
//根据list集合进行多条件查询
@Test
public void findByForeachlist() throws IOException{ List<Integer> ids=new ArrayList<Integer>();
ids.add(49);
ids.add(50); List<Student> list = dao.findByForeachlist(ids);
for (Student student : list) {
System.out.println(student.getSname());
}
}
1.3foreach使用自定义的泛型集合进行多条件查询
在MyBatis的映射文件中做如下配置
<!--根据自定义泛型集合进行多条件查询 -->
<select id="findByForeachMyList" resultType="Student">
select * from Student
<if test="list.size>0">
where sid in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.sid}
</foreach> </if>
</select>
在接口中定义一个和映射文件中插叙语句的id值相同的方法名称
//根据自定义泛型集合进行多条件查询
public List<Student> findByForeachMyList(List<Student> ids);
在测试类中书写如下代码进行代码测试
//根据自定义泛型集合进行多条件查询
@Test
public void findByForeachMyList() throws IOException{ List<Student> ids=new ArrayList<Student>();
Student stu=new Student();
stu.setSid(48);
Student stu2=new Student();
stu2.setSid(49);
ids.add(stu);
ids.add(stu2); List<Student> list = dao.findByForeachMyList(ids);
for (Student student : list) {
System.out.println(student.getSname());
}
}
MyBatis系列四 之 智能标签进行查询语句的拼接的更多相关文章
- Mybatis 系列9-强大的动态sql 语句
[Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...
- mysql第四篇--SQL逻辑查询语句执行顺序
mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> < ...
- Mybatis系列(四):Mybatis缓存
一.MyBatis缓存介绍 MyBatis 提供了一级缓存和二级缓存的支持 1. 一级缓存: 默认开启,基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- 深入浅出Mybatis系列四-配置详解之typeAliases别名(mybatis源码篇)
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(三)---配置详解之properties ...
- sql查询语句的拼接小技巧(高手勿喷)
1. 基本的查询语句后面加上 WHERE 1=1,便于增加查询条件. ASkStr := 'select * from Twork where 1=1 '; if length(cxTEworkid. ...
- mybatis 学习四(下) SQL语句映射文件增删改查、参数、缓存
2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id="getStudent" paramet ...
- Mybatis 系列9
上篇系列8中 简单介绍了mybatis的查询,至此,CRUD都已讲完. 本文将介绍mybatis强大的动态SQL. 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方 ...
- Mybatis 系列6
上篇系列5中 简单看了一下TypeHandler, 本次将结束对于mybatis的配置文件的学习, 本次涉及到剩下没提及到的几个节点的配置:objectFactory.databaseIdProvid ...
- Mybatis 系列3
系列文章 2 中,我们通过对mybatis源码的简单分析,可看出,在mybatis配置文件中,在configuration根节点下面,可配置properties.typeAliases.plugins ...
随机推荐
- MySQL数据库服务器逐渐变慢分析
第一步 检查系统的状态 1.1 使用sar来检查操作系统是否存在IO问题 #sar -u 2 10 — 即每隔2秒检察一次,共执行20次. [root@CacheMemCache tester]# s ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 「日常训练」Mike and Feet(Codeforces Round #305 Div. 2 D)
题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了 ...
- Linux 文件与目录管理命令
处理目录的常用命令 常见的处理目录的命令: ls: 列出目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录,语法:mkdir [-mp] 目录名称 -m :配置文件的权限 -p ...
- mysql 5.7.18 源码安装笔记
之所以贴出这样一篇笔记呢?主要是因为很久之前,源码安装MySQL的时候,碰到了太多太多的坎坷. 如果你有兴趣进行源码安装,那么请不要以这篇文章为标准,因为每个人的及其环境等其他因素还是差距比较大的. ...
- 输出1-n的全排(递归C++)
[问题描述] 输出1到n之间所有不重复的排列,即1到n的全排,要求所产生的任一数列不含有重复的数字. [代码展示] #include<iostream>using namespace st ...
- 【读书笔记】2_增强学习中的Q-Learning
本文为Thomas Simonini增强学习系列文章笔记或读后感,原文可以直接跳转到medium系列文章. 主要概念为: Q-Learning,探讨其概念以及用Numpy实现 我们可以将二维游戏想象成 ...
- LeetCode 29——两数相除
1. 题目 2. 解答 2.1. 方法一 题目要求不能使用乘法.除法和除余运算,但我们可以将除法转移到对数域. \[ \frac{a}{b} = e^{\frac{lna}{lnb}} = e^{ln ...
- nonebot 源码阅读笔记
前言 nonebot 是一个 QQ 消息机器人框架,它的一些实现机制,值得参考. nonebot NoneBot 初始化(配置加载) 阅读 nonebot 文档,第一个示例如下: import non ...
- 【Android入门】——模拟器的创建及常见问题汇总
[前言] 刚刚接触Android,第一门课我们就来创建一个模拟器.安卓模拟器,简称AVD(Android Virtual Device),是安卓运行的虚拟设备.有了他以后,我们就不需要在连着安卓手机进 ...