MyBatis--关联查询
当查询内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表间的关联关系的不同,关联查询分为四种:
- 一对一关联查询
- 一对多关联查询
- 多对一关联查询
- 多对多关联查询
一、一对多关联查询
一对多关联查询是指,在查询一对象的时候,同时将其所关联的多放对象也都查询出来。
例:国家Country与部长Minister间的一对多关系
1.定义实体
若定义的是双向关联,即双方的属性中均有对方的对象作为作用域属性出现,那么它们在定义个各自的toString()方法时需要注意,只让某一方可以输出另一方即可,不要让双方的toString()方法均可输出对方。这样会形成递归调用,程序出错。
Country:
public class Country {
private Integer cid;
private String cname;
//关联属性
private Set ministers;
}
Minister:
public class Minister {
private Integer mid;
private String mname;
}
2.定义数据库表
3.定义dao接口
public interface ICountyrDao {
Country selectCountryById(int cid);
}
4.测试类:
public class MyTest {
private ICountryDao dao;
private SqlSession session;
@Before
public void setUp(){
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(ICountryDao.class);
}
@After
public void tearDown(){
if(session!=null){
session.close();
}
}
@Test
public void test01(){
Country country = dao.selectCountryById(2);
System.out.println(country);
}
}
5.映射文件
方式一:多表连接查询方式:
注意,此时即使字段名与属性名相同,在
中也要写出他们的映射关系。因为框架是依据 封装对象的。
在映射文件中使用
- property:指定关联属性,即Country类中的集合属性
- ofType:集合属性的泛型类型
方式二:多表单独查询方式:
多表连接查询方式是将多张表进行连接,连为一张表后进行查询。其查询的本质是一张表。而多表单独查询方式是多张表各自查询各自的相关内容,需要多张表的联合数据,则将主表的查询结果联合其他表的查询结果,封装为一个对象。
多个查询时可以跨越多个映射文件的,即是可以跨越多个namespace的。在使用其它namespace的查询时,添加上其所在的namespace即可。
手动sql语句过程:
select cid,cname from country where cid=2
select mid,mname from minister where countryId=2
关联属性
二、多对一关联查询
这里的多对一关联查询是指,在查询多方对象的时候,同时将其所关联的一方对象也查询出来。
由于查询多方对象时也是一个一个查询,所以多对一关联查询,其实就是一对一关联查询。即一对一关联查询的实现方式与多对一的实现方式是相同的。
例:部长Minister与国家Country间的多对一关系
1.实体类:
Minister:
public class Minister {
private Integer mid;
private String mname;
//关联属性
private Country country;
}
Country:
public class Country {
private Integer cid;
private String cname;
}
2.dao接口:
public interface IMinisterDao {
Minister selectMinisterById(int mid);
}
3.测试类:
public class MyTest {
private IMinisterDao dao;
private SqlSession session;
@Before
public void before(){
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(IMinisterDao.class);
}
@After
public void tearDown(){
if(session!=null){
session.close();
}
}
@Test
public void test01(){
Minister minister = dao.selectMinisterById(2);
System.out.println(minister);
}
}
映射文件:
方式一:多表连接查询方式:
注意,在映射文件中使用
- property:指定关联属性,即Minister类中的country属性
- javaType:关联属性的类型
方式二:多表单独查询方式:
手动写sql过程:
select mid,mname,countryId from minister where mid=2
select cid,cname from country where cid=1
三、自关联查询
所谓自关联查询是指,自己即充当一方,又充当多方,是1:n或n:1的变型。例如,对于新闻栏目NewsLabel,可以充当一方,即父栏目,也可以充当多方,即子栏目。而反映到DB表中,只有一张表,这张表中具有一个外键,用于表示该栏目的父栏目。一级栏目没有父栏目,所以可以将其外键值设为0,而子栏目则具有外键值。
将自关联分为两种情况。一种是当作1:n,即当前类作为一方,其包含多方的集合域属性。一种是当作n:1,即当前类作为多方,其包含一方的域属性。
1.自关联的DB表
手动查询:
2.以一对多方式处理
以一对多方式处理,即一方可以看到多方。该处理方式的应用场景比较多。例如页面点击父栏目,显示出其子栏目。将鼠标定位在窗口中的某菜单项上会显示其所有子菜单项等。
查询指定栏目的所有子孙栏目
根据指定的id,仅查询出其所有子栏目。包括其所有辈分的孙子栏目。即给出的插叙id实际为父栏目id
a.实体类
//新闻栏目:当前的新闻栏目被看作是一方,即父栏目
public class NewsLabel {
private Integer id;
private String name; //栏目名称
private Set children;
}
b.dao接口
public interface INewsLabelDao {
List selectChildrenByParent(int pid);
}
c.mapper映射
通过select语句的递归调用实现查询所有下级栏目的功能。查询结果的集合数据
d.测试类
public class MyTest {
private INewsLabelDao dao;
private SqlSession session;
@Before
public void before(){
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(INewsLabelDao.class);
}
@After
public void tearDown(){
if(session!=null){
session.close();
}
}
@Test
public void test01(){
List children = dao.selectChildrenByParent(1);
for (NewsLabel newsLabel : children) {
System.out.println(newsLabel);
}
}
}
查询指定栏目及其所有子孙栏目
查询结果既要包含指定id的当前栏目,还包含其所有辈分的孙子栏目。即给出的id实际为当前要查询的栏目的id。
a.dao接口:
public interface INewsLabelDao {
//List selectChildrenByParent(int pid);
NewsLabel selectNewsLabelById(int id);
}
b.mapper映射文件
c.测试类
@Test
public void test02(){
NewsLabel newslabel = dao.selectNewsLabelById(1);
System.out.println(newslabel);
}
3.以多对一方式处理
多对一方式处理,即多方可以看到一方。该处理方式的应用场景,例如在网页上显示当前页面的站内位置
a.实体类
//新闻栏目:当前的新闻栏目被看作是多方,即子栏目
public class NewsLabel {
private Integer id;
private String name; //栏目名称
private NewsLabel parent;
}
b.dao接口
public interface INewsLabelDao {
//List selectChildrenByParent(int pid);
NewsLabel selectNewsLabelById(int id);
}
c.mapper映射:
d.测试类
@Test
public void test02(){
NewsLabel newslabel = dao.selectNewsLabelById(1);
System.out.println(newslabel);
}
四、多对多关联查询
多对多关联关系,例如一个学生可以选多门课程,而一门课程可以由多个学生选择。多对多关系,其实是由两个互反的一对多关系组成。一般情况下,多对多关系都会通过一个中间表来建立,例如选课表。
1.定义实体:
在定义双向关联(双方均可看到对方的关联关系)的实体的toString方法时,只让一方的toString方法中输出对方,不要让双方均可输出对方。否则将会出现输出时的递归现象。
Student:
public class Student {
private Integer sid;
private String sname;
private Set courses;
}
Course:
public class Course {
private Integer cid;
private String cname;
private Set students;
}
2.数据库表:
3.dao接口
public interface IStudentDao {
Student selectStudentById(int sid);
}
4.mapper映射
多对多关联关系也是通过映射文件
5.测试类:
public class MyTest {
private IStudentDao dao;
private SqlSession session;
@Before
public void before(){
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(IStudentDao.class);
}
@After
public void tearDown(){
if(session!=null){
session.close();
}
}
@Test
public void test01(){
Student student = dao.selectStudentById(1);
System.out.println(student);
}
}