Mybatis resultMap标签继承、复用、嵌套方式

目录
  • resultMap标签继承、复用、嵌套
    • 定义表与实体类
      • 实体类
      • 定义与表映射的resultMap
    • 继承、复用、嵌套
  • 使用resultMap需要注意的地方

resultMap标签继承、复用、嵌套

记录演示 Mybatis 中 resultMap 标签继承、复用(包括跨文件)以及多层嵌套的使用方法,

  • 继承: 继承已存在的 resultMap 标签进行扩展
  • 复用: 跨mapper文件引用现存的 resultMap 标签
  • 嵌套: 多层嵌套的JavaBean与 resultMap 映射方法

定义表与实体类

创建三个表 group member score

score 与 member 一对一,通过 score.id 关联

group 与 member 一对多,通过 group.id 关联

create table `score` (
    `id` int comment '主键',
    `math` float comment '数学成绩',
    `history` float comment '历史成绩',
    primary key (`id`)
)
create table `member` (
    `id` int comment '主键',
    `name` varchar comment '姓名',
    `group_id` int comment '所属组group表id',
    `score_id` int comment '成绩Score表id',
    primary key (`id`)
)
create table `group` (
    `id` int comment '主键',
    `name` varchar comment '组名',
    primary key (`id`)
)

实体类

创建三个实体类 Group Member Score

Score 类的对象是 Member 类的成员变量

Member 类的对象集合是 Group 类的成员变量

/** 成绩类 */
public class Score {
    private Integer id;
    /** 数学成绩 */
    private Float math;
    /** 历史成绩 */
    private Float hitory;
    ...getter And setter...
}
/** 成员类 */
public class Member {
    private Integer id;
    /** 姓名 */
    private String name;
    /** 分数对象 */
    private Score score;
    ...getter And setter...
}
/** 组类 */
public class Group {
    private Integer id;
    /** 组名 */
    private String groupName;
    /** 成员 */
    private List<Member> members;
    ...getter And setter...
}

定义与表映射的 resultMap

在 BeanMapper.xml 定义最基本的与数据库表字段映射的 resultMap 标签

<?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.example.BeanMapper">
    <!-- Score实体类映射 -->
    <resultMap id="scoreMap" type="com.example.Score">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="math" jdbcType="FLOAT" property="math" />
        <result column="history" jdbcType="FLOAT" property="history" />
    </resultMap>
    <!-- Member实体类映射 -->
    <resultMap id="memberMap" type="com.example.Member">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
    </resultMap>
    <!-- Group实体类映射 -->
    <resultMap id="groupMap" type="com.example.Group">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="groupName" />
    </resultMap>
</mapper>

继承、复用、嵌套

创建 DemoMapper.xml,演示标签的继承、复用、嵌套

复用现存标签时若位于相同mapper文件可直接使用 resultMap 的 id 属性引用,跨文件时需要指定 namespace 属性才可正常引用

  • extends: 继承,可继承其他 resultMap 并加以扩展
  • association: 复用现存的 resultMap,适用于对应的属性为单JavaBean时,使用 javaType 指定Java类型
  • collection: 复用现存的 resultMap,适用于对应的属性为JavaBean集合时,使用 ofType 指定Java类型
  • columnPrefix: 只将该属性指定前缀的属性赋值给当前 resultMap,存在多层嵌套时每进入一层就会将本层前缀截取掉。

如下面的mapper文件中,外层的 fullMemberMap 前缀为 member_,经本次筛选 member_score_id -> score_id,

内层的 scoreMap 前缀为 score_,经本次筛选 score_id -> id,最终被赋值给 Score.id

所以只有形如 member_score_id 的字段才会最终进入 scoreMap 的取值范围中

若是不复用只是单纯嵌套,则可以直接将三个类写在一个 resultMap 标签内实现

