MyBatis查询主表数据附带从表N条数据

问题描述:MyBatis 两表关联查询,主表显示数据的同时将从表1-N条数据显示

利用postman测试结果显示图,圆圈为从表数据,箭头为主表数据
在这里插入图片描述
下面是代码:
MyBatis:

 <resultMap id="ResultMap" type="com.bmcs.ps.web.vo.BannerDeVO">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="width" property="width" jdbcType="FLOAT"/>
        <result column="height" property="height" jdbcType="FLOAT"/>
        <result column="describe" property="describe" jdbcType="VARCHAR"/>
        <result column="code" property="code" jdbcType="VARCHAR"/>
        <result property="count" column="count" jdbcType="INTEGER"/>
        <result property="sell" column="sell" jdbcType="INTEGER"/>
        <result property="free" column="free" jdbcType="INTEGER"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <collection property="bannerDetailList" column="code" select="com.bmcs.ps.dal.mapper.BannerDetailMapper.findByCode"/>
    </resultMap>

    <select id="getAllBannerDetail" parameterType="com.bmcs.ps.web.vo.PageVO" resultMap="ResultMap">
        select
        b.id,b.`code`,b.width,b.height,b.`describe`,b.`code`,b.type,bd.`no`,bd.imageurl,bd.hyperurl,bd.starttime,bd.endtime,bd.price,bd.sort,bd.`status`,bd.`code`,bd.createtime
        from banner as b LEFT JOIN bannerdetail as bd ON b.`code` = bd.`code` where 1 = 1
        <if test="describe!=null and describe!=''">
            and b.`describe` like "%"#{describe}"%"
        </if>
        <if test="type!=null">
            and b.type=#{type}
        </if>
        GROUP BY b.id
        limit #{startpos},#{pagesize}
    </select>

实体类:

public class BannerDeVO implements Serializable {
    private Integer id;//id
    private float width;//宽
    private float height;//高
    private String describe;//描述
    private Integer type;//类型
    private String code;
    private Integer count;
    private Integer sell;
    private Integer free;
    private List<BannerDetail> bannerDetailList;

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
    public Integer getSell() {
        return sell;
    }

    public void setSell(Integer sell) {
        this.sell = sell;
    }

    public Integer getFree() {
        return free;
    }

    public void setFree(Integer free) {
        this.free = free;
    }


    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }



    public List<BannerDetail> getBannerDetailList() {
        return bannerDetailList;
    }

    public void setBannerDetailList(List<BannerDetail> bannerDetailList) {
        this.bannerDetailList = bannerDetailList;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

}

  • 实体类中BannerDeVO 包含主表个别需要显示的属性,BannerDetail类为从表实体类
  • < collection property=“bannerDetailList” column=“code” select=“com.bmcs.ps.dal.mapper.BannerDetailMapper.findByCode”/>详解

1、BannerDeVO类 中List< BannerDetail >取的 bannerDetailList要和MyBatis中collection标签里面 property="bannerDetailList"对应

2、column = “code” 按照显示主表中code = “”显示从表中N条数据

3、select=“com.bmcs.ps.dal.mapper.BannerDetailMapper.findByCode”/>按照mapper中findByCode根据code条件显示BannerDetail从表数据

BannerDetailMapper中findByCode

 List<BannerDetail> findByCode(@Param("code") Integer code);
  <select id="findByCode" parameterType="java.lang.String" resultType="com.bmcs.ps.dal.entity.BannerDetail">
      SELECT * FROM bannerdetail WHERE `code` = #{code}
    </select>

猜你喜欢

转载自blog.csdn.net/qq_40588618/article/details/89517692
今日推荐