多对多模型:
查询学生的同时查询课程的信息对应的 SQL :
SELECT * FROM `student`SELECT c.* FROM `student_course` sc INNER JOIN `course` c ON sc.cid = c.id WHERE sc.sid = #{id}
示例:
Student.java
package com.github.entity; import java.io.Serializable; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ public class Student implements Serializable { private Integer id; private String name; private Integer age; private List < Course > courseList; public Student() {} public Student(Integer id, String name, Integer age, List < Course > courseList) { this.id = id; this.name = name; this.age = age; this.courseList = courseList; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }@Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", courseList=" + courseList + '}'; } }
Course.java
package com.github.entity; import java.io.Serializable; /** * @author maxiaoke.com * @version 1.0 * */ public class Course implements Serializable { private Integer id; private String name; public Course() {} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }@Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
CourseMapper.java
package com.github.mapper; import com.github.entity.Course; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ @Mapperpublic interface CourseMapper {@Select("SELECT c.* FROM `student_course` sc INNER JOIN `course` c ON sc.cid = c.id WHERE sc.sid = #{id}") List < Course > findBySid(Integer id); }
StudentMapper.java
package com.github.mapper; import com.github.entity.Student; import org.apache.ibatis.annotations. * ; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ @Mapperpublic interface StudentMapper {@Select("SELECT * FROM `student`")@Results({@Result(id = true, property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age"), @Result(property = "courseList", //被包含对象的变量名 column = "id", //根据查询出来的student表中的id去中间表和course表中查询 many = @Many(select = "com.github.mapper.CourseMapper.findBySid") ) }) List<Student> findAll();}
测试:
package com.github.mybatis; import com.github.entity.Student; import com.github.mapper.StudentMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * @author maxiaoke.com * @version 1.0 * */ public class MybatisTest { SqlSession sqlSession;@Before public void before() throws IOException { // 使用Mybatis的Resources读取mybatis-config.xml配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); sqlSession = sqlSessionFactory.openSession(); } @Test public void test() throws IOException { StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); List<Student> studentList = studentMapper.findAll(); studentList.forEach(System.out::println); } @After public void after() { if (null != sqlSession) { sqlSession.close(); } }}