<?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.example.DemoMapper">
    <!-- extends: 继承 -->
    <resultMap id="fullMemberMap" extends="com.example.BeanMapper.memberMap" type="com.example.Member">
        <!-- association: 复用已存在的resultMap,单JavaBean属性时使用
                        使用javaType属性指定JavaBean的类型
                        跨文件引用需指定namespace -->
        <!-- columnPrefix: 只从 score_ 开头的字段为当前resultMap取值 -->
        <association  property="score" resultMap="com.example.BeanMapper.scoreMap" javaType="com.example.Score" columnPrefix="score_" />
    </resultMap>
    
    <resultMap id="fullGroupMap" extends="com.example.BeanMapper.groupMap" type="com.example.Group">
        <!-- collection: 复用已存在的resultMap,JavaBean集合属性时使用
                        使用ofType属性指定JavaBean的类型
                        同文件引用无需指定namespace -->
        <!-- columnPrefix: 只从 member_ 开头的字段为当前resultMap取值
                        进入fullMemberMap内嵌套的scoreMap时前缀 member_ 会被去除,即 member_score_id 字段才能被scoreMap正确接收 -->
        <collection property="members" ofType="com.example.Member" resultMap="fullMemberMap" columnPrefix="member_"/>
    </resultMap>
    <!-- 直接引用最终的resultMap,并根据columnPrefix属性设置的前缀为各个字段指定不同的别名 -->
    <select id="selectGroupById" parameterType="java.lang.Integer" resultMap="fullGroupMap">
        select g.id, g.name,
               m.id member_id, m.name member_name,
               s.id member_score_id, s.math member_score_math, s.history member_score_history
          from `group` g
            left join `member` m on m.group_id = g.id
            left join `score` s on s.id = m.score_id
          where g.id = #{id,jdbcType=INTEGER}
    </select>
</mapper>

使用resultMap需要注意的地方

今天主要还是根据需求在进行sql的编写 ,在mybatis里面进行复查和复用的时候一定要去看所对应的有没有这个类 ,今天弄了几个dto,还有时间戳的转换,java里面的时间戳是以毫秒来进行计算的。

