一、关联查询
当查询的内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表间的关联关系的不同。关联查询分为四种:
- 一对一关联查询;
- 一对多关联查询;
- 多对一关联查询;
- 多对多关联查询;
二、一对多关联查询
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