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

数据准备

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: 8888

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

#必须配置mapperxml的路径
mybatis:
  mapper-locations: classpath:mybatis/*.xml

启动类:

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

Controller

@RestController
@RequestMapping("/")
public class MyConrtroller {
    @Autowired
    MyService service;
    //一对一
    @GetMapping("/getStudentInfo1")
    public Object getStudentInfo1(){
        return service.getStudentInfo1();
    }
    //一对多
    @GetMapping("/getStudentInfo2")
    public Object getStudentInfo2(){
        return service.getStudentInfo2();
    }
    //多对多
    @GetMapping("/getStudentInfo3")
    public Object getStudentInfo3(){
        return service.getStudentInfo3();
    }
}

service:

@Service
public class MyService {

    @Autowired
    MyMapper mapper;

    public Object getStudentInfo1() {
        return mapper.getStudentInfo1();
    }

    public Object getStudentInfo2() {
        return mapper.getStudentInfo2();
    }

    public Object getStudentInfo3() {
        return mapper.getStudentInfo3();
    }
}

mapper接口

@Mapper
public interface MyMapper {

    StudentInfo getStudentInfo1();

    StudentInfo getStudentInfo2();

    ArrayList<StudentInfo> getStudentInfo3();

}

一对一

Mapper.xml文件

 <select id="getStudentInfo1" resultMap="resultMap1">
         select * from test.student_info where classes='1班' and name ='张三' and subject = '语文'
    </select>

    <resultMap id="resultMap1" type="com.anjiplus.mybatistest.bean.StudentInfo" autoMapping="true">
        <id column="classes" property="classes"></id>
        <association property="student" javaType="com.anjiplus.mybatistest.bean.Student" autoMapping="true" >
            <id column="name" property="name"></id>
            <id column="student_numer" property="studentNumber"></id>
            <association property="subjectAndScore" javaType="com.anjiplus.mybatistest.bean.SubjectAndScore" autoMapping="true" >
                <id column="subject" property="subject"></id>
                <result column="score" property="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="getStudentInfo2" resultMap="resultMap2">
         select * from test.student_info where classes ='1班' and name ='张三'
    </select>

    <resultMap id="resultMap2" type="com.anjiplus.mybatistest.bean.StudentInfo" autoMapping="true">
        <id column="classes" property="classes"></id>
        <association property="student" javaType="com.anjiplus.mybatistest.bean.Student" autoMapping="true" >
            <id column="name" property="name"></id>
            <id column="student_numer" property="studentNumber"></id>
            <collection property="subjectAndScore" javaType="java.util.ArrayList" ofType="com.anjiplus.mybatistest.bean.SubjectAndScore">
                <id column="subject" property="subject"></id>
                <result column="score" property="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="getStudentInfo3" resultMap="resultMap3">
         select * from test.student_info
    </select>

    <resultMap id="resultMap3" type="com.anjiplus.mybatistest.bean.StudentInfo" autoMapping="true">
        <id column="classes" property="classes"></id>
        <collection property="student" javaType="java.util.ArrayList" ofType="com.anjiplus.mybatistest.bean.Student" autoMapping="true" >
            <id column="name" property="name"></id>
            <id column="student_numer" property="studentNumber"></id>
            <collection property="subjectAndScore" javaType="java.util.ArrayList" ofType="com.anjiplus.mybatistest.bean.SubjectAndScore">
                <id column="subject" property="subject"></id>
                <result column="score" property="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是镶嵌结构)

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值