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);

<!—— collection:集合的型別可以是list或者array item:每一個元素的內容 open:拼接字串的時候開始比如,in(XX,XX) 的開始括號 close:拼接字串的時候開始比如,in(XX,XX) 的結束括號 separator:元素之間使用什麼進行分割——>

@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);

<!—— collection:集合的型別可以是list或者array item:每一個元素的內容 open:拼接字串的時候開始比如,in(XX,XX) 的開始括號 close:拼接字串的時候開始比如,in(XX,XX) 的結束括號 separator:元素之間使用什麼進行分割——>

@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);

<!—— collection:集合的型別可以是list或者array item:每一個元素的內容 open:拼接字串的時候開始比如,in(XX,XX) 的開始括號 close:拼接字串的時候開始比如,in(XX,XX) 的結束括號 separator:元素之間使用什麼進行分割——>

@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}