版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxdeng/article/details/82898668
1. 一对一关联查询
查询丈夫以及丈夫所属的妻子信息
CREATE table husband(
husbandId int primary key auto_increment,
husbandName varchar(20)
);
create table wife(
wifeId int primary key auto_increment,
husbandId int not null,
wifeName varchar(20)
);
insert into husband(husbandName) values('诸葛亮');
insert into husband(husbandName) values('习近平');
insert into husband(husbandName) values('刘邦');
insert into wife(husbandId,wifeName) values(1,'黄月英');
insert into wife(husbandId,wifeName) values(2,'彭丽媛');
insert into wife(husbandId,wifeName) values(3,'王氏');
1.1 嵌套结果(通过连接语句来查询)
@requires_authorization
<select id="findHusband" parameterType="int" resultMap="getHusband">
select h.husbandId,h.husbandName,w.wifeId,w.wifeName
from husband as h inner join wife as w on h.husbandId = w.husbandId
where h.husbandId = #{husbandId}
</select>
<resultMap type="com.hx.pojo.Husband" id="getHusband">
<id property="husbandId" column="husbandId" />
<result property="husbandName" column="husbandName" />
<!-- property 实体类定义属性名称,javaType 一的一方类路径 -->
<association property="wife" javaType="com.hx.pojo.Wife">
<id property="wifeId" column="wifeId" />
<result property="wifeName" column="wifeName" />
</association>
</resultMap>
1.2 嵌套查询(执行多条SQL语句)
<select id="findHusband2" parameterType="int" resultMap="findHusbandMap">
select * from husband where husbandId = #{husbandId}
</select>
<select id="findWife" parameterType="int" resultType="com.hx.pojo.Wife">
select * from wife where wifeId = #{wifeId}
</select>
<resultMap type="com.hx.pojo.Husband" id="findHusbandMap">
<id property="husbandId" column="husbandId" />
<result property="husbandName" column="husbandName" />
<!-- property 实体类定义属性名称,column 外键列,select 查询语句的别名 -->
<association property="wife" column="husbandId" select="findWife" />
</resultMap>
2. 一对多关联
查询班级以及班级对应的学生信息
create table class(
classId int primary key auto_increment,
className varchar(20)
);
create table student(
studentId int primary key auto_increment,
classId int not null,
name varchar(20)
);
insert into class(className) values('一年级三班');
insert into student(classId,name) values(1,'马皓轩');
insert into student(classId,name) values(1,'宋佳');
insert into student(classId,name) values(1,'安淇尔');
insert into student(classId,name) values(1,'王梓璇');
insert into student(classId,name) values(1,'李昊煜');
2.1 嵌套结果(通过连接语句来查询)
<select id="findPrimaryKey" parameterType="int" resultMap="findClassPrimaryKey">
SELECT c.classId,c.className,t.studentId,t.name from class as c inner join student as t on t.classId = c.classId where c.classId = #{id}
</select>
<resultMap type="com.hx.pojo.Classes" id="findClassPrimaryKey">
<id property="classId" column="classId" />
<result property="className" column="className" />
<!--
property 实体类定义集合属性名称,ofType 多的一方类路径
-->
<collection property="list" ofType="com.hx.pojo.Student">
<id property="studentId" column="studentId" />
<result property="name" column="name" />
</collection>
</resultMap>
2.2 嵌套查询(执行多条SQL语句)
<select id="findPrimaryKey3" parameterType="int" resultMap="findStudentPrimaryKye3">
SELECT * from class where classId = #{id}
</select>
<resultMap type="com.hx.pojo.Classes" id="findStudentPrimaryKye3">
<id property="classId" column="classId" />
<result property="className" column="className" />
<!--
property 实体类属性名称,ofType 多的一方类路径,column 外键列名称,select另外一条查询语句
-->
<collection property="list" ofType="com.hx.pojo.Student" column="classId" select="findStudentPrimaryKey"></collection>
<!--
<collection property="list" ofType="com.hx.pojo.Student" column="classId" select="com.hx.mybatis.dao.StudentMapper.findStudentPrimaryKey"></collection>
-->
</resultMap>
<select id="findStudentPrimaryKey" parameterType="int" resultType="com.hx.pojo.Student">
SELECT * from student where classId = #{ classId}
</select>
MyBatis 关联关系查询没有Hibernate 那么复杂,我们可以通过上面两个案例掌握一的配置方式和多的配置方式后就可以适用于多对多,多对一,一对一