Mybatis关联查询 ResultMap的继承

在引用文件中我们需要指定被引用文件的namespace+resultMap

当数据实体具有一对多,或多对多的关系时,如果需要分别编写级联获取,非级联获取的接口,为了避免定义多了ResultMap,可以使用ResultMap的extends属性来优化。

<resultMap type="com.bean.Topology" id="resultMapTopology">
    <id column="topology_pk" property="topology_pk" javaType="java.lang.Long"/>	
    <result column="topology_id" property="topology_id" javaType="java.lang.String"/>
</resultMap>
 
<resultMap type="com.bean.Topology" id="resultMapTopologyOnConnection" extends="resultMapTopology">
    <collection 
        property="topology_hostlist" 
        column="topology_pk"             
        select="com.mapper.HostMapper.getHostListByTopology_Pk" />
</resultMap>
 
<!-- 获取拓扑的全量数据 (非级联获取,不传参) -->
<select id="getTopologyList" resultMap="resultMapTopology">
	select * from odl_table_topology
</select>
	
<!-- 获取拓扑的全量数据 (级联获取,不传参)  -->
<select id="getTopologyListOnConnection" resultMap="resultMapTopologyOnConnection">
	select * from odl_table_topology
</select>

引用文件

    <select id="getRoleList" resultMap="com.demo.mymall.mbg.mapper.UmsRoleMapper.BaseResultMap">
        select r.*
        from ums_admin_role_relation ar left join ums_role r on ar.role_id = r.id
        where ar.admin_id = #{adminId}
    </select>
1

关键属性resultMap=“com.demo.mymall.mbg.mapper.UmsRoleMapper.BaseResultMap”

即被引用文件的namespace+resultMap

resultMap的定义

先来看看 resultMap 节点的官方定义:

简单的使用:

<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>复制代码

会把列名和属性名进行绑定,该节点一共有 4 个属性:

1. id :表示该 resultMap,共其他的语句调用

2. type:表示其对于的pojo类型,可以使用别名,也可以使用全限定类名

3. autoMapping:如果设置这个属性,MyBatis将会为这个ResultMap开启或者关闭自动映射。这个属性会覆盖全局的属性 autoMappingBehavior。默认值为:unset。

4. extends:继承,一个 resultMap 可以继承另一个 resultMap,这个属性是不是没有用过 ? ^^

接下来看下它可以有哪些子节点:

  • constructor - 用于注入结果到构造方法中
  • id – 标识ID列
  • result – 表示一般列
  • association – 关联查询
  • collection – 查询集合
  • discriminator - 鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为

constructor 

在查询数据库得到数据后,会把对应列的值赋值给javabean对象对应的属性,默认情况下mybatis会调用实体类的无参构造方法创建一个实体类,然后再给各个属性赋值,如果没有构造方法的时候,可以使用 constructor 节点进行绑定,如现有如下的构造方法:

    public Person(int id, String name, String job, int age) {
        this.id = id;
        this.name = name;
        this.job = job;
        this.age = age;
    }复制代码

 则,可以使用 constructor  节点进行绑定:

    <resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <constructor>
            <idArg column="id" javaType="int"/>
            <arg column="name" javaType="string" />
            <arg column="job" javaType="string" />
            <arg column="age" javaType="int" />
        </constructor>
    </resultMap>复制代码

association 

关联查询,在级联中有一对一、一对多、多对多等关系,association主要是用来解决一对一关系的,association 可以有多种使用方式:

比如现在有一个 Person 类,它有一个 Address 属性,关联 Address 对象:

public class Person implements Serializable {

    private int id;

    private String name;

    private String job;

    private int age;

    private Address address;
}

public class Address {
    private int id;

    private String name;

    private long number;

}复制代码

关联查询方式一:

    <resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" column="address_id" javaType="mybatis.pojo.Address" select="queryAddress" />
    </resultMap>

    <select id="queryAddress" resultType="mybatis.pojo.Address">
        select * from address where id = #{id}
    </select>复制代码

关联查询方式二:

    <resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" column="address_id" javaType="mybatis.pojo.Address" resultMap="addressMap"/>
    </resultMap>
    
    <resultMap id="addressMap" type="mybatis.pojo.Address">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
    </resultMap>复制代码

