热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Mybatis高级查询(一对一,一对多,多对多)

数据准备CREATETABLEstudent_info(classesvarchar(255)NOTNULL,namevarchar(255)NOTNULL,student_nu

数据准备

CREATE TABLE `student_info` (`classes` varchar(255) NOT NULL,`name` varchar(255) NOT NULL,`student_numer` int(11) DEFAULT NULL,`subject` varchar(255) DEFAULT NULL,`score` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `student_info` VALUES ('1班', '张三', '1', '语文', '100');
INSERT INTO `student_info` VALUES ('1班', '张三', '1', '数学', '99');
INSERT INTO `student_info` VALUES ('1班', '张三', '1', '英语', '98');
INSERT INTO `student_info` VALUES ('1班', '李四', '2', '语文', '100');
INSERT INTO `student_info` VALUES ('1班', '李四', '2', '数学', '99');
INSERT INTO `student_info` VALUES ('1班', '李四', '2', '英语', '99');
INSERT INTO `student_info` VALUES ('2班', '王五', '3', '数学', '100');
INSERT INTO `student_info` VALUES ('2班', '王五', '3', '语文', '100');
INSERT INTO `student_info` VALUES ('2班', '王五', '3', '英语', '100');

在这里插入图片描述
代码环境搭建:
依赖:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version></parent><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency></dependencies>

application.yml配置文件:

server:port: 8888spring:datasource:url: jdbc:mysql://localhost:3306/test?useUnicode&#61;true&characterEncoding&#61;utf8&zeroDateTimeBehavior&#61;convertToNull&useSSL&#61;true&serverTimezone&#61;GMT%2B8username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver#必须配置mapperxml的路径
mybatis:mapper-locations: classpath:mybatis/*.xml

启动类:

&#64;SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}

Controller

&#64;RestController
&#64;RequestMapping("/")
public class MyConrtroller {&#64;AutowiredMyService service;//一对一&#64;GetMapping("/getStudentInfo1")public Object getStudentInfo1(){return service.getStudentInfo1();}//一对多&#64;GetMapping("/getStudentInfo2")public Object getStudentInfo2(){return service.getStudentInfo2();}//多对多&#64;GetMapping("/getStudentInfo3")public Object getStudentInfo3(){return service.getStudentInfo3();}
}

service:

&#64;Service
public class MyService {&#64;AutowiredMyMapper mapper;public Object getStudentInfo1() {return mapper.getStudentInfo1();}public Object getStudentInfo2() {return mapper.getStudentInfo2();}public Object getStudentInfo3() {return mapper.getStudentInfo3();}
}

mapper接口

&#64;Mapper
public interface MyMapper {StudentInfo getStudentInfo1();StudentInfo getStudentInfo2();ArrayList<StudentInfo> getStudentInfo3();}

一对一

Mapper.xml文件

<select id&#61;"getStudentInfo1" resultMap&#61;"resultMap1">select * from test.student_info where classes&#61;&#39;1班&#39; and name &#61;&#39;张三&#39; and subject &#61; &#39;语文&#39;select><resultMap id&#61;"resultMap1" type&#61;"com.anjiplus.mybatistest.bean.StudentInfo" autoMapping&#61;"true"><id column&#61;"classes" property&#61;"classes">id><association property&#61;"student" javaType&#61;"com.anjiplus.mybatistest.bean.Student" autoMapping&#61;"true" ><id column&#61;"name" property&#61;"name">id><id column&#61;"student_numer" property&#61;"studentNumber">id><association property&#61;"subjectAndScore" javaType&#61;"com.anjiplus.mybatistest.bean.SubjectAndScore" autoMapping&#61;"true" ><id column&#61;"subject" property&#61;"subject">id><result column&#61;"score" property&#61;"score">result>association>association>resultMap>

sql查询结果为:
在这里插入图片描述

对应的javaBean:

public class StudentInfo {private String classes;private Student student;//为了篇幅省略getter和setter方法
}public class Student {private String name;private Integer studentNumber;private SubjectAndScore subjectAndScore;//为了篇幅省略getter和setter方法
}public class SubjectAndScore {private String subject;private Integer score;//为了篇幅省略getter和setter方法
}

调用getStudentInfo1接口返回:
在这里插入图片描述


一对多

Mapper.xml

<select id&#61;"getStudentInfo2" resultMap&#61;"resultMap2">select * from test.student_info where classes &#61;&#39;1班&#39; and name &#61;&#39;张三&#39;select><resultMap id&#61;"resultMap2" type&#61;"com.anjiplus.mybatistest.bean.StudentInfo" autoMapping&#61;"true"><id column&#61;"classes" property&#61;"classes">id><association property&#61;"student" javaType&#61;"com.anjiplus.mybatistest.bean.Student" autoMapping&#61;"true" ><id column&#61;"name" property&#61;"name">id><id column&#61;"student_numer" property&#61;"studentNumber">id><collection property&#61;"subjectAndScore" javaType&#61;"java.util.ArrayList" ofType&#61;"com.anjiplus.mybatistest.bean.SubjectAndScore"><id column&#61;"subject" property&#61;"subject">id><result column&#61;"score" property&#61;"score">result>collection>association>resultMap>

sql查询结果
在这里插入图片描述
对应javabean:

public class StudentInfo {private String classes;private Student student;//为了篇幅省略getter和setter方法
}public class Student {private String name;private Integer studentNumber;private ArrayList<SubjectAndScore> subjectAndScore;//为了篇幅省略getter和setter方法
}public class SubjectAndScore {private String subject;private Integer score;//为了篇幅省略getter和setter方法
}

调用getStudentInfo2接口返回:
在这里插入图片描述


多对多

Mapper.xml

<select id&#61;"getStudentInfo3" resultMap&#61;"resultMap3">select * from test.student_infoselect><resultMap id&#61;"resultMap3" type&#61;"com.anjiplus.mybatistest.bean.StudentInfo" autoMapping&#61;"true"><id column&#61;"classes" property&#61;"classes">id><collection property&#61;"student" javaType&#61;"java.util.ArrayList" ofType&#61;"com.anjiplus.mybatistest.bean.Student" autoMapping&#61;"true" ><id column&#61;"name" property&#61;"name">id><id column&#61;"student_numer" property&#61;"studentNumber">id><collection property&#61;"subjectAndScore" javaType&#61;"java.util.ArrayList" ofType&#61;"com.anjiplus.mybatistest.bean.SubjectAndScore"><id column&#61;"subject" property&#61;"subject">id><result column&#61;"score" property&#61;"score">result>collection>collection>resultMap>

sql查询结果:
在这里插入图片描述
对应javabean:

public class StudentInfo {private String classes;private ArrayList<Student> student;//为了篇幅省略getter和setter方法
}public class Student {private String name;private Integer studentNumber;private ArrayList<SubjectAndScore> subjectAndScore;//为了篇幅省略getter和setter方法
}public class SubjectAndScore {private String subject;private Integer score;//为了篇幅省略getter和setter方法
}

调用getStudentInfo3接口返回:
在这里插入图片描述


ResultMap

在select标签中,有resultType属性,这个属性用于对应sql查询出来的每一行数据封装一个数据类型,而resultMap可以做到多行数据封装成一个类型.,但是需要我们在xml中对数据类型进行提前拼接定义.
ResultMap标签属性:
id属性:用于识别唯一,在select 标签中通过resultMap属性进行引用.
type属性:指ResultMap封装成的java数据类型
autoMapping属性:是否将没有在ResultMap中配置的映射的列自动映射到结果类型中.比如在A类中有a b c d四个列,但是resutMap中我们只设置了a和b列,设置为true后,可以将查询出来的c d直接封装到A类中.
extending属性:ResultMap的继承,a继承了b那么在a中就不想要再写b中已经有的内容了.


ResultMap子标签:

id 标签: 用来配置"主键"(比如查询出两行数据a列都是1,那么在结果类型中a字段就是一个值:1)
column:sql中查询出来的字段名
property:映射的java类型中的属性名
javaType:对应的java类型
jdbcType:数据库类型
typeHandler:基本不用,要用自行百度
result 标签:用来设置sql查询出来的列与java类型中的列的映射(非主键)
属性同id标签
**constructor 标签:**通过构造函数属性注入,否则的话是通过setter方法注入属性,并且通过setter方法注入的类必须只有无参构造
**association 标签:**标注一对一关系
property:指association所封装的数据结构在resultmap返回类型中的属性名
javaType:指association锁封装的数据结构类型
autoMapping:
其他属性:略
select:指定一个mapper接口的方法,比如com.anjiplus.mapper.MyMapper.getId
**collection 标签:**标注多对多关系
property:指collection 所封装的数据结构在上一层返回类型中的属性名
javaType:指collection 锁封装的数据结构类型
ofType:指collection中每一个元素的数据类型
autoMapping:
select:指定一个mapper接口的方法,比如com.anjiplus.mapper.MyMapper.getId
其他不常用属性略
discriminator 标签: 对查询出的某个列的值进行鉴别,如果是值1就xxx 如果是值2就xxx(不常用,自行百度)
collection 与 association 标签的字标签与ResultMap的子标签相同(所以ResultMap是镶嵌结构)


推荐阅读
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • 如何实现JDK版本的切换功能,解决开发环境冲突问题
    本文介绍了在开发过程中遇到JDK版本冲突的情况,以及如何通过修改环境变量实现JDK版本的切换功能,解决开发环境冲突的问题。通过合理的切换环境,可以更好地进行项目开发。同时,提醒读者注意不仅限于1.7和1.8版本的转换,还要适应不同项目和个人开发习惯的需求。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 一次上线事故,30岁+的程序员踩坑经验之谈
    本文主要介绍了一位30岁+的程序员在一次上线事故中踩坑的经验之谈。文章提到了在双十一活动期间,作为一个在线医疗项目,他们进行了优惠折扣活动的升级改造。然而,在上线前的最后一天,由于大量数据请求,导致部分接口出现问题。作者通过部署两台opentsdb来解决问题,但读数据的opentsdb仍然经常假死。作者只能查询最近24小时的数据。这次事故给他带来了很多教训和经验。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • ubuntu用sqoop将数据从hive导入mysql时,命令: ... [详细]
author-avatar
mobiledu2502902523
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有