Mybatis中的一对多、多对一关联关系可以通过xml文档配置,也可以通过annotation注解配置:
以student(多)和school(一)为例:
config.xml:
<mappers>
<mapper class="com.dw.dao.SchoolMapper"/>
<mapper class="com.dw.dao.StudentMapper"/>
</mappers>
SchoolMapper.java:
package com.dw.dao;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import com.dw.domain.School;
public interface SchoolMapper {
/**单表查询*/
@Select("select * from t_school as sc where sc.sc_id=#{id}")
@Results({
@Result(column="sc_id",property="id"),
@Result(column="sc_name",property="name")
})
public School selectSchoolById(Integer id);
/**一对多 多表查询*/
@Select("select * from t_school as sc where sc.sc_id=#{id}")
@Results({
@Result(column="sc_id",property="id"),
@Result(column="sc_name",property="name"),
@Result(column="sc_id",property="students",many=@Many(select="com.dw.dao.StudentMapper.selectStudentBySchoolId",fetchType=FetchType.LAZY))
})
public School selectSchoolById2(Integer id);
}
StudentMapper.java:
package com.dw.dao;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.dw.domain.School;
import com.dw.domain.Student;
public interface StudentMapper {
/**单表查询*/
@Select("select * from t_student as s where s.s_id=#{id}")
@Results({
@Result(column="s_id",property="id"),
@Result(column="s_name",property="name"),
@Result(column="s_NO",property="NO")
})
public Student selectStudentById(Integer id);
/**多对一 多表查询*/
@Select("select * from t_student as s where s.s_id=#{id}")
@Results({
@Result(column="s_id",property="id"),
@Result(column="s_name",property="name"),
@Result(column="s_NO",property="NO"),
@Result(column="sc_id",property="school",javaType=School.class,
one=@One(select="com.dw.dao.SchoolMapper.selectSchoolById"))
})
public Student selectStudentById2(Integer id);
/**通过学校id查询学生*/
@Select("select s.s_id,s.s_name,s.s_NO from t_student as s where s.sc_id=#{id}")
@Results({
@Result(column="s_id",property="id"),
@Result(column="s_name",property="name"),
@Result(column="s_NO",property="NO")
})
public List<Student> selectStudentBySchoolId(Integer id);
}
测试:
/**school单表查询*/
@Test
public void selectSchool4() {
myInit();
School school = schoolMapper.selectSchoolById(1);
System.out.println(school);
destory();
}
/**student单表查询*/
@Test
public void selectStudent4() {
myInit();
Student student = studentMapper.selectStudentById(1);
System.out.println(student);
}
/**多对一 多表查询*/
@Test
public void selectStudent5() {
myInit();
Student student = studentMapper.selectStudentById2(1);
System.out.println(student);
destory();
}
/**一对多 多表查询*/
@Test
public void selectSchool5() {
myInit();
School school = schoolMapper.selectSchoolById2(1);
System.out.println(school);
destory();
}
@SuppressWarnings("unused")
private void myInit() {
try {
in = Resources.getResourceAsStream("config.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
session = sessionFactory.openSession();
// userDao = session.getMapper(UserDao.class);
// schoolDao = session.getMapper(SchoolDao.class);
// studentDao = session.getMapper(StudentDao.class);
schoolMapper = session.getMapper(SchoolMapper.class);
studentMapper = session.getMapper(StudentMapper.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unused")
private void destory() {
if(session !=null) {
session.close();
}
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
School.java:
package com.dw.domain;
import java.util.List;
public class School {
private Integer id;
private String name;
private List<Student> students;
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 List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "School [id=" + id + ", name=" + name + ", students=" + students + "]";
}
}
Student.java:
package com.dw.domain;
public class Student {
private Integer id;
private String name;
private String NO;
private School school;
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 String getNO() {
return NO;
}
public void setNO(String nO) {
NO = nO;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", NO=" + NO + ", school=" + school + "]";
}
}