一、关联查询

当查询的内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表间的关联关系的不同。关联查询分为四种:

  1. 一对一关联查询;
  2. 一对多关联查询;
  3. 多对一关联查询;
  4. 多对多关联查询;

二、一对多关联查询

1、通过多表连接查询方式实现

    定义实体类及DB表

一对一关联查询,其解决方案与多对一解决方案是相同的。

一对多关联查询:在查询一方对象的时候,同时将其所关联的多方对象也都查询出来。

Demo中使用国家(country)和部长(minister)进行示例。

 public class Minister {
private Integer mid;
private String mname; public Integer getMid() {
return mid;
} public void setMid(Integer mid) {
this.mid = mid;
} public String getMname() {
return mname;
} public void setMname(String mname) {
this.mname = mname;
} @Override
public String toString() {
return "Minister [mid=" + mid + ", mname=" + mname + "]";
} }

Minister

 import java.util.Set;

 public class Country {
private Integer cid;
private String cname;
// 关联属性
private Set<Minister> ministers;// 一般用set public Integer getCid() {
return cid;
} public void setCid(Integer cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public Set<Minister> getMinisters() {
return ministers;
} public void setMinisters(Set<Minister> ministers) {
this.ministers = ministers;
} @Override
public String toString() {
return "Country [cid=" + cid + ", cname=" + cname + ", ministers=" + ministers + "]";
} }

Country

这里关联属性,一般一对多,用Set。(Set不能重复,list和array)。

数据库创建contry表和minister表

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.BasicConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.jmu.bean.Country;
import com.jmu.dao.ICountryDao;
import com.jmu.utils.MybatisUtils; public class MyTest {
private ICountryDao dao;
private SqlSession sqlSession; @Before
public void Before() {
sqlSession = MybatisUtils.getSqlSession();
dao = sqlSession.getMapper(ICountryDao.class);
BasicConfigurator.configure();
}
@After
public void after(){
if (sqlSession!=null) {
sqlSession.commit(); } } @Test
public void test01() {
Country country = dao.selectCountryById(2);
System.out.println(country);
} }

com.jmu.test.MyTest

 public interface ICountryDao {
Country selectCountryById(int cid);
}

com.jmu.dao.ICountryDao

对应实体类,Mapper.xml中

 <mapper namespace="com.jmu.dao.ICountryDao">
<resultMap type="Country" id="countryMapper">
<id column="cid" property="cid" />
<result column="cname" property="cname" />
<collection property="ministers" ofType="Minister"><!-- ofType="Minister",要封装的类,也是集合的泛型 -->
<id column="mid" property="mid" />
<result column="mname" property="mname" />
</collection>
</resultMap>
<select id="selectCountryById" resultMap="countryMapper">
select cid,cname,mid ,mname
from country,minister
where countryId=cid and cid=#{xxx}
</select>
</mapper>

/mybatis8-one2many/src/com/jmu/dao/mapper.xml

输出:

0 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById  - ==>  Preparing: select cid,cname,mid ,mname from country,minister where countryId=cid and cid=?
57 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById - ==> Parameters: 2(Integer)
96 [main] TRACE com.jmu.dao.ICountryDao.selectCountryById - <== Columns: cid, cname, mid, mname
96 [main] TRACE com.jmu.dao.ICountryDao.selectCountryById - <== Row: 2, England, 4, ddd
99 [main] TRACE com.jmu.dao.ICountryDao.selectCountryById - <== Row: 2, England, 5, eee
99 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById - <== Total: 2
Country [cid=2, cname=England, ministers=[Minister [mid=4, mname=ddd], Minister [mid=5, mname=eee]]]

output

2、通过多表单独查询方式实现

 <mapper namespace="com.jmu.dao.ICountryDao">
<select id="selectMinisterByCountry" resultType="Minister">
select mid,mname from minister where countryId=#{ooo}
</select>
<resultMap type="Country" id="countryMapper">
<id column="cid" property="cid" />
<result column="cname" property="cname" />
<collection property="ministers" ofType="Minister"
select="selectMinisterByCountry" column="cid" /><!-- ofType="Minister",要封装的类,也是集合的泛型 -->
</resultMap>
<select id="selectCountryById" resultMap="countryMapper">
select cid,cname from
country where cid=#{xxx}
</select>
</mapper>

/mybatis8-one2many2-2/src/com/jmu/dao/mapper.xml

 0 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById  - ==>  Preparing: select cid,cname from country where cid=?