所以说在专用mysql的时候要注意

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • mybatis的mapper.xml中resultMap标签的使用详解

    1.前言 最近博主在做一个ssm框架的共享汽车管理系统,其中,数据库字段设计的有下划线方式,a_username,然后在写mapper.xml里面的sql语句的时候,一直出现查询语句查询的值为null的情况.或者是resultMap标签和驼峰规则不太明白的同学,可以看这里. 于是顺便梳理一下. 2.关于resultMap 2.1.什么是resultMap? 在mybatis中有一个resultMap标签,它是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与数据库表中的字段进

  • mybatis多层嵌套resultMap及返回自定义参数详解

    1.两层嵌套,一个list中加另外一个list data:[ {a:123,b:456,c:[{d:7,e:8}]} ] xml文件定义的sql select * from zhy z LEFT JOIN wl w on z.id = w.zid resultMap可以定义: <resultMap id="zhyResultMap" type="zhy的doman实体" extends="zhy自动生成的BaseResultMap">

  • mybatis中resultMap 标签的使用教程

    MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL本身,而不需要花费精力去处理例如注册驱动.创建connection.创建statement.手动设置参数.结果集检索等jdbc繁杂的过程代码. MyBatis特点: 1.开源的优秀持久层框架 2.SQL语句与代码分离 3.面向配置的编程 4.良好支持复杂数据映射 5.动态SQL resultMap 标签: 用来描述如何从数据库结果集中来加载对象 (敲黑板!!)主管数据库的字段和实体类属性的匹配,

  • Mybatis resultMap标签继承、复用、嵌套方式

    目录 resultMap标签继承.复用.嵌套 定义表与实体类 表 实体类 定义与表映射的resultMap 继承.复用.嵌套 使用resultmap需要注意的地方 resultMap标签继承.复用.嵌套 记录演示 Mybatis 中 resultMap 标签继承.复用(包括跨文件)以及多层嵌套的使用方法, 继承: 继承已存在的 resultMap 标签进行扩展 复用: 跨mapper文件引用现存的 resultMap 标签 嵌套: 多层嵌套的JavaBean与 resultMap 映射方法 定义

  • Mybatis resultMap标签继承、复用、嵌套方式

    目录 resultMap标签继承.复用.嵌套 定义表与实体类 表 实体类 定义与表映射的resultMap 继承.复用.嵌套 使用resultMap需要注意的地方 resultMap标签继承.复用.嵌套 记录演示 Mybatis 中 resultMap 标签继承.复用(包括跨文件)以及多层嵌套的使用方法, 继承: 继承已存在的 resultMap 标签进行扩展 复用: 跨mapper文件引用现存的 resultMap 标签 嵌套: 多层嵌套的JavaBean与 resultMap 映射方法 定义

  • MyBatis 配置之集合的嵌套方式

    目录 MyBatis 配置之集合的嵌套 前言介绍 代码示例 外部引用 小结一下吧 MyBatis 集合.集合嵌套查询 集合 集合的嵌套查询 集合的嵌套结果 MyBatis 配置之集合的嵌套 前言介绍 在一些查询结果包装类中,包含一些 List 集合属性,使用 collection 标签可以声明该 List 集合中属性的类型,便于 MyBatis 对包装类中的集合类型属性进行映射. 代码示例 例如商城,取出某一个商品信息以及该商品的评价列表,其中商品包装类 Product 的定义代码如下: pac

  • Mybatis中resultMap标签和sql标签的设置方式

    目录 resultMap标签和sql标签的设置 1.项目目录 2.数据库中的表的信息 3.配置文件的信息 4.User类 5.IUserDao接口 6.MybatisTest 7.运行结果 resultMap标签的使用规则 自定义结果映射规则 association联合查询 使用association进行分布查询 collection分步查询 resultMap标签和sql标签的设置 1.项目目录 2.数据库中的表的信息 3.配置文件的信息 1.SqlMapConfig.xml文件 <?xml

  • MyBatis resultMap id标签的错误使用方式

    目录 MyBatis resultMap id标签的错误使用 本节的问题主要是我对mybatis id标签的错误使用 resultMap标签的使用规则 自定义结果映射规则 association联合查询 使用association进行分布查询 collection分步查询 MyBatis resultMap id标签的错误使用 我们在编写VO对象,如果业务场景稍微复杂一点,就会用到集合属性.例如用户查看个人订单列表,每个订单又包含多种或者多个规格的商品. 本节的问题主要是我对mybatis id

  • 详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作

    一.getMapper()接口 解析:getMapper()接口 IDept.class定义一个接口, 挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的 通过代理:生成接口的实现类名称,在MyBatis底层维护名称$$Dept_abc,selectDeptByNo() 相当于是一个强类型 Eg 第一步:在cn.happy.dao中定义一个接口 package cn.happy.dao; import java.util.List; import cn.happy.e

  • mybatis if标签使用总结

    在项目开发中,mybatis <if> 标签使用广泛,本文讲解if标签的两种使用方式 其一.使用 <if> 标签判断某一字段是否为空 其二.使用 <if> 标签判断传入参数是否相等 具体代码如下 数据库表结构和数据 实体类 package com.demo.bean; public class Commodity { private String name; private String date; public String getName() { return na

  • Mybatis中连接查询和嵌套查询实例代码

    首先在mysql中确立表: #表一:地址国家表 CREATE TABLE address(aid INT AUTO_INCREMENT PRIMARY KEY,aname VARCHAR(20)); INSERT INTO address VALUES(NULL,"魏国"); INSERT INTO address VALUES(NULL,"蜀国"); INSERT INTO address VALUES(NULL,"吴国"); #表二:出场人物

随机推荐