当前位置: 首页 > news >正文

SpringBoot 自定义映射规则resultMap association一对一

介绍

例:学生表,班级表,希望在查询学生的时候一起返回该学生的班级,而一个实体类封装的是一个表,如需要多表查询就需要自定义映射。

表结构

班级表
在这里插入图片描述

学生表
在这里插入图片描述

SQL语句

SELECT a.id,a.name,a.classes,b.id classesId,b.name classesNmae 
FROM students a,classes b WHERE a.name='蔡徐坤'  and a.classes=b.id 

在这里插入图片描述

实体类

班级类

@Data
public class Students {
    Long id;
    String name;
    }

学生类

@Data
public class Classes {
    Long id;
    String name;
}

Vo类 (返回前端)

@Data
public class StudentsVo {
    Students students;
    Classes classes;
}

Mapper

查询语句

<select id="list"  resultMap="listMap">
    SELECT a.id,a.name,a.classes,b.id classesId,b.name classesNmae  
     FROM students a,classes b WHERE a.name=#{name}  and  a.classes=b.id
</select

映射规则

id:自定义规则的名称,可以当作是一个方法
type:封装到哪个类
association :一对一
javaType:子对象,如果 学生 或 班级
autoMapping:false 或 true true为自动映射属性,如果属性名和字段名一致就可以使用,不一致时使用result

 <!--自定义映射规则-->
    <resultMap id="listMap" type="com.example.demo.demos.web.pojo.vo.StudentsVo" >
    
        <association property="students" javaType="com.example.demo.demos.web.pojo.Students"  autoMapping="true">
        </association>
        <!--封装学生类-->
        
        <association property="classes" javaType="com.example.demo.demos.web.pojo.Classes"  autoMapping="true">
            <id property="id" column="classesId" ></id>
            <result property="name" column="classesNmae"></result>
        </association>
       <!--封装班级类-->
        
    </resultMap>

id:表示是主键
result:属性名不一致是使用
property:实体类属性名
column:数据字段名

结果

{
    "code": 200,
    "msg": "获取成功",
    "data": [
        {
            "students": {
                "id": 18,
                "name": "蔡徐坤"
            },
            "classes": {
                "id": 1,
                "name": "大数据5"
            }
        },
        {
            "students": {
                "id": 21,
                "name": "蔡徐坤"
            },
            "classes": {
                "id": 2,
                "name": "大数据1"
            }
        }
    ]
}

这里json分为两个对象封装了,一个存学生,一个存班级,也可以把学生直接存到学生对象里。

优化

修改学生的实体类,直接把班级类定义
学生实体类

@Data
public class Students {
    Long id;
    String name;
    //班级消息
    Classes belong;
}

Mapper

<!--自定义映射规则-->
<resultMap id="listMap" type="com.example.demo.demos.web.pojo.Students" >
    
    <!--设置主键-->
    <id property="id" column="id"></id>
    <!--字段映射到属性-->
    <result property="name" column="name"></result>
    
    <!--属性和字段但一致的情况下可以使用autoMapping="true"-->
    
    <association property="belong" javaType="com.example.demo.demos.web.pojo.Classes" >
        <id property="id" column="classesId"></id>
        <result property="name" column="classesNmae"></result>
    </association>
    <!--班级信息封装的类-->
    
</resultMap>

一样的效果,都可以查出,但是结构更好一点

{
    "code": 200,
    "msg": "获取成功",
    "data": [
        {
            "id": 18,
            "name": "蔡徐坤",
            "belong": {
                "id": 1,
                "name": "大数据5"
            }
        },
        {
            "id": 21,
            "name": "蔡徐坤",
            "belong": {
                "id": 2,
                "name": "大数据1"
            }
        }
    ]
}

相关文章:

  • Nacos配置
  • 动态规划--(算法竞赛、蓝桥杯)--二维费用背包
  • 如何学习自然语言处理之语言模型
  • 【投稿优惠|快速见刊】2024年图像,机器学习和人工智能国际会议(ICIMLAI 2024)
  • element-ui附件上传及在线查看详细总结,后续赋源码
  • StarRocks——中信建投基于StarRocks构建统一查询服务平台
  • WPF 【十月的寒流】学习笔记(1):DataGrid过滤
  • Unity将4个纹理图拼接成1个纹理
  • 类型字段定义影响WebApi传值及SqlSugar调用Select创建新对象
  • Vue3修改Element-plus语言与项目国际化
  • Kotlin:协程基础
  • 打造去中心化透明储蓄罐:Solidity智能合约的又一实践
  • FL Studio All Plugins Edition2024中文完整版Win/Mac
  • 【PyTorch模型转换和部署】
  • 【数据结构】周末作业
  • web组态(BY组态)接入流程
  • 4 局域网基础(2)
  • 11.以太网交换机工作原理
  • centos服务配置springboot服务开机启动
  • 第一章 Win10系统VM安装Ubuntu1704虚拟机
  • 印度媒体称印巴在克什米尔再次交火
  • 从“高阶智驾”到“辅助驾驶”,上海车展上的“智驾”宣发变调
  • 为何不当教练转型高校管理岗?苏炳添曾吐露过真实的想法
  • 政治局会议:积极维护多边主义,反对单边霸凌行径
  • 当哲学与戏剧作为一种生活方式——《人生六戏》分享会
  • 证券时报:落实“非禁即入” ,让创新活力充分涌流