45 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById - ==> Parameters: 2(Integer)
79 [main] TRACE com.jmu.dao.ICountryDao.selectCountryById - <== Columns: cid, cname
80 [main] TRACE com.jmu.dao.ICountryDao.selectCountryById - <== Row: 2, England
84 [main] DEBUG com.jmu.dao.ICountryDao.selectMinisterByCountry - ====> Preparing: select mid,mname from minister where countryId=?
85 [main] DEBUG com.jmu.dao.ICountryDao.selectMinisterByCountry - ====> Parameters: 2(Integer)
91 [main] TRACE com.jmu.dao.ICountryDao.selectMinisterByCountry - <==== Columns: mid, mname
92 [main] TRACE com.jmu.dao.ICountryDao.selectMinisterByCountry - <==== Row: 4, ddd
93 [main] TRACE com.jmu.dao.ICountryDao.selectMinisterByCountry - <==== Row: 5, eee
94 [main] DEBUG com.jmu.dao.ICountryDao.selectMinisterByCountry - <==== Total: 2
95 [main] DEBUG com.jmu.dao.ICountryDao.selectCountryById - <== Total: 1
Country [cid=2, cname=England, ministers=[Minister [mid=4, mname=ddd], Minister [mid=5, mname=eee]]]

output

3、自关联查询

所谓自关联是指,自己既充当一方,又充当多方,是1:n或n:1的变型。

下面以新闻栏目为例

(1)自关联的DB表

 //新闻栏目:当前的新闻栏目被看作是一方,即父栏目

 import java.util.Set;

 public class NewLabel{
private Integer id;
private String name;// 栏目名称
private Set<NewLabel> children; 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 Set<NewLabel> getChildren() {
return children;
} public void setChildren(Set<NewLabel> children) {
this.children = children;
} @Override
public String toString() {
return "NewLable [id=" + id + ", name=" + name + ", children=" + children + "]";
} }

com.jmu.bean.NewLabel

(2)以一对多方式处理

  • 查询指定栏目的所有子孙栏目

新闻栏目:当前的新闻栏目被看作是一方,即父栏目

 import com.jmu.bean.NewsLabel;

 public interface INewsLabelDao {
List<NewsLabel> selectChildrenByParent(int pid) ;
}

com.jmu.dao.INewsLabelDao

 @Test
public void test01() {
List<NewsLabel> children=dao.selectChildrenByParent(2);
for (NewsLabel newLabel : children) {
System.out.println(newLabel);
}
}

MyTest

 <mapper namespace="com.jmu.dao.INewsLabelDao">
<!-- <select id="selectChildrenByParent" resultMap="newslabelMapper">
select id,name from newslabel where pid=#{ooo}
</select> -->
<resultMap type="NewsLabel" id="newslabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="children"
ofType="NewsLabel"
select="selectChildrenByParent"
column="id"
></collection>
</resultMap>
<select id="selectChildrenByParent" resultMap="newslabelMapper">
select id,name from newslabel where pid=#{xxx}
</select>
</mapper>

/mybatis9-oneself-one2many/src/com/jmu/dao/mapper.xml

输出:

0 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent  - ==>  Preparing: select id,name from newslabel where pid=?
47 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ==> Parameters: 2(Integer)
83 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <== Columns: id, name
83 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <== Row: 3, NBA
87 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ====> Preparing: select id,name from newslabel where pid=?
88 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ====> Parameters: 3(Integer)
89 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Columns: id, name
89 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Row: 5, 火箭
89 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Preparing: select id,name from newslabel where pid=?
89 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Parameters: 5(Integer)
90 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <====== Total: 0
91 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Row: 6, 湖人
92 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Preparing: select id,name from newslabel where pid=?
92 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Parameters: 6(Integer)
93 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <====== Total: 0
93 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Total: 2
94 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <== Row: 4, CBA
95 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ====> Preparing: select id,name from newslabel where pid=?
95 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ====> Parameters: 4(Integer)
96 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Columns: id, name
96 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Row: 7, 北京金隅
97 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Preparing: select id,name from newslabel where pid=?
97 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Parameters: 7(Integer)
98 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <====== Total: 0
98 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Row: 8, 浙江广厦
98 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Preparing: select id,name from newslabel where pid=?
98 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Parameters: 8(Integer)
99 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <====== Total: 0
99 [main] TRACE com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Row: 9, 青岛双星
100 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Preparing: select id,name from newslabel where pid=?
101 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - ======> Parameters: 9(Integer)
102 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <====== Total: 0
102 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <==== Total: 3
102 [main] DEBUG com.jmu.dao.INewsLabelDao.selectChildrenByParent - <== Total: 2
NewLable [id=3, name=NBA, children=[NewLable [id=5, name=火箭, children=[]], NewLable [id=6, name=湖人, children=[]]]]
NewLable [id=4, name=CBA, children=[NewLable [id=9, name=青岛双星, children=[]], NewLable [id=7, name=北京金隅, children=[]], NewLable [id=8, name=浙江广厦, children=[]]]]

