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. poorpool 的 考场 NOI Linux 配置

    把~/.bashrc里的force_color_prompt=yes前面那个#去掉,终端就有高亮啦qwq!(然后source一下 然后vim ~/.vimrc然后加入 (为什么不改/etc/vim/v ...

  2. L007- linux系统优化进阶课堂小节

    首先把这节课所讲的大概引锁一下,然后下面详细列举. 1.填加普通用户,通过sudo管理. 2.更改默认的SSH服务端口及禁止root用户远程连接. 3.定时自动更新服务器时间 4.关闭防火墙(ipta ...

  3. 在Kotlin编写RecyclerView适配器(KAD 16)

    作者:Antonio Leiva 时间:Mar 14, 2017 原文链接:https://antonioleiva.com/recyclerview-adapter-kotlin/ 通过创建Recy ...

  4. LINUX系统下Java和Scala的环境配置

    最近,笔者在研究一个有关“自然语言处理”的项目,在这个项目中,需要我们用Spark进行编程.而Spark内核是由Scala语言开发的,所以在使用Spark之前,我们必须配置好Scala,而Scala又 ...

  5. HDU 1693 Eat the Trees(插头DP,入门题)

    Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...

  6. HDU 4588 Count The Carries(数学统计)

    Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...

  7. ORACLE和SQL语法区别归纳

    数据类型比较类型名称 Oracle   SQLServer   比较字符数据类型  CHAR  CHAR  都是固定长度字符资料但oracle里面最大度为2kb,SQLServer里面最大长度为8kb ...

  8. org.json.Json Object的put和append方法比较

    json.append(key,value) 会把 value 包装成一个数组 JSONObject append = new JSONObject().append("a", & ...

  9. Python + OpenCV 实现LBP特征提取

    背景 看了些许的纹理特征提取的paper,想自己实现其中部分算法,看看特征提取之后的效果是怎样 运行环境 Mac OS Python3.0 Anaconda3(集成了很多包,浏览器界面编程,清爽) 步 ...

  10. chm文件空白如何解决

    解决办法:http://jingyan.baidu.com/article/8275fc86b5fb6646a03cf6b0.html