二.关联关系:在实际开发中,对数据库的操作常常会涉及多张表,这在面向对象中就涉及了对象与对象之间的关联关系。针对多表之间的操作,MyBatis框架提供了关联映射,通过关联映射就可以很好地处理对象与对象之间的关联关系。
三. 一对一关系:下面我们看两张表,Teacher(教师表)和Teaching(所教课程表)然后这张表里面教师工号(Tno)和所教课程编号(cno)是一对一的关系。
上面分析的关系我们通过查询教师工号同时能看到教师所教的课程编号,及我们还要在Teacher持久类中添加teaching这个属性。(这是从Teacher角度考虑的)
然后我们来看他们的Mapper.xml配置文件,首先先看到他们的对应接口:
接着我们编写他们的Mapper.xml文件
然后可以按着接口写测试方法了。
代码一: 1.TeacherMapper.xml
2.TeachingMapper.xmlselect * from teacher where Tno=#{Tno}
方式二: 代码二:
三. 一对多关系:
使用association映射到JavaBean的某个“”复杂类型“属性,如JavaBean类,即JavaBean内部嵌套一个复杂数据类型(JavaBean)属性,这种情况就属于复杂类型的关联。但是要注意的是,association元素仅仅处理一对一的关联关系。
比如一个系可能对应多个教师,那我们要实现查询一个系号然后返回属于这个系号的教师名字,这时候我们就要用到一对多的关系了,这时我们就要用到collection来实现了。
在Department持久实体类中添加一个存放Teacher集合的List
TeacherMapper接口中加入方法
ListfindTeacherBydeptno(String deptno);
TeacherMapper.xml
测试: 代码:
方式二: 四. 多对多:
在实际开发中,多对多的关联关系也是很常见,在学校,一个学生选了多门课,一门课也有很多学生选,这就是多对多的关系。下面我们了解如何实现多对多的关系。
然后先创建两个持久化类Sc和Student
和他们对应的接口:
package org.example.mapper;
import org.apache.ibatis.annotations.Param;
import org.example.po.Student;
import java.util.List;
public interface StudentMapper {
List getStudent(@Param("cno")String cno);
}
package org.example.service;
import org.apache.ibatis.annotations.Param;
import org.example.po.Sc;
import java.util.List;
public interface ScService {
List getSc(String cno);
}
Mapper配置文件:
StudentMapper.xml:
SCMapper.xml:
代码: ScMapper.xmlStudentMapper.xml
sevice部分:
package org.example.service;
import org.apache.ibatis.annotations.Param;
import org.example.po.Sc;
import java.util.List;
public interface ScService {
List getSc(String cno);
}
package org.example.service;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.mapper.ScMapper;
import org.example.po.Sc;
import java.util.List;
public class ScServiceImpl implements ScService{
private SqlSessionFactory sqlSessionFactory;
public ScServiceImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory=sqlSessionFactory;
}
@Override
public List getSc(String cno) {
SqlSession sqlSession=sqlSessionFactory.openSession();
ScMapper scMapper=sqlSession.getMapper(ScMapper.class);
List scs=scMapper.getSc(cno);
sqlSession.close();
return scs;
}
}
测试方法:
package org.example;
import static org.junit.Assert.assertTrue;
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.example.mapper.TeacherMapper;
import org.example.po.Department;
import org.example.po.Sc;
import org.example.po.Student;
import org.example.po.Teacher;
import org.example.service.DepartmentServiceImpl;
import org.example.service.ScServiceImpl;
import org.example.service.TeacherServiceImpl;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class AppTest
{
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
InputStream in= Resources.getResourceAsStream("MyBatis_config.xml");
this.sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
}
// @Test
// public void findTeacherByTno(){
// TeacherServiceImpl teacherService=new TeacherServiceImpl(sqlSessionFactory);
// Teacher teacher=teacherService.findTeacherByTno("601");
// System.out.println(teacher);
// }
// @Test
// public void findteacherDeptno(){
// DepartmentServiceImpl departmentService=new DepartmentServiceImpl(sqlSessionFactory);
// List department=departmentService.findTeachersBydeptno("d01");
//
// }
@Test
public void getstudent(){
ScServiceImpl scService=new ScServiceImpl(sqlSessionFactory);
List scs=scService.getSc(null);
for (Sc sc:scs){
System.out.println("课程编号为"+sc.getCno()+"的课:");
for(Student student:sc.getStudents()){
System.out.println("学生姓名为:"+student.getSname()+"学生班级为:"+student.getClassno());
}
}
}
}