output

  • 查询指定栏目及其所有子孙栏目

这里的查询结果,即要包含指定id的当前栏目,还包含其所有子孙栏目。

 import com.jmu.bean.NewsLabel;

 public interface INewsLabelDao {
NewsLabel selectNewsLabelById(int id);
}

com.jmu.dao.INewsLabelDao

 @Test
public void test01() {
NewsLabel newsLabel=dao.selectNewsLabelById(2);
System.out.println(newsLabel);
}

MyTest

 <mapper namespace="com.jmu.dao.INewsLabelDao">
<select id="selectNewslabelByParent" resultMap="newslabelMapper">
select id,name from newslabel where pid=#{ooo}
</select>
<resultMap type="NewsLabel" id="newslabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="children"
ofType="NewsLabel"
select="selectNewslabelByParent"
column="id"
></collection>
</resultMap>
<select id="selectNewsLabelById" resultMap="newslabelMapper">
select id,name from newslabel where id=#{xxx}
</select>
</mapper>

mapper.xml

输出:

0 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById  - ==>  Preparing: select id,name from newslabel where id=?
48 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ==> Parameters: 2(Integer)
96 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Columns: id, name
97 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Row: 2, 体育新闻
101 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ====> Preparing: select id,name from newslabel where pid=?
105 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ====> Parameters: 2(Integer)
106 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <==== Columns: id, name
106 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <==== Row: 3, NBA
107 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ======> Preparing: select id,name from newslabel where pid=?
107 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ======> Parameters: 3(Integer)
108 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Columns: id, name
108 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Row: 5, 火箭
109 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Preparing: select id,name from newslabel where pid=?
109 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Parameters: 5(Integer)
110 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <======== Total: 0
111 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Row: 6, 湖人
113 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Preparing: select id,name from newslabel where pid=?
114 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Parameters: 6(Integer)
115 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <======== Total: 0
115 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Total: 2
115 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <==== Row: 4, CBA
116 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ======> Preparing: select id,name from newslabel where pid=?
116 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ======> Parameters: 4(Integer)
117 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Columns: id, name
117 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Row: 7, 北京金隅
117 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Preparing: select id,name from newslabel where pid=?
118 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Parameters: 7(Integer)
118 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <======== Total: 0
119 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Row: 8, 浙江广厦
119 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Preparing: select id,name from newslabel where pid=?
120 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Parameters: 8(Integer)
121 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <======== Total: 0
121 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Row: 9, 青岛双星
123 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Preparing: select id,name from newslabel where pid=?
124 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - ========> Parameters: 9(Integer)
125 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <======== Total: 0
125 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <====== Total: 3
126 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewslabelByParent - <==== Total: 2
126 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Total: 1
NewLable [id=2, name=体育新闻, children=[NewLable [id=4, name=CBA, children=[NewLable [id=7, name=北京金隅, children=[]], NewLable [id=9, name=青岛双星, children=[]], NewLable [id=8, name=浙江广厦, children=[]]]], NewLable [id=3, name=NBA, children=[NewLable [id=5, name=火箭, children=[]], NewLable [id=6, name=湖人, children=[]]]]]]

output

(3)以多对一方式实现

  • 查询当前栏目及其所有父辈栏目

新闻栏目:当前的新闻栏目被看作是多方,即子栏目

 //新闻栏目:当前的新闻栏目被看作是多方,即子栏目
public class NewsLabel{
private Integer id;
private String name;// 栏目名称
private NewsLabel parent;//父栏目
// private Set<NewsLabel> children;//子栏目,完整自关联
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 NewsLabel getParent() {
return parent;
} public void setParent(NewsLabel parent) {
this.parent = parent;
} @Override
public String toString() {
return "NewsLabel [id=" + id + ", name=" + name + ", parent=" + parent + "]";
} }

