21.Mybatis动态SQL之foreach

在Mybaits中foreach适用于数组,List集合等函数,对数组、List集合的元素进行循环,一般使用在查询语句是in语法中,foreach的标准语法由一下标签构成:

collection:表示循环的对象是数组还是list集合。如果mapper接口方法地形参是数组,collection="array";如果mapper接口形参是 List,collection="list";

open:循环开始时的字符。 sql.append("(");

close:循环结束是的字符。 sql.append(")");

item:集合成员,自定义的变量。 Integer item = idlist.get(i); //item是集合成员;

separator:集合成员之间的分隔符。 sql.append(",");

#{item的值}:获取集合成员的值。

一、foreach值Array

/** * 根据学生ID的数组查询学生信息 * @param ids * @return */List selectStudentByIds(String[] ids);
@Testpublic void selectStudentByIds() {    //把配置文件读取到数据流    InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml");    //创建SqlSessionFactory    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);    //创建SqlSession    SqlSession sqlSession = sqlSessionFactory.openSession();    //使用反射获取StudentMapper    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);    //查询ID的数组    String[] ids = {"4", "5", "6"};    //查询数据    List students = studentMapper.selectStudentByIds(ids);    //打印结果    students.forEach(student -> System.out.println("student = " + student));}

二、foreach简单类型的List

简单类型的List和Array十分类似,在查询的方法上,传递的是List,在Mapper.xml中collection="list"使用的是list,其他全部一样;

/** * 根据学生ID的数组查询学生信息 * @param ids * @return */List selectStudentByIds(List ids);
@Testpublic void selectStudentByIds() {    //把配置文件读取到数据流    InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml");    //创建SqlSessionFactory    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);    //创建SqlSession    SqlSession sqlSession = sqlSessionFactory.openSession();    //使用反射获取StudentMapper    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);    //查询ID的集合    List list = new ArrayList<>();    list.add("3");    list.add("4");    //查询数据    List students = studentMapper.selectStudentByIds(list);    //打印结果    students.forEach(student -> System.out.println("student = " + student));}

三、foreach对象类型的List

当List集合中存储的是对象的时候,取值时需要使用对象.属性,取得属性值;

/** * 根据某一个学生的Clazzno查询同班学生 * @param list 学生的信息集合 * @return */List selectStdByStuClazzNo(List list);
@Testpublic void selectStudentByIds() {    //把配置文件读取到数据流    InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml");    //创建SqlSessionFactory    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);    //创建SqlSession    SqlSession sqlSession = sqlSessionFactory.openSession();    //使用反射获取StudentMapper    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);    //构造查询的集合    List list=new ArrayList<>();    //构造学生信息    Student student=new Student();    student.setCalzzno("002");    //添加学生信息    list.add(student);    //查询数据    List students = studentMapper.selectStdByStuClazzNo(list);    //打印结果    students.forEach(stu -> System.out.println("stu = " + stu));}

注意:上一个方法每一次循环出来是一个id,这个方法每次循环出来是一个student,在sql语句的条件上,需要指定:#{student.id}

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章