接口方法:
public abstract List<Student> selectStudentList2(List<Student> listId);
mapper 文件:
<select id="selectStudentList2" resultType="com.wzy.pojo.Student">select id,name,gender,age,birthday,files from student<where><if test="list != null and list.size > 0">id in<foreach collection="list" open="(" item="studentId" separator="," close=")">#{studentId.id}</foreach></if></where></select>
测试方法:
package com.wzy.dao2;import com.wzy.pojo.Student;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;public class StudentDao2Test003 {@Testpublic void selectStudentList2() throws IOException {//1.MyBatis2.xml 主配置文件String config = "MyBatis2.xml";//2.读取主配置文件InputStream resourceAsStream = Resources.getResourceAsStream(config);//3.创建 SqlSessionFactory 对象,用于获取 Dao 代理对象SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);//4.Dao 代理对象StudentDao2 studentDao2 = factory.openSession().getMapper(StudentDao2.class);//5.把筛选数据保存到集合List<Student> list = new ArrayList<>();Student student1 = new Student();student1.setId(1);list.add(student1);student1 = new Student();student1.setId(2);list.add(student1);//6.执行查询语句List<Student> studentList = studentDao2.selectStudentList2(list);//7.输出结果studentList.forEach(student -> System.out.println(student));}}
