接口方法:

    1. public abstract List<Student> selectStudentList2(List<Student> listId);

    mapper 文件:

    1. <select id="selectStudentList2" resultType="com.wzy.pojo.Student">
    2. select id,name,gender,age,birthday,files from student
    3. <where>
    4. <if test="list != null and list.size > 0">
    5. id in
    6. <foreach collection="list" open="(" item="studentId" separator="," close=")">
    7. #{studentId.id}
    8. </foreach>
    9. </if>
    10. </where>
    11. </select>

    测试方法:

    1. package com.wzy.dao2;
    2. import com.wzy.pojo.Student;
    3. import org.apache.ibatis.io.Resources;
    4. import org.apache.ibatis.session.SqlSessionFactory;
    5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    6. import org.junit.Test;
    7. import java.io.IOException;
    8. import java.io.InputStream;
    9. import java.util.ArrayList;
    10. import java.util.List;
    11. public class StudentDao2Test003 {
    12. @Test
    13. public void selectStudentList2() throws IOException {
    14. //1.MyBatis2.xml 主配置文件
    15. String config = "MyBatis2.xml";
    16. //2.读取主配置文件
    17. InputStream resourceAsStream = Resources.getResourceAsStream(config);
    18. //3.创建 SqlSessionFactory 对象,用于获取 Dao 代理对象
    19. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    20. //4.Dao 代理对象
    21. StudentDao2 studentDao2 = factory.openSession().getMapper(StudentDao2.class);
    22. //5.把筛选数据保存到集合
    23. List<Student> list = new ArrayList<>();
    24. Student student1 = new Student();
    25. student1.setId(1);
    26. list.add(student1);
    27. student1 = new Student();
    28. student1.setId(2);
    29. list.add(student1);
    30. //6.执行查询语句
    31. List<Student> studentList = studentDao2.selectStudentList2(list);
    32. //7.输出结果
    33. studentList.forEach(student -> System.out.println(student));
    34. }
    35. }