MyBatis一对多或多对多处理方式

一切从业务出发
产品表:prdt
在这里插入图片描述
房间表:room
在这里插入图片描述
产品适用房间表:prdtroom
在这里插入图片描述
需求:
通过查产品能查到对应适用的房间位置

测试SQL

select
            prdt.id as id,
            prdt.productName as productName,
            prdt.productImage as productImage,
            prdt.livetype as livetype,
            prdt.typeName as typeName,
            prdt.spc as spc,
            prdt.productUnit as productUnit,
            prdt.parameters as parameters,
            prdt.agencyPrice as agencyPrice,
            prdt.customPrice as customPrice,
            prdt.price as price,
            prdt.rem as rem,
            prdt.status as status,
            prdt.update_time as update_time,
            prdt.create_by as create_by,
            prdt.create_time as create_time,
            room.id as rid,
            room.name as name
        from prdt
        left join prdtroom on prdtroom.productId=prdt.id
        left join room on room.id=prdtroom.roomId
         where prdt.id=1

显示正常:
在这里插入图片描述
显示正常

实体层

package com.flo.po;

import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class Prdt {
    
    
    private Integer id;//1 ID
    private String productName;// 2 产品名称
    private String productImage;// 3 主图
    private String typeName; // 4 产品分类
    private String spc; // 5 产品规格
    private String productUnit; //6 产品单位
    private String parameters;//7 技术参数
    private String customPrice;//8 经销价
    private String agencyPrice;//9 代理价
    private String rem;//10 产品描述
    private Integer status;//状态11
    private Integer createBy;//创建人12
    private Date createTime;//创建时间13
    private Date updateTime;//修改时间14
    private String price;//市场价格
    private Integer liveType;//供电类型
    private List<Room> rooms;//适用房间类型
}

dao

/**
     * 根据ID查询产口
     */
    Prdt findProductById(Integer id);

mapper

<!-- 自定义结果映射 -->
    <resultMap type="com.flo.po.Prdt" id="productRoom">
        <id property="id" column="id"/>
        <result property="productName" column="productName"/>
        <result property="productImage" column="productImage"/>
        <result property="liveType" column="liveType"/>
        <result property="typeName" column="typeName"/>
        <result property="spc" column="spc"/>
        <result property="productUnit" column="productUnit"/>
        <result property="parameters" column="parameters"/>
        <result property="parameters" column="parameters"/>
        <result property="agencyPrice" column="agencyPrice"/>
        <result property="customPrice" column="customPrice"/>
        <result property="price" column="price"/>
        <result property="rem" column="rem"/>
        <result property="status" column="status"/>
        <result property="productName" column="productName"/>
        <!-- 多表关联映射 -->
        <collection property="rooms" ofType="com.flo.po.Room">
            <id property="rid" column="id"/>
            <result property="name" column="name"/>
        </collection>
    </resultMap>

<!--    根据ID查询产品-->
    <select id="findProductById" parameterType="integer" resultMap="productRoom">
        select
            prdt.id as id,
            prdt.productName as productName,
            prdt.productImage as productImage,
            prdt.livetype as livetype,
            prdt.typeName as typeName,
            prdt.spc as spc,
            prdt.productUnit as productUnit,
            prdt.parameters as parameters,
            prdt.agencyPrice as agencyPrice,
            prdt.customPrice as customPrice,
            prdt.price as price,
            prdt.rem as rem,
            prdt.status as status,
            prdt.update_time as update_time,
            prdt.create_by as create_by,
            prdt.create_time as create_time,
            room.id as rid,
            room.name as name
        from prdt
        left join prdtroom on prdtroom.productId=prdt.id
        left join room on room.id=prdtroom.roomId
         where prdt.id=#{id}
    </select>

其它过程在这里不一一说明。
Controller层

@RequestMapping("updateProduct")
    public String updateProduct(Integer id,Model model){
    
    
        Prdt prdt=prdtService.findProductById(id);
        System.out.println(prdt.toString());//显示结果
        model.addAttribute("product",prdt);
        return "page/updateProduct";
    }

效果如下:

Prdt(id=1, productName=自营智能语音音响, productImage=/uploadFile/20200713170783878387.jpg, typeName=智能音响, spc=20年夏季新款, productUnit=个, parameters=讯飞内核, customPrice=299.00, agencyPrice=290, rem=test, status=0, createBy=null, createTime=null, updateTime=null, price=318.00, liveType=3, rooms=[Room(rid=1, name=客厅, prdts=null)])

好了,前台可以正常显示了。

后记:
需要注意的是定义的 实体从类(room)的字段不能与主类(prdt)有重复。比如Room里有一个ID ,但prdt 里也有一个ID ,需要把room实体类的ID换一下。

猜你喜欢

转载自blog.csdn.net/weixin_44690195/article/details/108795702