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系列四 之 智能标签进行查询语句的拼接的更多相关文章

  1. Mybatis 系列9-强大的动态sql 语句

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

  2. mysql第四篇--SQL逻辑查询语句执行顺序

    mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> < ...

  3. Mybatis系列(四):Mybatis缓存

    一.MyBatis缓存介绍 MyBatis 提供了一级缓存和二级缓存的支持        1. 一级缓存: 默认开启,基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  4. 深入浅出Mybatis系列四-配置详解之typeAliases别名(mybatis源码篇)

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(三)---配置详解之properties ...

  5. sql查询语句的拼接小技巧(高手勿喷)

    1. 基本的查询语句后面加上 WHERE 1=1,便于增加查询条件. ASkStr := 'select * from Twork where 1=1 '; if length(cxTEworkid. ...

  6. mybatis 学习四(下) SQL语句映射文件增删改查、参数、缓存

    2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id="getStudent" paramet ...

  7. Mybatis 系列9

    上篇系列8中 简单介绍了mybatis的查询,至此,CRUD都已讲完. 本文将介绍mybatis强大的动态SQL. 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方 ...

  8. Mybatis 系列6

    上篇系列5中 简单看了一下TypeHandler, 本次将结束对于mybatis的配置文件的学习, 本次涉及到剩下没提及到的几个节点的配置:objectFactory.databaseIdProvid ...

  9. Mybatis 系列3

    系列文章 2 中,我们通过对mybatis源码的简单分析,可看出,在mybatis配置文件中,在configuration根节点下面,可配置properties.typeAliases.plugins ...

随机推荐

  1. Linux 文件属性及修改权限

    输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...

  2. Python request 简单使用

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  3. 剑指offer-变态跳台阶09

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. class Solution: def jumpFloorII(self, n ...

  4. AMR无限增发代币至任意以太坊地址的漏洞利用及修复过程

    AMR无限增发代币至任意以太坊地址的漏洞利用及修复过程 0x00 项目简述 Ammbr主要目标是打造具有高度弹性且易于连接的分布式宽带接入平台,同时降低上网相关成本.Ammbr打算创建具有人工智能和智 ...

  5. 团队项目-第五次Scrum 会议

    时间:10.31 时长:30分钟 地点:教室(主南201) 工作情况 团队成员 已完成任务 待完成任务 解小锐 修复在接受任务时,前端和后端对接中的bug 完成员工信息的简单初始化 陈鑫 完成hire ...

  6. 将EXCEL表中的数据轻松导入Mysql数据表

    转载自:http://blog.163.com/dielianjun@126/blog/static/164250113201042310181431/ 在网络上有不较多的方法,在此介绍我已经验证的方 ...

  7. python基础之列表解析

    python列表解析:是一个让人欣喜的术语,你可以在一行使用一个for循环将所有的值放在一个列表之中.python列表解析属于python的迭代中的一种,相比python for循环速度会快很多. e ...

  8. Uuuuuunity

    foreach http://blog.csdn.net/byondocean/article/details/6871881 yield  http://www.cnblogs.com/CareyS ...

  9. Android Studio的初体验

    在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...

  10. Spring温故而知新 – bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...