我這里是拿商品做為例子
不多說直接上代碼
?
Mapper.xml
MybatisPlus、?
<?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();} }
希望能幫助大家,謝謝。
?