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. pprof 查看goroutine

    package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...

  2. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...

  3. 原生js实现五子棋

    为什突然做这个,因为这是个笔试题,拖了一个月才写(最近终于闲了O(∩_∩)O),废话不多说,说说这个题吧 题目要求 编写一个单机[五子棋]游戏,要求如下: 1.使用原生技术实现,兼容 Chrome 浏 ...

  4. 让PC版网站在移动端原样式显示

    一般PC网站在移动端显示效果往往和PC版原样式不同,为了在移动端下还原原PC站样式,可以采用以下方式解决: 1) 去掉页头的: <meta name="viewport" c ...

  5. LINQ学习笔记——(3)基本查询操作符

    Select() 作用于uIEnumerable<TSource>类型 public static void Test() { List<string> persons = n ...

  6. 容器基础(五): 实现一个简单容器sdocker

    在前面几部分的基础上, 我们更新一下代码,实现一个简单容器 sdocker. sdocker目录构成 linux: # tree . ├── Makefile ├── cpu-test.c # 由cp ...

  7. URAL 1932 The Secret of Identifier(容斥)

    Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...

  8. 创建带maven的javaWeb项目

    1File——Maven——maven-archtypes-webapp GroupId:表示项目组织唯一标识符 ArtifacrId:表示项目唯一标识符 例如项目名称 Version是项目版本 这三 ...

  9. servletContext的定义

  10. thrift的lua

    thrift的lua实现 最近要进行系统升级,后台的数据是根据城市区分的.担心新系统的稳定性及新数据的准确性,计划部分城市采用新接口.接口的入参里没有城市信息,只有经纬度坐标,需要调用一个thrift ...