SpringBoot整合Mybatis-Plus----多表关联查询(使用注解)

继续在 SpringBoot整合Mybatis-Plus 基础上修改项目

一、创建两张表

用户表(User)、区域表(Area),其中用户表里通过 area_id 字段关联区域表的 id 主键
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、编写实体类

User

package org.spring.springboot.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class User {
    //指定主键使用数据库ID自增策略
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String userName;
    private String passWord;
    private Integer areaId;
    //由于 area_name 不是 User 数据库表里的字段,因此需要添加 @TableField 注解,并将 exist 属性设置为 false。
    @TableField(exist = false)
    private String areaName;
    //假设我们需要查询一个区域及其下面的所有用户,首先对 Area 实体类稍作修改,增加 users 集合属性
    @TableField(exist = false)
    private Area area;
}

Area

package org.spring.springboot.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.util.List;

@Data
public class Area {
    //指定主键使用数据库ID自增策略
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String areaName;

    @TableField(exist = false)
    private List<User> users;
}

三、使用 @One 注解实现一对一关联

UserMapper

package org.spring.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.spring.springboot.entity.User;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select("select user.* ,area.area_name from user,area "+
            " where user.area_id = area.id and user.id = #{id}"
            )
    User getUserById(int id);

    @Select("SELECT * FROM user WHERE area_id = #{areaId}")
    User selectByAreaId(int areaId);
}

UserMapperOne

package org.spring.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.spring.springboot.entity.User;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserMapperOne extends BaseMapper<User> {
    //@Results 注解来映射查询结果集到实体类属性
    /*
    * 当我们需要通过查询到的一个字段值作为参数,去执行另外一个方法来查询关联的内容,而且两者是一对一关系时,可以使用 @One 注解来便捷的实现。
      selectById 方法是 BaseMapper 就提供的,所以我们不需要在 AreaMapper 中手动定义。
      @Result(column = "area_id", property = "areaId") 可以不写,也不会报错。但是会导致我们查询结果(User 实体)的 areaId 属性没有值(因为第二个 Result 将 area_id 值作为查询条件传入子查询)。
    */
    @Results({
            @Result(column = "area_id", property = "areaId"),
            @Result(column = "area_id", property = "area",
                    one = @One(select = "org.spring.springboot.mapper.AreaMapper.selectById"))
    })
    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(int id);
}

四、使用 @Many 注解实现一对一关联

AreaMapper

package org.spring.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.spring.springboot.entity.Area;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface AreaMapper extends BaseMapper<Area> {
    /*
    @Many 的用法与 @One 类似,只不过如果使用 @One 查询到的结果是多行,会抛出 TooManyResultException 异常,这种时候应该使用的是 @Many 注解,实现一对多的查询。
    @Result(column = "id", property = "id") 可以不写,也不会报错。但是会导致我们查询结果(Area 实体)的 id 属性没有值(因为第二个 Result 将 id 值作为查询条件传入子查询)。
    */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "users",
                    many = @Many(select = "org.spring.springboot.mapper.UserMapper.selectByAreaId"))
    })
    @Select("SELECT * FROM area WHERE id = #{id}")
    Area getAreaById(int id);
}

五、Controller层

SelectController

package org.spring.springboot.controller;

import org.spring.springboot.entity.Area;
import org.spring.springboot.entity.User;
import org.spring.springboot.mapper.AreaMapper;
import org.spring.springboot.mapper.UserMapper;
import org.spring.springboot.mapper.UserMapperOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class SelectController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserMapperOne userMapperOne;
    @Autowired
    AreaMapper areaMapper;

    @RequestMapping("/selectUser")
    public User test(){
        User user = userMapper.getUserById(1);
        return user;
    }

    @RequestMapping("/selectUserOne")
    public User testOne(){
        User user = userMapperOne.getUserById(1);
        return user;
    }

    @RequestMapping("/selectUserMany")
    public Area testMany(){
        Area area = areaMapper.getAreaById(1);
        return area;
    }
}

六、测试

输入http://localhost:8086/api/selectUser
在这里插入图片描述
输入http://localhost:8086/api/selectUserOne
在这里插入图片描述
输入http://localhost:8086/api/selectUserMany
在这里插入图片描述
整体项目结构
在这里插入图片描述

参考文章
SpringBoot - MyBatis-Plus使用详解7(Mapper的CRUD接口4:多表关联查询)

世界地图矢量数据可以通过种网站进行下载。以下是一些提供免费下载世界地图矢量数据的网站: 1. Open Street Map (https://www.openstreetmap.org/): 这个网站可以根据输入的经纬度或手动选定范围来导出目标区域的矢量图。导出的数据格式为osm格式,但只支持矩形范围的地图下载。 2. Geofabrik (http://download.geofabrik.de/): Geofabrik提供按洲际和国家快速下载全国范围的地图数据。数据格式支持shape文件格式,包含个独立图层,如道路、建筑、水域、交通、土地利用分类、自然景观等。数据每天更新一次。 3. bbbike (https://download.bbbike.org/osm/): bbbike提供全球主要的200个城市的地图数据下载,也可以按照bbox进行下载。该网站还提供全球的数据,数据格式种类齐全,包括geojson、shp等。 4. GADM (https://gadm.org/index.html): GADM提供按国家或全球下载地图数据的服务。该网站提供种格式的数据下载。 5. L7 AntV (https://l7.antv.antgroup.com/custom/tools/worldmap): L7 AntV是一个提供标准世界地图矢量数据免费下载的网站。支持种数据格式下载,包括GeoJSON、KML、JSON、TopJSON、CSV和高清SVG格式等。可以下载中国省、市、县的矢量边界和世界各个国家的矢量边界数据。 以上这些网站都提供了世界地图矢量数据的免费下载服务,你可以根据自己的需求选择合适的网站进行下载。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值