我们学习了如何使用 Mybatis if where trim 等动态语句来处理一些简单的查询操作。对于一些 SQL 语句中含有 in 条件,需要迭代条件集合来生成的情况,可以使用 foreach 来实现 SQL 条件的迭代

Mybatis foreach 标签用于循环语句,它很好的支持了数据和 List、set 接口的集合,并对此提供遍历的功能。语法格式如下。

•	<foreach item="item" index="index" collection="list|array|map key" open="(" separator="," close=")">
•	参数值
•	</foreach>

foreach 标签主要有以下属性,说明如下。

  • item:表示集合中每一个元素进行迭代时的别名。比如你是对哪个列的东西进行迭代,则需要填写该列的列名
  • index:指定一个名字,表示在迭代过程中每次迭代到的位置。
  • open:表示该语句以什么开始(既然是 in 条件语句,所以必然以(括号的左半部分开始)。
  • separator:表示在每次进行迭代之间以什么符号作为分隔符(既然是 in 条件语句,所以必然以  ,   逗号作为分隔符)。
  • close:表示该语句以什么结束(既然是 in 条件语句,所以必然以)括号的右半部分开始)。
    使用 foreach 标签时,最关键、最容易出错的是 collection 属性,该属性是必选的,但在不同情况下该属性的值是不一样的,主要有以下 3 种情况:
  • 如果传入的是单参数且参数类型是一个 List,collection 属性值为 list。
  • 如果传入的是单参数且参数类型是一个 array 数组,collection 的属性值为 array。
  • 如果传入的参数是多个,需要把它们封装成一个 Map,当然单参数也可以封装成 Map。Map 的 key 是参数名,collection 属性值是传入的 List 或 array 对象在自己封装的 Map 中的 key。
  • Student表中内容

StudentMapper接口文档内容

public List<Student> selectStudent(List<Integer> ageList);

因为我们后续传出的是student,这个是一个数组,因为查出来的内容是不止一个的

而传入的也是一个年龄的数组,因为后续的查询是

SELECT * FROM student where age in age1age2

   而后面(age1age2是可以看成一个数组,age的类型是integer的类型

StudentMapper.xml中

<select id="selectStudent" parameterType="com.cmj.entity.Student" resultType="com.cmj.entity.Student">
		SELECT * FROM student WHERE age in
			<foreach collection="list" item="age" index="index" open="(" separator="," close=")"> 
				#{age}
			</foreach>
	</select>

 在我们的test中去写:

private SqlSessionFactory getFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		// 初始化mybatis,创建SqlSessionFactory类实例
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		return sqlSessionFactory;
///foreach學習 
	@Test
	public void selectStudent() throws IOException {
		SqlSession session=getFactory().openSession();
		StudentMapper mapper =session.getMapper(StudentMapper.class);	
		List<Integer> ageList=new ArrayList<Integer>();
		ageList.add(3);
		ageList.add(6);
		List<Student> stuList =mapper.selectStudent(ageList);
		for (Student student : stuList) {
			System.out.println(student);

这样就可以在数据库表中进行循环查出对应年龄在(你需要查询的年龄中)的人员的名单及对应的详细信息

我们学习了如何使用 Mybatisif、where、trim等动态语句来处理一些简单的查询操作。对于一些 SQL 语句中含有 in 条件,需要迭代条件集合来生成的情况,可以使用 foreach 来实现 SQL 条件的迭代Mybatis foreach 标签用于循环语句,它很好的支持了数据和 List、set 接口的集合,并对此提供遍历的功能。语法格式如下。• &lt;foreach item="item" index="index" collection="list|array|map key.. public class SysRoleData extends DataEntity&lt;SysRoleData&gt; { private static final long serialVersionUID = 1L; private String kind; /... import com.youkeda.comment.dataobject.UserDO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.time.LocalDateTime; import jav. collection:必填,值为要迭代循环的属性名。这个属性值的情况有很多 item :变量名,值为从迭代对象取出来的每一个值 index: 索引的属性名,在集合数组情况下值为当前索引值,当迭代循环的对象时Map类型时,这个值为map的key值 open: 整个循环内容开头的字符串 close:整个循环内容结尾的字符串 separator: 每次循环的分隔符
在做mybatis的mapper.xml文件的时候,我们时常用到这样的情况:动态生成sql语句的查询条件,这个时候我们就可以用mybatisforeachforeach元素的属性主要有item,index,collection,open,separator,close。 item:集合元素迭代时的别名,该参数为必选。 index:在list和数组,index是元素的序号,在map,index是元素的key,该参数可选 open:foreach代码的开始符号,一般是(和close="...
二、注意事项 mybatis 多个foreach 循环,第一次循环的collection 看到的值是一个(数组、list、map、对象,由collection的配置主导)。 第二次foreach 是直接copy第一个foreach的...
delete from xxx_table where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> => (1,2,3,4) foreach标签:主要应用于批量删除与批量插入操作。 foreach标签包含的的常用属性有collection,item,index,open,separ.
鳝鱼lebron: 2、状态为阻塞状态时:sleep、wiat、join还有synchronized抢锁的状态时,他也是会进行中断位置的标记,并且通过抛出异常的方式进行中断; 大佬,这种状态的时候,应该是清除中断标记吧 源码的注释 <p> If this thread is blocked in an invocation of the {@link * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link * Object#wait(long, int) wait(long, int)} methods of the {@link Object} * class, or of the {@link #join()}, {@link #join(long)}, {@link * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, * methods of this class, then its interrupt status will be cleared and it * will receive an {@link InterruptedException}. SpringBoot分页查询 学习威龙463: 感谢!没想到还没学到分页的技术 先把模糊查询的sql文改好了 感谢大佬细心写出来呜呜 JAVA中的switch YCS14579: Java SE 7是JDK1.5吗 redis中数据存取 qq_40507331: 比如100个学生信息,redis怎么搭配分页呢? 登录拦截器 weixin_51531948: <mvc:exclude-mapping path="/student/loading"/>,你应该把你文件结构图发一下,不然初学者不知道配置拦截路径在哪