当前位置: 首页 > 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"}}]
}
http://www.lryc.cn/news/307893.html

相关文章:

  • 华东地区汽车相关夹具配套企业分布图,你了解多少?
  • SpringBoot - 后端数据返回前端各个数据类型全局格式化
  • 实验室记账项目(java+Mysql+jdbc)
  • spring boot 整合 minio存储 【使用篇】
  • 【Redis】深入理解 Redis 常用数据类型源码及底层实现(5.详解List数据结构)
  • Vue+Flask电商后台管理系统
  • SpringBoot保姆级入门文档
  • Springboot同一台服务器部署多个项目,导致redis混淆,如何根据不同项目区分
  • redis启动错误
  • 单片机烧录方式 -- IAP、ISP和ICP
  • 数据结构(C语言版)01
  • Node.js-文件读取输入
  • 时隔一年的测评:gpt3.5发展到什么程度了?
  • [RCTF2015]EasySQL1 题目分析与详解
  • 开源的 Python 数据分析库Pandas 简介
  • LeetCode 2125.银行中的激光束数量
  • 【探索AI】Sora - 探索AI视频模型的无限可能
  • NGINX的重写与反向代理机制解析
  • JVM的深入理解
  • JavaWeb——007MYSQL(DQL多表设计)
  • 深度学习500问——Chapter01:数学基础
  • day03_登录注销(前端接入登录,异常处理, 图片验证码,获取用户信息接口,退出功能)
  • k8s初始化报错 [ERROR CRI]: container runtime is not running: ......
  • vscode windows 免密登录 powershell.sh
  • 10 种3D 建模技术
  • 常见的socket函数封装和多进程和多线程实现服务器并发
  • Tomcat架构分析
  • 旧项目集成阿里云滑动验证码(web和H5方式)
  • 机器人内部传感器阅读梳理及心得-速度传感器-数字式速度传感器
  • 【vue+element ui】大屏自适应中el-select下拉内容在低分辨率下显示不全问题解决