mybatis-plus的多对一,一对多关系映射
MyBatis中的一对多和对多一,多对多
主要就是resultMap中
1.association(关联) – 一个复杂的类型关联;许多结果将包成这种类型(多对一
嵌套结果映射 – 关联本身可以是一个 resultMap 元素,或者从别处引用一个
2. collection(集合) – 复杂类型的集合(一对多)
嵌套结果映射 – 集合本身可以是一个 resultMap 元素,或者从别处引用一个
多对一或一对多,实际上就是选择的参照表不一样
更多详细信息,在mybatis官方文档就能找到,建议大家可以直接看官方文档
什么是关联:
在mybatis中是用来处理"has a"关系,比如一个学生有一所学校,即一个学生关联一所学校,所以association能用来处理我们数据中所谓的一对一,多对一关系(一个学校有多个学生,但是对于学生来说,一个学生只能关联一个学校)。
什么是集合:
将嵌套映射的结果集合到一个list中,比如一个学校有多个学生,即1个学校对应多个学生,对于学校来说,一个学校有多个学生。
可以看到,在处理我们数据关系的时候,就是对这2个属性的使用,而一对多和多对一都是相互的,只是各自站的角度不同。
多对一
多对一首先就是在多方编写一方的实体类属性
然后编写嵌套查询的xml
多对一使用的是 collection 关联查询
多方的数据

一方的数据库

lombok的maven坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
多方的实体类(图中的注解使用的式lombok)
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
/**
* (ComponentPeijian)表实体类
* 多对一
* 在多方的实体类上面编写一方的实体类属性
* @author makejava
* @since 2022-08-17 15:55:53
*/
@SuppressWarnings("serial")
@ToString
@Data
public class ComponentPeijian extends Model<ComponentPeijian> implements Serializable{
//配件编号
private Integer componentId;
//类型编号
private Integer typeId;
//配件说明
private String componentName;
//容器
private String capacity;
//价格
private Integer price;
// @TableField(exist = false) //标识不是数据库的字段,但在项目中必须使用
// 多个配件对应一个类型 多对一关系
private ComputerType computerType;
/**
* 获取主键值
*
* @return 主键值
*/
@Override
protected Serializable pkVal() {
return this.componentId;
}
}
一方的实体类
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
/**
* (ComputerType)表实体类
*
* @author makejava
* @since 2022-08-17 15:56:23
*/
@SuppressWarnings("serial")
public class ComputerType extends Model<ComputerType> implements Serializable{
//类型编写
private Integer typeId;
//类型名称
private String typename;
public Integer getTypeId() {
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
/**
* 获取主键值
*
* @return 主键值
*/
@Override
protected Serializable pkVal() {
return this.typeId;
}
}
对应的mapper.xml
<select id="getAllTwo" resultMap="getTwoMap">
SELECT
cp.*,
cy.typename
FROM
component_peijian AS cp
JOIN computer_type AS cy ON cy.type_id = cp.type_id
</select>
<!-- 多对一 关系映射 继承多方的这个xml -->
<resultMap id="getTwoMap" type="com.xiaoliang.computer.entity.ComponentPeijian" extends="ComponentPeijianMap">
<!--
复杂的属性单独处理
这里解释下 association 和 collection 的使用场景
collection 应用场景为一对多关系,即实体里放集合。关联-association
association用于一对一、多对一场景使用。集合-collection
-->
<association property="computerType" javaType="com.xiaoliang.computer.entity.ComputerType">
<result property="typeId" column="type_id" jdbcType="INTEGER"/>
<result property="typename" column="typename" jdbcType="VARCHAR"/>
</association>
</resultMap>
持久层接口
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.xiaoliang.computer.entity.ComponentPeijian;
/**
* (ComponentPeijian)表数据库访问层
*
* @author makejava
* @since 2022-08-17 15:55:53
*/
@Mapper
public interface ComponentPeijianDao extends BaseMapper<ComponentPeijian> {
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<ComponentPeijian> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<ComponentPeijian> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<ComponentPeijian> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<ComponentPeijian> entities);
/***
* 多对一
* @return
*/
List<ComponentPeijian> getAllTwo();
}
业务层接口
import com.baomidou.mybatisplus.extension.service.IService;
import com.xiaoliang.computer.entity.ComponentPeijian;
import java.util.List;
/**
* (ComponentPeijian)表服务接口
*
* @author makejava
* @since 2022-08-17 15:55:54
*/
public interface ComponentPeijianService extends IService<ComponentPeijian> {
/**
* 业务层接口
* @return
*/
List<ComponentPeijian> getAllTwo();
}
业务层实现方法
package com.xiaoliang.computer.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoliang.computer.dao.ComponentPeijianDao;
import com.xiaoliang.computer.entity.ComponentPeijian;
import com.xiaoliang.computer.service.ComponentPeijianService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* (ComponentPeijian)表服务实现类
*
* @author makejava
* @since 2022-08-17 15:55:54
*/
@Service("componentPeijianService")
public class ComponentPeijianServiceImpl extends ServiceImpl<ComponentPeijianDao, ComponentPeijian> implements ComponentPeijianService {
@Resource
private ComponentPeijianDao ComponentPeijianDao;
@Override
public List<ComponentPeijian> getAllTwo() {
return ComponentPeijianDao.getAllTwo();
}
}
控制层(controller)
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoliang.computer.entity.ComponentPeijian;
import com.xiaoliang.computer.service.ComponentPeijianService;
import com.xiaoliang.computer.util.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* (ComponentPeijian)表控制层
*
* @author makejava
* @since 2022-08-17 15:55:52
*/
@RestController
@RequestMapping("componentPeijian")
public class ComponentPeijianController extends ApiController {
/**
* 服务对象
*/
@Resource
private ComponentPeijianService componentPeijianService;
@GetMapping("/getAll")
public Map getAll(){
Map codeMap = new HashMap();
List<ComponentPeijian> allTwo = componentPeijianService.getAllTwo();
codeMap.put("data",allTwo);
codeMap.put("code",200);
return codeMap;
}
}
查询成功

// TODO…
后续…
一对多的xml映射
<!-- 一对多-->
<select id="oneVsMore" resultMap="more">
SELECT
*
FROM
computer_type AS ct
JOIN component_peijian AS cp ON ct.type_id = cp.type_id
</select>
<!--一对多关系-->
<resultMap id="more" type="computerType" extends="ComputerTypeMap">
<collection property="componentPeijianList" ofType="com.xiaoliang.computer.entity.ComponentPeijian" javaType="list">
<result property="componentId" column="component_id" jdbcType="INTEGER"/>
<result property="typeId" column="type_id" jdbcType="INTEGER"/>
<result property="componentName" column="component_name" jdbcType="VARCHAR"/>
<result property="capacity" column="capacity" jdbcType="VARCHAR"/>
<result property="price" column="price" jdbcType="INTEGER"/>
</collection>
</resultMap>
一对多持久层
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.xiaoliang.computer.entity.ComputerType;
/**
* (ComputerType)表数据库访问层
*
* @author makejava
* @since 2022-08-17 15:56:23
*/
@Mapper
public interface ComputerTypeDao extends BaseMapper<ComputerType> {
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<ComputerType> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<ComputerType> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<ComputerType> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<ComputerType> entities);
List<ComputerType> oneVsMore();
}
业务层接口
package com.xiaoliang.computer.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xiaoliang.computer.entity.ComputerType;
import java.util.List;
/**
* (ComputerType)表服务接口
*
* @author makejava
* @since 2022-08-17 15:56:23
*/
public interface ComputerTypeService extends IService<ComputerType> {
List<ComputerType> oneVsMore();
}
业务层实现类
package com.xiaoliang.computer.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoliang.computer.dao.ComputerTypeDao;
import com.xiaoliang.computer.entity.ComputerType;
import com.xiaoliang.computer.service.ComputerTypeService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* (ComputerType)表服务实现类
*
* @author makejava
* @since 2022-08-17 15:56:23
*/
@Service("computerTypeService")
public class ComputerTypeServiceImpl extends ServiceImpl<ComputerTypeDao, ComputerType> implements ComputerTypeService {
@Resource
private ComputerTypeDao computerTypeDao;
@Override
public List<ComputerType> oneVsMore() {
return computerTypeDao.oneVsMore();
}
}
控制层
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoliang.computer.entity.ComputerType;
import com.xiaoliang.computer.service.ComputerTypeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
/**
* (ComputerType)表控制层
*
* @author makejava
* @since 2022-08-17 15:56:23
*/
@RestController
@RequestMapping("computerType")
public class ComputerTypeController extends ApiController {
/**
* 服务对象
*/
@Resource
private ComputerTypeService computerTypeService;
@GetMapping("/getOneVsMore") // /computerType/getOneVsMore
public R getOneVsMore() {
return success(computerTypeService.oneVsMore());
}
}
查询结果