com.jmu.bean.NewsLabel

 public interface INewsLabelDao {
NewsLabel selectNewsLabelById(int id);
}

com.jmu.dao.INewsLabelDao

 public void test01() {
NewsLabel newsLabel=dao.selectNewsLabelById(3);
System.out.println(newsLabel);
}

MyTest

 <mapper namespace="com.jmu.dao.INewsLabelDao">
<!-- <select id="selectNewslabelByParent" resultMap="newslabelMapper">
select id,name,pid from newslabel where id=#{ooo}
</select> -->
<resultMap type="NewsLabel" id="newslabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="parent"
javaType="NewsLabel"
select="selectNewsLabelById"
column="pid"></association>
</resultMap>
<select id="selectNewsLabelById" resultMap="newslabelMapper">
select id,name,pid from newslabel where id=#{xxx}
</select>
</mapper>

mapper.xml

输出:

 0 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById  - ==>  Preparing: select id,name,pid from newslabel where id=?
57 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ==> Parameters: 3(Integer)
104 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Columns: id, name, pid
108 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Row: 3, NBA, 2
111 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ====> Preparing: select id,name,pid from newslabel where id=?
112 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ====> Parameters: 2(Integer)
112 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <==== Columns: id, name, pid
113 [main] TRACE com.jmu.dao.INewsLabelDao.selectNewsLabelById - <==== Row: 2, 体育新闻, 0
113 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ======> Preparing: select id,name,pid from newslabel where id=?
113 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - ======> Parameters: 0(Integer)
114 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - <====== Total: 0
115 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - <==== Total: 1
115 [main] DEBUG com.jmu.dao.INewsLabelDao.selectNewsLabelById - <== Total: 1
NewsLabel [id=3, name=NBA, parent=NewsLabel [id=2, name=体育新闻, parent=null]]

output

4、多对多关联查询

例如:一个学生可以选多门课程,而一门课程可以由多门学生选。

一般情况下,多对多关心都会通过一个中间表来建立。例如选课表。

创建DB表和实体类:

middle表中有外键,为多方,即中间表为多方

  

 import java.util.Set;

 public class Course {
private Integer cid;
private String cname;
private Set<Student> students; public Integer getCid() {
return cid;
} public void setCid(Integer cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} @Override
public String toString() {
return "Course [cid=" + cid + ", cname=" + cname + ", students=" + students + "]";
} }

com.jmu.bean.Course

 import java.util.Set;

 public class Student {
private Integer sid;
private String sname;
private Set<Course> courses; public Integer getSid() {
return sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public Set<Course> getCourses() {
return courses;
} public void setCourses(Set<Course> courses) {
this.courses = courses;
} @Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", courses=" + courses + "]";
} }

com.jmu.bean.Student

 import com.jmu.bean.Student;

 public interface IStudentDao {
Student selectStudentById(int sid);
}

com.jmu.dao.IStudentDao

 @Test
public void test01() {
Student student = dao.selectStudentById(1);
System.out.println(student);
}

MyTest

 <mapper namespace="com.jmu.dao.IStudentDao">
<resultMap type="Student" id="studentMapper">
<id column="sid" property="sid" />
<result column="sname" property="sname" />
<collection property="courses" ofType="Course">
<id column="cid" property="cid" />
<result column="cname" property="cname" />
</collection>
</resultMap>
<select id="selectStudentById" resultMap="studentMapper">
select sid,sname,cid,cname
from student1,middle,course
where sid=studentId and cid=courseId and sid=#{xxx}
</select>
</mapper>

mapper.xml

输出:

0 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById  - ==>  Preparing: select sid,sname,cid,cname from student1,middle,course where sid=studentId and cid=courseId and sid=?
69 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - ==> Parameters: 1(Integer)
101 [main] TRACE com.jmu.dao.IStudentDao.selectStudentById - <== Columns: sid, sname, cid, cname
102 [main] TRACE com.jmu.dao.IStudentDao.selectStudentById - <== Row: 1, 刘备, 1, JavaSE
112 [main] TRACE com.jmu.dao.IStudentDao.selectStudentById - <== Row: 1, 刘备, 2, JavaEE
112 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentById - <== Total: 2
Student [sid=1, sname=刘备, courses=[Course [cid=1, cname=JavaSE, students=null], Course [cid=2, cname=JavaEE, students=null]]]

