ruoyi 对表查询(一对一,一对多)

该博客介绍了 MyBatis 中的联合查询映射配置,包括一对一和一对多的关联映射。示例展示了如何在 Mapper XML 文件中定义 resultMap,以及对应的实体类属性,用于查询 LmbBirdCollect 数据,包括其关联的 LmbBirdPoint 和 LmbBirdCollects 集合。同时,提供了 SQL 查询语句,通过 LEFT JOIN 实现数据的联合查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mapper.XML

<!--联合查询用到的map-->
    <resultMap type="LmbBirdCollect" id="LmbBirdCollectResult2">
        <id property="collectId"    column="collect_id"    />
        <result property="pointId"    column="point_id"    />
        <result property="collectTime"    column="collect_time"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <!--一对一,使用association,property是在LmbBirdCollect实例类中的字段名,column是在LmbBirdCollect中和LmbBirdPoint相关联的字段-->
        <association property="birdPoint" column="point_id" javaType="LmbBirdPoint" >
            <id property="pointId"    column="point_id"    />
            <result property="itemId"    column="item_id"    />
            <result property="pointName"    column="point_name"    />
            <result property="longitude"    column="longitude"    />
            <result property="latitude"    column="latitude"    />
            <result property="altitude"    column="altitude"    />
            <result property="remarks"    column="remarks"    />
            <result property="status"    column="status"    />
        </association>
        <!--一对多,使用collection,property是LmbBirdCollect实例类中的字段名,column是在LmbBirdCollect实例类和LmbBirdCollects关联的字段,注意:这里要使用ofType-->
        <collection property="birdCollects" column="collect_id" ofType="LmbBirdCollects">
            <id property="collecstId"    column="collects_id"    />
            <result property="collectId"    column="collect_id"    />
            <result property="birdId"    column="bird_id"    />
            <result property="quantity"    column="quantity"    />
        </collection>
    </resultMap>

<select id="selectLmbBirdCollectByCollectId" parameterType="Long" resultMap="LmbBirdCollectResult2">
        SELECT c.collect_id, c.point_id, c.collect_time, c.status, c.del_flag, c.create_by,
               c.create_time, c.update_by, c.update_time, c.remark ,p.point_name,s.bird_id,s.quantity
        FROM lmb_bird_collect c
        LEFT JOIN lmb_bird_point p ON c.point_id=p.point_id
        LEFT JOIN lmb_bird_collects s ON c.collect_id=s.collect_id
        where c.collect_id = #{collectId}
 </select>    

实体类:

public class LmbBirdCollect extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 自增id */
    private Long collectId;

    /** 所属区域 */
    @Excel(name = "所属点")
    private Long pointId;

    /** 创建时间 */
    @Excel(name = "调查时间", width = 30, dateFormat = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date collectTime;

    /** 鸟种 */
    @Excel(name = "状态")
    private String status;

    private LmbBirdPoint birdPoint;
    private List<LmbBirdCollects> birdCollects;

    public List<LmbBirdCollects> getBirdCollects() {
        if(birdCollects == null){
            List<LmbBirdCollects> birdCollects = new ArrayList<>();
        }
        return birdCollects;
    }

    public void setBirdCollects(List<LmbBirdCollects> birdCollects) {
        this.birdCollects = birdCollects;
    }

    public LmbBirdPoint getBirdPoint() {
        if(birdPoint == null){
            birdPoint = new LmbBirdPoint();
        }

        return birdPoint;
    }

    public void setBirdPoint(LmbBirdPoint birdPoint) {
        this.birdPoint = birdPoint;
    }

    /** 删除标志(0代表存在 2代表删除) */
    private String delFlag;

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public Long getCollectId() {
        return collectId;
    }

    public void setCollectId(Long collectId) {
        this.collectId = collectId;
    }

    public Long getPointId() {
        return pointId;
    }

    public void setPointId(Long pointId) {
        this.pointId = pointId;
    }

    public Date getCollectTime() {
        return collectTime;
    }

    public void setCollectTime(Date collectTime) {
        this.collectTime = collectTime;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "LmbBirdCollect{" +
                "collectId=" + collectId +
                ", pointId=" + pointId +
                ", collectTime=" + collectTime +
                ", status='" + status + '\'' +
                ", birdPoint=" + birdPoint +
                ", birdCollects=" + birdCollects +
                ", delFlag='" + delFlag + '\'' +
                '}';
    }
}
### 若依 Vue 框架中实现一对主子功能的方式 在若依 Vue 框架中,实现一对主子功能通常涉及以下几个方面:数据库设计、后端接口开发以及前端页面展示逻辑。 #### 数据库设计 为了支持商品管理和分类的关系,在数据库层面需要定义两张——`商品分类` 和 `商品`。这两张通过外键关联形成主子关系。以下是创建这两张的 SQL 语句: ```sql -- 创建商品分类 () CREATE TABLE product_category ( id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID', name VARCHAR(50) NOT NULL COMMENT '分类名称' ); -- 创建商品 (),包含外键指向分类 CREATE TABLE product ( id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID', category_id BIGINT NOT NULL COMMENT '所属分类ID', name VARCHAR(100) NOT NULL COMMENT '商品名称', price DECIMAL(10, 2) NOT NULL COMMENT '价格', FOREIGN KEY (category_id) REFERENCES product_category(id) ); ``` 上述 SQL 定义了两者的主子关系[^1]。 --- #### 后端接口开发 后端部分主要负责处理数据交互,包括 VO 类的设计和 Mapper 方法的编写。 ##### 新建 VO 类 VO(Value Object)用于封装返回给前端的数据结构。对于主子关系,可以在父对象中嵌套子对象列。例如: ```java public class CategoryVo { private Long id; private String name; // 子集合 private List<Product> products = new ArrayList<>(); // Getter and Setter methods... } public class Product { private Long id; private Long categoryId; private String name; private BigDecimal price; // Getter and Setter methods... } ``` ##### 编写 Mapper 方法 Mapper 层需提供查询方法以加载主及其对应的子记录。可以通过 MyBatis 的 `<resultMap>` 配置完成复杂映射: ```xml <!-- 查询分类及对应的商品 --> <select id="selectCategoryWithProducts" resultType="com.example.CategoryVo"> SELECT c.id AS cid, c.name AS cname, p.id AS pid, p.category_id AS pcid, p.name AS pname, p.price AS pprice FROM product_category c LEFT JOIN product p ON c.id = p.category_id </select> ``` 随后,可基于此配置构建 Service 层逻辑,将结果转换为所需的 VO 结构[^2]。 --- #### 前端页面展示 Vue.js 中可通过组件化的方式来渲染主子数据。以下是一个简单的示例: ##### 组件模板 ```vue <template> <div> <!-- 商品分类列 --> <el-table :data="categories" style="width: 100%"> <el-table-column prop="name" label="分类名称"></el-table-column> <!-- 商品列 --> <el-table-column label="商品"> <template slot-scope="scope"> <ul> <li v-for="(product, index) in scope.row.products" :key="index"> {{ product.name }} - ¥{{ product.price }} </li> </ul> </template> </el-table-column> </el-table> </div> </template> ``` ##### 脚本部分 ```javascript export default { data() { return { categories: [] // 分类数据数组 }; }, created() { this.fetchData(); }, methods: { fetchData() { // 请求后端 API 获取数据 axios.get('/api/categories-with-products').then(response => { this.categories = response.data; // 将响应数据赋值给 categories }); } } }; ``` 在此过程中,需要注意将后端返回的数据绑定至前端模型中的相应字段[^3]。 --- #### 添加新业务模块 如果希望扩展新的业务模块,则可以参考若依微服务架构的做法。具体操作如下: 1. 在项目根目录下新建一个独立的 Maven 模块。 2. 复制现有模块(如 `ruoyi-modules-system`)中的依赖项到新模块的 `pom.xml` 文件中。 3. 修改相关路径和服务注册信息以便集成 Spring Cloud 微服务体系[^4]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值