SpaingBoot中Mybatis外键联表查询

本文介绍了一个使用MyBatis进行联合查询的例子,通过Mapper文件定义了复杂的SQL语句来实现不同表之间的关联,并展示了如何映射实体类。
Mapper文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="io.dao.gather.PeopleCollectionDao">

	<!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="io.entity.gather.PeopleCollectionEntity" id="peopleCollectionMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="identity" column="identity"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="filePath" column="file_path"/>
        <result property="remark" column="remark"/>
        <result property="rank" column="rank"/>
        <result property="flag" column="flag"/>
        <result property="type" column="type"/>
        <association property="positionCollectionEntity" javaType="io.entity.gather.PositionCollectionEntity"
        column="flag" resultMap="positionCollectionMap">
        </association>
    </resultMap>

    <!-- 外键Map -->
    <resultMap type="io.entity.gather.PositionCollectionEntity" id="positionCollectionMap">
        <id property="id" column="id" />
        <result property="flag" column="flag" />
        <result property="positionX" column="position_x" />
        <result property="positionY" column="position_y" />
    </resultMap>

    <select id="queryByName" resultMap="peopleCollectionMap">
        SELECT * from tb_gather_collection_people_info people JOIN tb_gather_collection_position positon on people.flag =positon.flag where people.name=#{name}
    </select>

</mapper>



Entity文件:

package io.entity.gather;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;

import java.io.Serializable;

@TableName("tb_gather_collection_people_info")
public class PeopleCollectionEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    @TableId
    private Long id;
    /**
     * 姓名
     */
    private String name;
    /**
     * 身份证号
     */
    private String identity;
    /**
     * 手机号码
     */
    private String phone;
    /**
     * 户籍地址
     */
    private String address;
    /**
     * 文件位置
     */
    private String filePath;

    /**
     * 备注
     */
    private String remark;
    /**
     * 序号(用于区分同行人员第几位)
     */
    private Integer rank;
    /**
     * 同一次数据采集的标识
     */
    private String flag;
    /**
     * 1,人员信息,2,同行人员,3,车主信息,4,房东信息,5,驾驶员,6,车上人员
     */
    private Integer type;

    /**
     * 采集场景外键
     */
     private PositionCollectionEntity positionCollectionEntity;
//省略getter和setter方法        
 }

MyBatis中,如果你需要处理数据库之间的,通常是在映射文件(XML或注解形式)中设置关系。这里以XML为例,假设你有两个实体,一个是用户(User),另一个是订单(Order),Order有一个字段userId引用User的id: 1. **配置实体类**: 首先,你需要为这两个类创建Java Bean,并标注它们的属性为`@Table`,并提供约束的字段。 ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // 其他字段... } @Entity @Table(name = "order") public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long orderId; @ManyToOne @JoinColumn(name = "user_id") // 指定名称 private User userId; // 用户ID // 其他字段... } ``` 2. **映射文件**: 在Mapper XML文件中,`<association>`元素用于示一对多或一对一的关系,其中`joinColumn`属性指定了字段和关字段的对应关系: ```xml <select id="selectOrder" resultType="Order"> SELECT * FROM order o JOIN user u ON o.user_id = u.id </select> <mapper namespace="yourMapperNamespace"> <resultMap id="orderResultMap" type="Order"> <id property="orderId" column="order_id"/> <!-- 使用association来映射 --> <association property="userId" javaType="User" column="user_id"> <id property="id" column="id"/> <!-- 如果有其他属性需要映射,可以添加更多<property>标签 --> </association> </resultMap> </mapper> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值