output

MyBatis_关联关系查询的更多相关文章

  1. mybatis_映射查询

    一.一对一映射查询: 第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段.此方法较为简单,企业中使用普遍. <!- ...

  2. MyBatis_多查询条件问题

    一.多查询条件无法整体接收问题的解决 在实际工作中,表单中所给出的查询条件有时是无法将其封装成一个对象,即查询方法只能携带多个参数,而不能携带将这多个参数进行封装的一个对象.对于这个问题,有两种解决方 ...

  3. hibernate关联关系查询

    关联关系 一对一 A中包含B的对象,B中包含A的对象 一对多 A中包含B的集合,B中包含A的对象 多对多 A中包含B的集合,B中包含A的集合 1,一对多配置 一名老师可以对应多名学生 2,模型类 老师 ...

  4. dapper关联关系查询小测试

    测试实体类(表结构) public class User { public int user_id { get; set; } public string user_name { get; set; ...

  5. mybatis 关联关系查询 java

    <mapper namespace="com.rrz.modules.awardrecord.dao.CheckAwardsDao"> <resultMap ty ...

  6. select 嵌套查询

    1. SELECT语句的子查询 语法:     SELECT ... FROM (subquery) AS name ... 先创建一个表: CREATE TABLE t1 (s1 INT, s2 C ...

  7. JPA学习笔记(8)——映射双向一对多关联关系

    双向一对多关联关系 前面的博客讲的都是单向的,而本问讲的是双向的(双向一对多 = 双向多对一) 什么是双向? 我们来对照一下单向和双向 单向/双向 User实体类中是否有List< Order& ...

  8. Django 多表查询

    多表查询是模型层的重要功能之一, Django提供了一套基于关联字段独特的解决方案. ForeignKey 来自Django官方文档的模型示例: from django.db import model ...

  9. SpringBoot Data JPA 关联表查询的方法

    SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...

随机推荐

  1. 深度学习之tensorflow (一)

    一.TensorFlow简介 1.TensorFlow定义: tensor  :张量,N维数组 Flow   :  流,基于数据流图的计算 TensorFlow : 张量从图像的一端流动到另一端的计算 ...

  2. HTML基础--元素类型及类型转换

    元素类型及类型转换 一.XHTML元素分类 根据css显示分类,XHTML元素被分为三种类型:块状元素,内联元素,可变元素 1.块状元素(block element) 1)块状元素在网页中就是以块的形 ...

  3. C++静态库与动态库(转)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0cAAAC6CAIAAADqPKGOAAAgAElEQVR4nO2dy5WrOhREFYgjcAQ9Iw

  4. Linux运维正则表达式之grep

    一.什么是正则表达式?简单的说,正则表达式就是一套处理大量的字符串而定义的规则和方法.例如:假设 @代表12345通过正则表达式这些特殊符号,我们可以快速过滤.替换需要的内容.linux正则表达式一般 ...

  5. Django 入门案例开发(中)

    昨天已经描述了如何搭建Django的开发环境,今天描述业务流程,具体我们要实现一个什么样的业务: 以下的业务都是假设的(网上书店   页面做的low): 1.用户注册及登录业务: 这是一个网上书店阅读 ...

  6. [树莓派(raspberry pi)] 01、在linux环境下给树莓派安装系统及入门各种资料

    1.预准备 进入帮助页面https://www.raspberrypi.org/help/,可以直接看到GET START WITH PASPBERRY PI,点击进入: 接下来会引导你检查配件是否齐 ...

  7. FreeMarker 快速入门

    FreeMarker 快速入门 FreeMarker是一个很值得去学习的模版引擎.它是基于模板文件生成其他文本的通用工具.本章内容通过如何使用FreeMarker生成Html web 页面 和 代码自 ...

  8. h5前端流行的框架

    很多时候别人问你,上手的框架有哪些,其实我们都是知道的,只是一时却也说不上哪些比较,这里想给大家介绍一下,我所遇到的,还算好用的框架,做个分享 1 Bootstrap 官网:http://getboo ...

  9. ShoneSharp语言(S#)的设计和使用介绍系列(3)— 修炼工具

    ShoneSharp语言(S#)的设计和使用介绍 系列(3)- 修炼工具 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. ...

  10. Spring MVC 学习总结(八)——Spring MVC概要与环境配置(IDEA+Maven+Tomcat7+JDK8、示例与视频)

    一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...