关联查询方式三:

    <resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" javaType="mybatis.pojo.Address">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="number" property="number"/>
        </association>
    </resultMap>复制代码

collection 

collection 集合,如果pojo对象有一个属性是集合类型的,可以使用collection 来进行查询:

public class Person implements Serializable {

    private int id;

    private String name;

    private String job;

    private int age;

    private List<Address> addressList;
}复制代码
    <resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <collection property="addressList" javaType="ArrayList" ofType="mybatis.pojo.Address">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="number" property="number"/>
        </collection>
    </resultMap>复制代码

当然还有其他的方法,具体可以参考官网。

discriminator

鉴别器,mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为,有点像 Java的 switch 语句,鉴别器指定了 column 和 javaType 属性。 列是 MyBatis 查找比较值的地方。 JavaType 是需要被用来保证等价测试的合适类型,

比如某列的值等于多少,则返回1,等于多少返回2等等。

<resultMap id="vehicleResult" type="Vehicle">
  <id property="id" column="id" />
  <result property="vin" column="vin"/>
  <result property="year" column="year"/>
  <result property="make" column="make"/>
  <result property="model" column="model"/>
  <result property="color" column="color"/>
  <discriminator javaType="int" column="vehicle_type">
    <case value="1" resultMap="carResult"/>
    <case value="2" resultMap="truckResult"/>
    <case value="3" resultMap="vanResult"/>
    <case value="4" resultMap="suvResult"/>
  </discriminator>
</resultMap>复制代码

以上就是 resultMap 节点的全部使用方法,下面是一个比较复杂的例子,源码解析会按照其来解析,例子来自于官方文档。

<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
    <arg column="name" javaType="string" />
  </constructor>

  <id column="id" property="id" />
  <result property="title" column="blog_title"/>

  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
  </association>

  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
  </collection>
  
  <discriminator javaType="int" column="draft">
    <case value="1" resultType="DraftPost"/>
  </discriminator>
</resultMap>

1 一对多情况

实体类

方法如下,首先创建两个实体类。

public class SceneVO {
 private String sessionId;
 private List<SubSceneVO> sceneList;
 // 省略对应的getter和setter方法
}
public class SubSceneVO {
 private String sceneId;
 private String sceneName;
 // 省略对应的getter和setter方法
}

自定义Mapper和xml文件

public interface BusinessScenesCustomMapper {
 SceneVO selectBySessionId(String sessionId);
}
<?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="你的包名.mapper.BusinessScenesCustomMapper">
    <resultMap id="BaseResultMap" type="你的包名.vo.SceneVO">
        <result column="session_id" jdbcType="VARCHAR" property="sessionId"/>
    <!--
      collection 标签:用于定义关联的list集合类型的封装规则
      property:对应父类中list属性名,这里SceneVO类里的List变量名为sceneList
      ofType:集合存放的类型,List集合要装的类的类名,这里是SubSceneVO
    -->
        <collection property="sceneList" ofType="你的包名.vo.SubSceneVO">
            <result column="scene_id" jdbcType="VARCHAR" property="sceneId"/>
            <result column="scene_name" jdbcType="VARCHAR" property="sceneName"/>
        </collection>
    </resultMap>

<!-- 一个session_id对应多条记录,返回的是SceneVO对象,SceneVO对象有一个List装着SubSceneVO  -->
    <select id="selectBySessionId" parameterType="string" resultMap="BaseResultMap">
        select session_id, scene_id, scene_name
        from 表名
        where session_id = #{sessionId,jdbcType=VARCHAR}
    </select>
</mapper>
  • collection标签: 用于定义关联的List集合类型的封装规则

  • property属性: 对应父类中List集合的变量名,这里SceneVO类里的List变量名为sceneList

  • ofType属性: 集合存放的类型,List集合要装的类的类名,这里是SubSceneVO

Service层

public interface SceneService {
 /**
 * 获取场景信息
 */
 SceneVO getScenesInfo(String sessionId);
}
@Service
public class SceneServiceImpl {

 @Resource
 private BusinessScenesCustomMapper businessScenesCustomMapper;
 ......
 public SceneVO getScenesInfo(String sessionId) {
  return businessScenesCustomMapper.selectBySessionId(sessionId);
 }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值