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 ...
随机推荐
- TFS权限配置
装了TFS,要给TFS里添加用户,然后分配权限.其实一般项目中权限都不会控制的那么细,所以就直接想给项目组的每个人建一个用户,让他们都能访问这个项目的代码并进行任何操作.只想怎么简单怎 ...
- asp.net mvc 无刷新加载
1.视图(index) <!--start--> <div data-am-widget="list_news" class="am-list-news ...
- 深入浅出 Webpack
深入浅出 Webpack 评价 Webpack 凭借强大的功能与良好的使用体验,已经成为目前最流行,社区最活跃的打包工具,是现代 Web 开发必须掌握的技能之一.作者结合自身的实战经验,介绍了 Web ...
- 深入理解计算机系统(1)--hello world程序的生命周期
第一篇笔记的主题是讨论Hello World程序的生命周期,程序是最简单的hello world程序,使用高级C语言编写. 先介绍整个生命周期中涉及到的几个部分以及相应的概念,然后总结整个生命周期,最 ...
- jmeter使用beanshell构造参数化
1.先在本地写一个java类,用来随机生成一个数字,如: package com.jmeter.test; public class BeanShellTest { public int getRan ...
- Sqlite Datetime类型详解
日期和时间函数 date(timestring, modifier, modifier, ...) time(timestring, modifier, modifier, ...) datetime ...
- 【个人笔记】关于C++小数的处理
无论是C-Style还是C++-Style的输出,小数都会四舍五入.如果想要截断两种比较好的方法.第一种:利用sscanf输出成字符串,再人为地putchar().第二种:已知钦定保留6位小数,那么可 ...
- Python简要标准库(3)
shelve 若只需要一个简单的存储方案,那么shelve模块可以满足你大部分的需要,你所需要的只是为它提供文件名.shelve中唯一有趣的函数是open,在调用的时候他会返回一个Shelf对象 注意 ...
- centos7源码安装cloud-init
<template> <name>centos72-source</name> <os> <name>CentOS-7</name&g ...
- CodeForces-1121C System Testing
题目链接 https://vjudge.net/problem/CodeForces-1121C 题面 Description Vasya likes taking part in Codeforce ...