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 ...
随机推荐
- 【java并发编程实战】第七章:取消与关闭
停止线程的几种方式 一般的逻辑停止 public class ThreadInterruptTest { public static volatile boolean cancel = true; p ...
- vue路由文档笔记
引入router this.$router 和 router 使用起来完全一样.我们使用 this.$router 的原因是我们并不想在每个独立需要封装路由的组件中都导入路由 可以在任何组件内通过 t ...
- vs code 在终端下使用 code ./ 打开当前项目
Mac OS Visual Studio Code的扩展工具菜单中有Install command line的快捷安装 运行 VS code并打开命令面板( ⇧⌘P ),然后输入 shell comm ...
- HDU 5794 A Simple Chess Lucas定理+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 题意概述: 给出一个N*M的网格.网格上有一些点是障碍,不能经过.行走的方式是向右下角跳马步.求 ...
- org.json.Json Object的put和append方法比较
json.append(key,value) 会把 value 包装成一个数组 JSONObject append = new JSONObject().append("a", & ...
- webmagic 二次开发爬虫 爬取网站图片
webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫. webmagic介绍 编写一个简单的爬虫 webmagic的使用文档:http://w ...
- Hadoop执行bin/stop-all.sh时no namenode to stop异常
1 先关闭hadoop所有服务 命令: bin/stop-all.sh 2 格式化namenode 命令: bin/hadoop namenode -format 3 重新启动所有服务 命令: bin ...
- 列数不固定时怎么使用el-tabel展示数据
<el-table :data="contents" stripe> <el-table-column v-for="(item, index) in ...
- [剑指Offer] 17.树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) [思路]要查找树A中是否存在和树B结构一样的子树,可以分成两步: 1.第一步在树A中找到和B的根节 ...
- ARC075 F.Mirrored
题目大意:给定D,询问有多少个数,它的翻转减去它本身等于D 题解做法很无脑,利用的是2^(L/2)的dfs,妥妥超时 于是找到了一种神奇的做法. #include <iostream> u ...