Mybatis, 实现一对多

我这里是拿商品做为例子

不多说直接上代码

 

Mapper.xml

 

<?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="com.nf147.mall.dao.CommodityMapper">
    <resultMap id="BaseResultMap" type="com.nf147.mall.entity.Commodity">
        <id column="product_id" jdbcType="INTEGER" property="productId"/>
        <result column="category_id" jdbcType="INTEGER" property="categoryId"/>
        <result column="product_code" jdbcType="VARCHAR" property="productCode"/>
        <result column="product_name" jdbcType="VARCHAR" property="productName"/>
        <result column="product_content" jdbcType="LONGVARCHAR" property="productContent"/>

     <!--这里还需要标明关系  property属性对应是实体类的字段名-->
        <association property="standard" resultMap="StandardResultMap"></association>
        <association property="dommodityattribute" resultMap="DommodityattributeResultMap"></association>
    </resultMap>
         
    <!--这个是要查询的表1  总之你直接去要查询的表复制就好 -->
<resultMap id="DommodityattributeResultMap" type="com.nf147.mall.entity.Dommodityattribute">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="product_id" jdbcType="INTEGER" property="productId"/>
        <result column="product_simg" jdbcType="VARCHAR" property="productSimg"/>
</resultMap>
        <!--这个是要查询的表2-->
<resultMap id="StandardResultMap" type="com.nf147.mall.entity.Standard">
        <id column="specs_id" jdbcType="INTEGER" property="specsId"/>
        <result column="product_id" jdbcType="INTEGER" property="productId"/>
        <result column="product_specs" jdbcType="VARCHAR" property="productSpecs"/>
        <result column="product_price" jdbcType="DECIMAL" property="productPrice"/>
</resultMap>

 

实体类:

public class Commodity {
    private Integer productId;

    private Integer categoryId;

    private String productCode;

    private String productName;

    private String productContent;

    private Standard standard;      //第一个表的实体类

    private Dommodityattribute dommodityattribute;  //第二个表的实体类  然后再提供 get 和 set 的方法

    public Standard getStandard() {
        return standard;
    }

    public void setStandard(Standard standard) {
        this.standard = standard;
    }

    public Dommodityattribute getDommodityattribute() {
        return dommodityattribute;
    }

    public void setDommodityattribute(Dommodityattribute dommodityattribute) {
        this.dommodityattribute = dommodityattribute;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode == null ? null : productCode.trim();
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName == null ? null : productName.trim();
    }

    public String getProductContent() {
        return productContent;
    }

    public void setProductContent(String productContent) {
        this.productContent = productContent == null ? null : productContent.trim();
    }
}

希望能帮助大家,谢谢。

 

posted @ 2019-01-16 10:10  随★风  阅读(594)  评论(0编辑  收藏  举报