(一)创建测试数据库
CREATE TABLE `t_teacher` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
?
CREATE TABLE `t_student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
`tid` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
(二)根据测试数据库创建实体类
-
学生类
package com.jarreet.test.pojo;
?
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
? -
老师类
package com.jarreet.test.pojo;
?
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
?
import java.util.ArrayList;
import java.util.List;
?
(三)文件配置
-
在 application.yml 配置 mybatis
mybatis
-
在 TestApplication 启动类上加上包扫描注解
二、多对一查询
我们根据上面搭建的环境,给出一个查询需求:查询所有学生的信息及他们的老师
(一)数据持久化层
-
StudentDao.java
package com.jarreet.test.dao;
?
import com.jarreet.test.pojo.Student;
import org.springframework.stereotype.Repository;
?
import java.util.List;
? -
StudentMapper.xml
(二)业务逻辑层
-
StudentService.java
package com.jarreet.test.service;
?
import com.jarreet.test.pojo.Student;
?
import java.util.List;
?
public interface StudentService {
?
List<Student> getAllStudents();
} -
StudentServiceImpl.java
package com.jarreet.test.service.impl;
?
import com.jarreet.test.dao.StudentDao;
import com.jarreet.test.pojo.Student;
import com.jarreet.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
import java.util.List;
?
(三)访问控制层
StudentController.java
package com.jarreet.test.controller;
?
import com.jarreet.test.pojo.Student;
import com.jarreet.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
?
import java.util.List;
?
(四)结果展示
三、一对多查询
我们根据上面搭建的环境,给出一个查询需求:查询所有老师的信息及他们的学生
(一)数据持久化层
-
TeacherDao.java
package com.jarreet.test.dao;
?
import com.jarreet.test.pojo.Teacher;
import org.springframework.stereotype.Repository;
?
import java.util.List;
? -
TeacherMapper.xml
(二)业务逻辑层
-
TeacherService.java
package com.jarreet.test.service;
?
import com.jarreet.test.pojo.Teacher;
?
import java.util.List;
?
public interface TeacherService {
List<Teacher> getAllTeachers();
} -
TeacherServiceImpl.java
package com.jarreet.test.service.impl;
?
import com.jarreet.test.dao.TeacherDao;
import com.jarreet.test.pojo.Teacher;
import com.jarreet.test.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
import java.util.List;
?
(三)访问控制层
TeacherController.java
package com.jarreet.test.controller;
?
import com.jarreet.test.pojo.Teacher;
import com.jarreet.test.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
?
import java.util.List;
?
(四)结果展示
四、多对多查询
多对多查询其实就是双向的一对多查询
这里我们给出一个多对多的情境:网上选课,一个账号可以选择多个课程,而一个课程也有可能被多个账号选择
那么我们现在需要一个账号表 t_account ,一个课程表 t_course;但是这两张表就够了吗?
当然不够,开头说了,多对多查询是双向的一对多,那么我们就需要一张中间表 account_course 来实现
(一)创建数据库表
CREATE TABLE `t_account`(
`id` INT(11) PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(11) DEFAULT NULL
);
?
CREATE TABLE `t_course`(
`id` INT(11) PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(11) DEFAULT NULL
);
?
CREATE TABLE `account_course`(
`id` INT(11) PRIMARY KEY AUTO_INCREMENT,
`aid` INT(11) DEFAULT NULL,
`cid` INT(11) DEFAULT NULL
);
(二)根据数据库表创建实体类
-
账户类
package com.jarreet.test.pojo;
?
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
?
import java.util.List;
? -
课程类
package com.jarreet.test.pojo;
?
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
?
import java.util.List;
?
(三)数据持久化层
-
AccountDao.java
package com.jarreet.test.dao;
?
import com.jarreet.test.pojo.Account;
import org.springframework.stereotype.Repository;
? -
AccountMapper.xml
-
CourseDao.java
package com.jarreet.test.dao;
?
import com.jarreet.test.pojo.Course;
import org.springframework.stereotype.Repository;
? -
CourseMapper.xml
(四)业务逻辑层
-
AccountService.java
package com.jarreet.test.service;
?
import com.jarreet.test.pojo.Account;
?
public interface AccountService {
?
Account findAccountById(Integer id);
} -
AccountServiceImpl.java
package com.jarreet.test.service.impl;
?
import com.jarreet.test.dao.AccountDao;
import com.jarreet.test.pojo.Account;
import com.jarreet.test.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
? -
CourseService.java
package com.jarreet.test.service;
?
import com.jarreet.test.pojo.Course;
?
public interface CourseService {
Course findCourseById(Integer id);
} -
CourseServiceImpl.java
package com.jarreet.test.service.impl;
?
import com.jarreet.test.dao.CourseDao;
import com.jarreet.test.pojo.Course;
import com.jarreet.test.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
(五)访问控制层
-
AccountController.java
package com.jarreet.test.controller;
?
import com.jarreet.test.pojo.Account;
import com.jarreet.test.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
? -
CourseController.java
package com.jarreet.test.controller;
?
import com.jarreet.test.pojo.Course;
import com.jarreet.test.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
?
(六)结果展示
至此,关于 Mybatis 的多表查询就讲解完毕了,下面我们简单做个总结。
Mybatis的多表查询无非就是做好实体类属性和mapper.xml文件的映射,其中:
association 是将结果集封装成?个实体类的对象(?个?标对象),并通过 JavaType 设置数类型;
collection 是将结果集封装成?个集合对象(多个?标对象),并通过 ofType 设置数据类型;
当多对一时,使用 association
当一对多时,使用 collection;
而当多对多时,无非就是进行一个双向的一对多绑定
特别要注意的几个点:
搭建环境时,要在配置文件中配置好 mybatis 信息,否则有可能报错
需要开启包扫描,要么在启动类上加上 @MapperScan 注解,要么在 持久化接口上加上 @Mapper 注解
当需要级联的对象的属性名和自身的属性名相同时,需要起别名,防止冲突覆盖