MyBatis2——ResultMap的association和collection

ResultMap简介

之前学的简单查询我们可以有实体类来映射,那如果我们没有一个匹配的类来接收呢?答:这时,如果有个容器来接受这些数据该多好!ResultMap就可以做到,它可以将映射变得更加复杂和灵活。

ResultMap配置文件粗略介绍(后面详解association和collection)

<mapper namespace="">
 	<resultMap id="唯一标识" type="java对象">
 		<id property="与数据库主键字段相对应的对象的属性" column="数据库的主键字段" jdbcTyp="字段类型"/>
 		<!--
			若干<result></result>
		-->
 		<result property="映射对象属性" column="查询数据库结果字段"/>
		<!--
			<association></association>或者是<collection></collection>
		-->
	</resultMap>
	
	<!--这里的resultMap=""就是上面resultMap的id-->
	<select id="" resultMap="">  
		<!--SQL语句-->
	</select>	
</mapper>

association介绍

association主要是进行一对一或者多对一嵌套查询

<mapper namespace="">
 	<resultMap id="" type="">
 		<!--这一部分的查询结果是含主键的表的字段-->
 		<id property="主键映射对象属性" column="数据库主键字段" jdbcTyp=""/>
 		<result property="" column=""/></resultMap>
 		
		<!--associtaion里面是含外键的表的字段-->
		<association proper="外键映射对象" javaType="">
			<id property="外键映射对象属性" column="数据库外键字段"/>
			<result property="" column=""></result>
		</association>
	<select id="" resultMap=""> </select>	
</mapper>

association实例

想必上面讲了肯定还是有点迷惑,现在以电影角色和演员一一对应关系为例,进行查询。
其中两个数据表,演员表和角色表,两者关系如下:
在这里插入图片描述
mysql建表及插入语句如下:

drop table if exists cast;
drop table if exists role;
create table cast
(
   c_name               varchar(32) not null,
   primary key (c_name)
);
create table role
(
   c_name               varchar(32) not null,
   r_name               varchar(32)
);
alter table role add constraint FK_Relationship_2 foreign key (c_name)
      references cast (c_name) on delete restrict on update restrict;
insert into cast values('本.阿弗莱克');
insert into cast values('亨利卡维尔');
insert into cast values('盖尔加朵');
insert into role values('本.阿弗莱克','蝙蝠侠');
insert into role values('亨利卡维尔','超人');
insert into role values('盖尔加朵','神奇女侠');

实体类

get()set()省略
public class Cast {
    
    
    private String c_name;
    private Role role;
    public Cast(){
    
    }
    @Override
    public  String toString()
    {
    
    
        return c_name+" 扮演了 "+role;
    }
}
public class Role {
    
    
    private String c_name;
    private String r_name;
    @Override
    public String toString()
    {
    
    
        return r_name;
    }
}

实例Mapper配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.MyBatisDemo.RM">
    <resultMap id="castResultMap" type="com.MyBatisDemo.RM.Cast">
        <id property="c_name" column="cname"/>
        <result property="c_name" column="cname"/>
        <association property="role" javaType="com.MyBatisDemo.RM.Role">
            <id property="c_name" column="crname"/>
            <result property="r_name" column="rname"/>
        </association>
    </resultMap>
    <select id="query" resultMap="castResultMap">
        select
            a.c_name as cname,
            b.c_name as crname,
            b.r_name as rname
        from cast a join role b on a.c_name=b.c_name where a.c_name=#{value}
    </select>
</mapper>

实例测试

/***
     * association一对一查询,查询演员在这部电影中演了什么角色
     * @throws Exception
     */
    @Test
    public void associationTest() throws Exception
    {
    
    
        SqlSession sqlSession=connection.getSqlSession();
        Cast cast=sqlSession.selectOne("query","盖尔加朵");
        System.out.println(cast);
    }

在这里插入图片描述

collection介绍

collection是集合的嵌套查询,直白点是进行一对多查询。

collection实例

这里以员工和部门的关系为实例进行讲解
在这里插入图片描述
我们想查询某部门下面的员工信息怎么办呢?部门与员工是一对多的关系,因此不能采用association。在java里面表示一个部门下有多个员工是用数组来表示。员工和部门之间通过部门id来进行关联,那么做法就是我先找到部门id,然后再把部门id传给员工表找到属于该部门的员工,将员工以集合的形式返回。

collection实例配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.MyBatisDemo.RM">
    <resultMap id="depRes" type="com.MyBatisDemo.RM.Dep">
        <!--<id property="d_id" column="d_id"/>
        <result property="s_name" column="s_name"/>
        <result property="s_id" column="s_id"/>-->
        <collection property="staffs" javaType="java.util.List" ofType="com.MyBatisDemo.RM.Staff"
                    select="queryStaff" column="d_id"/>
    </resultMap>
   <select id="queryDep" resultMap="depRes">
       select d_id,d_name from dep where d_id=#{value}
   </select>
    <select id="queryStaff" resultType="com.MyBatisDemo.RM.Staff">
        select s_id,s_name from staff where d_id=#{value}
    </select>
</mapper>

  上面的xml我们有两个select,一个是"queryDep",一个是"queryStaff"。这里先介绍简单的queryStaff,它的作用就是来查询对应部门下的员工信息。queryDep由于返回类型比较复杂,所以用resultMap,它的select语句比较简单——通过部门id查找部门信息。
  这里着重讲解resultMap里面collection的内容,它的property对应的是Dep类里面的数组,因为我们最后要返回就是员工信息的集合嘛。ofType是映射到集合属性中pojo的类型。select=""里面就是集合里面查找的方法,column里面就是两者之间的关联属性。

实体类

get()set()省略
/***
 * 职员实体类
 * 一个职员属于一个部门
 */
public class Staff {
    
    
    private String s_id;
    private String s_name;
    private String d_id;
    
}
```java
/***
 * 部门实体类
 * 一个部门下面有个员工
 */
public class Dep {
    
    
    private String d_id;
    private String d_name;
    private List<Staff> staffs;
}

sql语句

drop table if exists dep;
drop table if exists staff;
create table dep
(
   d_id                 varchar(32) not null,
   d_name               varchar(32),
   primary key (d_id)
);
create table staff
(
   s_id                 varchar(32) not null,
   d_id                 varchar(32) not null,
   s_name               varchar(32),
   primary key (s_id)
);
alter table staff add constraint FK_Relationship_1 foreign key (d_id)
      references dep (d_id) on delete restrict on update restrict;
insert into dep values('1','人力部门');
insert into dep values('2','996部门');
insert into staff values('1','1','张美丽');
insert into staff values('2','2','李二狗');
insert into staff values('3','2','王明明');

实例测试

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41988893/article/details/104944459