SpringBoot整合Mybatis-Plus----多表关联查询(使用注解) 野性酷女 2022-09-15 05:39 453阅读 0赞 ### 多表关联查询(使用注解) ### * 一、创建两张表 * 二、编写实体类 * 三、使用 @One 注解实现一对一关联 * 四、使用 @Many 注解实现一对一关联 * 五、Controller层 * 六、测试 继续在 [SpringBoot整合Mybatis-Plus][SpringBoot_Mybatis-Plus] 基础上修改项目 # 一、创建两张表 # 用户表(User)、区域表(Area),其中用户表里通过 area\_id 字段关联区域表的 id 主键 ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16] ![在这里插入图片描述][ad5d07b8a6584851b44eaf7e75dfe80e.png] ![在这里插入图片描述][968825e0c7da4f5ebb1d0bd932e45075.png] ![在这里插入图片描述][74f8fea908af483cb3f99d05451db204.png] # 二、编写实体类 # 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 ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 1] 输入http://localhost:8086/api/selectUserOne ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 2] 输入http://localhost:8086/api/selectUserMany ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 3] 整体项目结构 ![在这里插入图片描述][watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_19_color_FFFFFF_t_70_g_se_x_16] **参考文章** [SpringBoot - MyBatis-Plus使用详解7(Mapper的CRUD接口4:多表关联查询)][SpringBoot - MyBatis-Plus_7_Mapper_CRUD_4] [SpringBoot_Mybatis-Plus]: https://blog.csdn.net/jike11231/article/details/120656683?spm=1001.2014.3001.5501 [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/3f66b0845f834f498e0c59f1913317fd.png [ad5d07b8a6584851b44eaf7e75dfe80e.png]: /images/20220828/2e7cc65e0991472fa06d9d8cdf9c352b.png [968825e0c7da4f5ebb1d0bd932e45075.png]: /images/20220828/dc310963d91b4e8995a14cb5e73059ef.png [74f8fea908af483cb3f99d05451db204.png]: /images/20220828/4c6b302f944146a08cfe62c564128094.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 1]: /images/20220828/a8997e7d5f22452baf4cd796fb12e65a.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 2]: /images/20220828/023d3937773041459a9cd4c38b641441.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_20_color_FFFFFF_t_70_g_se_x_16 3]: /images/20220828/ce5928d8e5d24994b129619ffbd6fcbe.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5oeS6Jmr6Jmrfg_size_19_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/1eb225cb1b1b46aca7765949c8c06bc0.png [SpringBoot - MyBatis-Plus_7_Mapper_CRUD_4]: https://www.hangge.com/blog/cache/detail_2903.html#
还没有评论,来说两句吧...